CST8204:
Lab Exercise 5
Reading directories in C
Directories and i-nodes in Linux and Unix contain the information needed to manage the file systems and disk space.
Here is some information based on the man pages for each. Remember that the include files are found in /usr/include and its subdirectories, if you need to look at them. References are Chapter 4 of Stevens in sections 4.2 (page 73) and 4.21 (page 107), and a bit in K&R2 in section 8.6 (page 179). See also the Lab Prep.
opendir(3), readdir(3), closedir(3) - read directory entries
DESCRIPTION
The opendir() function opens a directory stream corresponding to the directory name, and returns a pointer (DIR *dirp) to the directory stream. The stream is positioned at the first entry in the directory. The function returns a pointer to the directory stream or NULL if an error occurred.
The readdir() function returns a pointer to a dirent structure representing the next available directory entry in the directory stream pointed to by dirp. It returns NULL on reaching the end-of-file (no more directory entries) or if an error occurs. The data returned by readdir() is overwritten each time by subsequent calls to it for the same directory stream. The only field you should use in the dirent structure is char d_name[].
closedir() closes the directory stream. It’s important to use it before your program terminates or before you open the next directory because it frees memory allocated for you for the directory stream and entry.
stat(2), lstat(2), fstat(2) - get status for a file (from its i-node)
DESCRIPTION
These functions return i-node information about a file. You do not need any access rights to the destination file to get this information but you need at least access rights (--x; better is r-x) to all directories in the path leading to the file.
stat(2) "stats" the file pointed to by filename and fills in buf, which you must provide, perhaps by struct stat buf; and the argument &buf in the stat() call.
lstat(2) is identical to stat(2) except in the case of a symbolic link where the link itself is "stat"ed, not the file that it refers to. fstat(2) is also identical to stat(2), except that the open file pointed to by filedes (as returned by a successful open(2) or creat(2)) is "stat"ed in place of a filename.
Using the opendir(3), readdir(3), closedir(3),and stat(2) system calls (you may also use others, of course, except for the always-prohibited system(3) call), write a simple C program to list some file information, perhaps file sizes or mode flags (the mode reads best in octal).
There's no need to reproduce the ls(1) or stat(1) commands, but see how these commands and functions work and the kind of information they return. Be careful in using the man pages, since two of the function names above appear more than once and they're not the ones you want! Use "man section name", where section is the section number in parentheses and name is the name of the command or function, like man 2 readdir.
Demo your program to the instructor before the end of the lab; don't wait until the last minute if you need some help. In fact, you should have started yesterday, or even the day before, with the Lab Prep document and the textbooks. Why didn't you?