Lab 6 Preparation

As you recall, a single directory entry referring to a single file, directory, or whatever contains only two useful pieces of data:

  1. the i-node number (which we don't use directly);
  2. the file name (which we do, carefully).

The useful elements of a directory entry can be considered to look like this:

struct dirent {
ino_t d_ino; /* don't use */

char d_name[256]; /* 255 + 1 */ };

We most often use only the d_name field in a directory entry, which contains the bare filename, zero-terminated as a normal C string (note: the '\0' is not stored in the directory on disk, only in its memory representation). To use the d_name for anything outside its own directory, you must prefix it with an absolute or relative path. If you're not sure what these are, see Stevens/Rago on page 5 or your notes from last semester. You can use sprintf(3), or strcpy(3) and strcat(3) together, to form the fully-qualified name.

You can use a path-qualified filename in a call to stat(2) to get the i-node information contained in the struct stat. like the file type in st_mode and the actual i-node number (st_ino; pretend it's of type unsigned long).

There is a good sample of a recursive directory-reading program in Section 8.6 of K&R2, with examples of much of what you need to know for this exercise. Read it over carefully and ask me if you are confused by the recursion, the function pointer, or anything else in it.

You need to recall that each directory contains 2 special entries:

. this directory itself, a self-reference: dot

.. this directory's parent directory: dot-dot

All other directory entries are of files or subdirectories (or a few other things like links, named pipes, and such). The tree structure consists of i-node numbers that connect a directory forward to its subdirectories and backward to its parent directory, ultimately leading to the root directory /, which has both the . and the .. entries with the same number (which is how you can tell it's the root)

IANAA (I Am Not An Artist), but here is a schematic diagram showing a small section of the directory tree with a total of 6 directories. We do not know what the parent directory of D1 is, but it has two subdirectories, D21 and D22. D21 has two more subdirectories itself, D21a and D21b, neither of which has any more subdirectories. D22 has a single subdirectory, D22a, that leads nowhere.

I have shown each . self-reference as a rather lovely circle-arrow-line. In each case, the i-node number field in that directory entry contains the i-node number of that directory. The .. entry uses a strong straight line leading back to the directory where its name is given, to the parent directory. For example, the i-node number in D22a's .. entry is the same number as the . entry in directory D22. The D22a entry in D22 has the same i-node number as the . entry in D22a.


You can explore i-node numbers from a terminal window by using the -i argument of ls, which can also be combined with other options. For example, ls -ial will show you the i-node number (-i) for all entries (-a; includes "hidden" files that start with a dot) in the long form (-l). You can save the output of an ls (or indeed any command) by redirecting the output to a file: ls -ial > ls1.

Review the directory-reading commands from last week along with stat(2), then read over the instructions for Lab 6 and follow them carefully.