Question: Introduction This laboratory examines directory structures and builds on a recursive directory listing program. While directories are stored on disk, there is a special set
Introduction
This laboratory examines directory structures and builds on a recursive directory listing program. While directories are stored on disk, there is a special set of procedures that are used to manipulate them. This laboratory will introduce these procedures, and you will make an addition to an existing directory listing program.
Directory Procedures
Linux has a set of procedures that are used to manipulate directories. These procedures treat directories as a stream of directory entries. A directory must be opened, then it can be read one directory at a time. After processing the directory is closed. The opendir() procedure is used to open a directory. The single parameter to this function is a text string which is the name of the directory. This procedure returns a pointer to a directory stream of type DIR. The readdir()procedure reads one directory entry and returns a pointer to a dirent structure that represents the contents of that directory entry. The single parameter to this procedure is the directory stream. This procedure sequentially reads directory entries. The man 3 readdir man page describes the structure that readdir returns. The closedir() procedure closes the directory once you are finishing processing it. The single parameter to this procedure is the directory stream.
Directory Listing Program
lab5.c program that performs a recursive directory listing. The parameter to this program is the directory where the listing starts. It sequentially reads each entry in the directory and prints the Inode number and file name. If the entry is a directory it lists the contents of that directory before going on to the next directory entry. Most of this work is done by the dumpDir() procedure that calls itself recursively to list sub directories. In this laboratory you will make a small change to the listing. Now each directory entry is displayed on a single line with the Inode and file name. Add one more field to this line which is the type of file (regular file, directory, symbolic link, etc.). This information is stored in the dirent structure returned by readdir(). The man page for readdir explains how to interpret this structure and determine the file type.
Explain how you determined the file type.
/***************************************** * lab5.c * * Starting point for laboratory 5 solution * The single parameter to this progra is * the path to the directory to be listed. ****************************************/ #include
/* * This procedure prints the contents of a directory. * If a directory entry is another directory it calls * itself recursively the list the contents of that * directory. * * The parameters to this procedure are: * dir - the directory stream for the directory * indent - the level of the directory in the listing * base - the filename of the directory */ void dumpDir(DIR *dir, int indent, char *base) { struct dirent *entry; // the current directory entry char *name; // the name of the entry int type; // the type of the directory entry DIR *newdir; // directroy stream for recursive listing char *dirname; // full name of sub-directory int len; int i;
entry = readdir(dir); while(entry != NULL) { name = entry->d_name; type = entry->d_type; if(name[0] != '.') { // skip filenames that start with . for(i=0; i
int main(int argc, char **argv) { DIR *dir;
if(argc != 2) { printf("usage: lba5 directory "); exit(1); }
dir = opendir(argv[1]); if(dir == NULL) { printf("can't open directory: %s ", argv[1]); exit(1); } dumpDir(dir,0,argv[1]); closedir(dir); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
