CST8204:
Lab Exercise 6
More on i-nodes
You learned last week that directories and i-nodes in Linux and Unix contain the information needed to manage the file systems and disk space.
Use this new-found knowledge to write a program to list the directory tree from either the current directory '.' or from a single directory name in argv that shows the i-node number for each directory as well as the structure of the tree. To keep your output tidy, you may wish to use a format like %7lu, where the '7' (or some other number; perhaps 8 or 9 is better) is the minimum width in which to print the i-node.
You will want your output to resemble this, indenting each new directory level by a few characters (2 or 3 should be enough to make a visible difference). The sample below on the left uses spaces alone, while the right-hand sample is slightly more elaborate and uses pipes, pluses, and dashes; either technique is satisfactory.
|
|
To accomplish this, first create a simple directory structure perhaps similar to what has been used in the example above. Your program will then verify the values of argc and argv to determine where to start. If a starting directory is supplied as an argument, first confirm that it is indeed a directory via stat(2). If no argument is given, then start at the current directory.
You may use either a recursive (calling a function from itself, returning when the bottom of each branch of the tree is reached; see samples of recursion in K&R2) or an iterative (while or do-while loops) technique, whichever you find more comfortable.
Demo your program, including testing for run-time error cases (e.g., uses '.' if there is no argument , error if there's more than one argument or the argument is not a directory, etc.), to the instructor before the end of the lab.