CS 3733 Operating Systems, Spring 2000 Assignment 3


Due Friday, March 3

The functions you will be writing for this assignment are based on Program 3.1. Create a separate directory for all of the files for this assignment.

The utility routines from parts 1 through 4 should be put in a file called directory_utilities.c. You will also write a main function which will go in directory_driver.c.

You should have a makefile which compiles all of the code and can run lint on the two files together.

Create an include file, directory_utilities.h, containing the prototypes and other definitions in directory_utilities.

All I/O done in directory_utilities should use file descriptors. None of the functions in directory_utilities described here send any output to standard out or standard error.

There are copies of directory_utilities.h and a makefile available in /usr/local/courses/cs3733/spring2000/assign3. The cover sheet for handing in this assignment is also in this directory.

The main program described in Part 5 whould be written incrementally, to test the first 4 parts of the assignment as they are completed.

Part 1
Write a function which, given a directory name, returns the number of files in that directory, other than . and ..

    int get_number_files(char *dir);

Part 2
Write a function which given a directory name returns an array of strings containing the names of the files in that directory. It should not include either . or .. .

   char **get_file_list(char *dir);
It returns NULL of there is an error or if there are no files to return. If it returns NULL, it should clean up all allocated space.
Hint: Don't implement the cleanup until the rest of the assignment is working.

Part 3
Write a function given a directory and filename returns something of type file_data which contains the name and contents of the file

   typedef struct file_data {
      int size;
      char *name;
      char *data;
   } file_data;

   file_data *get_file_data(char *dir, char *name);

It returns NULL if there is an error reading the file. It should not print anything, even if there is an error.

Note: this function must allocate space for the a variable of type file_data, for the name string, and the data array.

Part 4
Write a function which, given a directory name, returns an array of file_data elements corresponding to the files in that directory. You may assume that the directory contains only ordinary files (no subdirectories) other than the . and .. directories. Use your functions you have already written. It returns NULL if there is an error.

   file_data *get_directory_files(char *dir);

Note that this function returns an array of structures, not an array of pointers to structures.

Part 5
Write a main function which is in its own directory and takes one command line parameter. The parameter represents the name of a directory. The program starts by identifying the programmer. It then calls get_directory_files and displays the result returned. It should display the names, sizes, and contents of all of the files. Test this with a simple directory which you create containing 3 short ASCII files. Put the main program in a file called directory_driver.

Test your program using the directory /home/srobbins/public_html/cs3733/assign3test and print the results of running your program on this directory.