Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please I need help for the following assignment: The goal of this project is to introduce you to the basic library functions for reading directories

Please I need help for the following assignment:

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 "-b", where "f" stands for the ascending order, and "b" 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 "-b" 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 buf[]) contains the following characters "abc.txt\0def.txt\0ghi.txt\0" (three files: abc.txt, def.txt and ghi.txt), sa[0] should store the address of buf[0], 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.

II. Note:

  1. Your program must be written in C. Operations on C-style strings may make use of the C string library.
  2. All operations on directories must be done using the system calls described in the lectures. These are declared in dirent.h and include opendir( ), readdir( ), rewinddir( ), and closedir( ).

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

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 2 Lnai 6322

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215882X, 978-3642158827

More Books

Students also viewed these Databases questions

Question

=+7. How did investors react to the acquisition announcement?

Answered: 1 week ago