Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The goal of this project is to introduce you to the basic library functions for reading directories in Linux. You will implement a program that

image text in transcribed

The goal of this project is to introduce you to the basic library functions for reading directories in Linux. You will implement a program that lists all the files in a given directory. The final listing will be accomplished by your stringout(const char[], const char" function. 1. Your program should take one or two command line parameters, where the first is a path (relative or absolute) to a directory and the second if present, is passed along to stringout( . ) as the second argument. If there is only one command line argument supply an empty string for the second argument to stringout( . ). An example command line is: $ myls path_to_dir [flag] Here "myls" is the name of your executable file. Some examples of "path_to_dir" are "/", "/usr/local/bin", ".","..". The optional flags can be "-f" or "-", where "f" stands for the ascending order, and "6" is for the descending order (provided by strcmp(). 2. Your program should list all of the files in the directory specified by "path_to_dir" to standard output in the order encountered by readdir. or else in the order indicated by the flags parameter if it is one of the proper nonempty forms. List the names of the files one per line. 3. stringout.c: considering that the number of files in a particular directory is not pre-determined, your program is expected to be robust enough so that it can list any number of files. In addition, your program is expected to output a sorted file list (either in ascending or descending order). To achieve the goal in an efficient way, you need to implement stringout() function as explained below. stringout function takes two arguments: const char*[] and const char * The first argument is supposed to be a NULL terminated array of C style strings. and the second is just a C style string. This function call stringout (sa, flag) should write the strings pointed by each element of sa (again sa is a pointer array) to system standard output, one per line, in an order which is determined by flag. If flag is an empty string the output order will be exactly as in sa. If flag is "-f", the output will be in ascending order. And flag "-6" will give a descending order of the file list. The intention of using sa (char[] data structure) is to simplify the file name sorting. Since the number of files in a directory is unknown, it is suggested that you repeatedly malloc a string array (with fixed size) to store the file names found in a directory. For example, suppose you initially allocate a string buffer with 1024 bytes to hold the filenames (note that you need to append a null terminator at the end of each file name). If the space is running out there are too many files), you then malloc( another string buffer with the same size. Your program should repeat the process until all files can be stored in the buffers. Everytime you copy a file name into the buffer, keep in mind that you separate them with null terminators, and you should assign the address of the corresponding string (the file name) to next available sa element. . For example, if your string buffer (char bufli) contains the following characters "abc.txt Odef.txt Oghi.txt 0" (three files: abc.txt, def.txt and ghi.txt), sa[0] should store the address of buffo], sa[1] should store the address of buf[8], and sa[2] should store the address of buf[17]. In this way, when sorting is performed, you only need to swap sa[0], sa[1] and sa[2], instead of modify anything in buf. 4. Your project submission should include the implementation files myls.c (along with its hearder file), stringout.c (along with its header file), a Makefile, and a README file. The goal of this project is to introduce you to the basic library functions for reading directories in Linux. You will implement a program that lists all the files in a given directory. The final listing will be accomplished by your stringout(const char[], const char" function. 1. Your program should take one or two command line parameters, where the first is a path (relative or absolute) to a directory and the second if present, is passed along to stringout( . ) as the second argument. If there is only one command line argument supply an empty string for the second argument to stringout( . ). An example command line is: $ myls path_to_dir [flag] Here "myls" is the name of your executable file. Some examples of "path_to_dir" are "/", "/usr/local/bin", ".","..". The optional flags can be "-f" or "-", where "f" stands for the ascending order, and "6" is for the descending order (provided by strcmp(). 2. Your program should list all of the files in the directory specified by "path_to_dir" to standard output in the order encountered by readdir. or else in the order indicated by the flags parameter if it is one of the proper nonempty forms. List the names of the files one per line. 3. stringout.c: considering that the number of files in a particular directory is not pre-determined, your program is expected to be robust enough so that it can list any number of files. In addition, your program is expected to output a sorted file list (either in ascending or descending order). To achieve the goal in an efficient way, you need to implement stringout() function as explained below. stringout function takes two arguments: const char*[] and const char * The first argument is supposed to be a NULL terminated array of C style strings. and the second is just a C style string. This function call stringout (sa, flag) should write the strings pointed by each element of sa (again sa is a pointer array) to system standard output, one per line, in an order which is determined by flag. If flag is an empty string the output order will be exactly as in sa. If flag is "-f", the output will be in ascending order. And flag "-6" will give a descending order of the file list. The intention of using sa (char[] data structure) is to simplify the file name sorting. Since the number of files in a directory is unknown, it is suggested that you repeatedly malloc a string array (with fixed size) to store the file names found in a directory. For example, suppose you initially allocate a string buffer with 1024 bytes to hold the filenames (note that you need to append a null terminator at the end of each file name). If the space is running out there are too many files), you then malloc( another string buffer with the same size. Your program should repeat the process until all files can be stored in the buffers. Everytime you copy a file name into the buffer, keep in mind that you separate them with null terminators, and you should assign the address of the corresponding string (the file name) to next available sa element. . For example, if your string buffer (char bufli) contains the following characters "abc.txt Odef.txt Oghi.txt 0" (three files: abc.txt, def.txt and ghi.txt), sa[0] should store the address of buffo], sa[1] should store the address of buf[8], and sa[2] should store the address of buf[17]. In this way, when sorting is performed, you only need to swap sa[0], sa[1] and sa[2], instead of modify anything in buf. 4. Your project submission should include the implementation files myls.c (along with its hearder file), stringout.c (along with its header file), a Makefile, and a README file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

When is livestock considered Sec. 1231 property?

Answered: 1 week ago

Question

Describe Watsons attempts to popularize the science of behavior?

Answered: 1 week ago

Question

b. Why were these values considered important?

Answered: 1 week ago