One of the key services
provided by any operating system is the management of files. The C language
meshes with UNIX nicely in this regard. In fact all UNIX system calls and most
libraries are implemented in C. In this programming assignment we will
implement a new version of the UNIX command ls. In the UNIX file system, a
directory contains names and attributes of files stored in it. A number of UNIX
library functions are available for accessing directories. To use any of them
you need to include the header dirent.h
The function opendir with prototpe DIR *opendir(const char *name); opens the named directory and
associates a directory stream with it. If the named directory cannot be
accessed or there is not enough memory to hold the contents of the directory, opendir returns NULL. Once a directory stream is opened, the function readdir sequentially access its contents.
The prototype of the readdir is struct dirent *readdir(DIR *dir);. This routine returns a pointer to
the next directory entry of type struct dirent. The pointer value becomes NULL when it reaches the end of the
directory.
Look at /usr/include/dirent.h for details on the struct dirent. Also, take a look at
/usr/include/sys/stat.h for details on the struct stat. There are other UNIX system
library
routines such as getcwd which gets the current working
directory.
RTFM for more details (i.e., man 3 getcwd). I have provided two demo programs which make use of some of
these calls, in addition to the code we started in lecture. A demo program
showing how to use getcwd and search a directory is given in
search.c. I've also provided the demo
program filetimes.c that allows us
to
get the last modified time of a file. This can be modified to find the size of
the
file as well. For more info on what stat
returns, check out man
2 stat.
The purpose of this program
is to implement the UNIX command, ls. You must write a program named lab6.c
(executable: myls) that does the following:
Note . and ..
should NOT be printed! Also, all commands must work if dir_name is not
specified, in which case the current directory should be used, i.e., ./myls -s should list all the files in current
directory sorted by size.
I would suggest using a dynamic array
of structs to read and store the information in the directory and then
manipulate this array to do the sorting and searching. One way to do this would
be to read the directory to determine the number of files contained within,
malloc the array of structs, and then use the rewinddir command to reposition to the front
of the directory and read the information in. Of course, you may choose another
implementation if you desire.
You may find the UNIX qsort utility handy for this assignment. We will discuss qsort and function pointers in lecture on Tuesday. For more
information, RTFM. As above, you are not required to use qsort to do the sorting...
Your solution should be in
the form of one .c file. Just hand in the .c file and your design document for your program.
Don't hand in any input or .o or exe files.