CS360 Midterm -- October 18, 1999. Question 4:
Answer and grading
14 points
This is very much like the prsize program from the
lecture notes. The code below does not solve the open file
problem, but that is ok since I said you didn't have to
solve it. Also, you should call lstat() instead of
stat(), but since I didn't put that in the cheat sheet,
it's ok if you missed it.
void do_prdr(char *dir)
{
char *s;
DIR *d;
struct dirent *de;
struct stat buf;
s = (char *) malloc(strlen(dir)+258);
d = opendir(dir);
if (d == NULL) { perror(dir); return; }
for (de = readdir(dir); de != NULL; de = readdir(dir)) {
if (strcmp(de->de_name, ".") != 0 && strcmp(de->de_name), "..") != 0) {
sprintf(s, "%s/%s", dir, de->de_name);
if (lstat(s, &buf) == 0) {
printf("%s\n", s);
if (S_ISDIR(buf.st_mode)) do_prdr(s);
} else {
perror(s);
}
}
}
free(s);
closedir(d);
}
main()
{
do_prdr(".");
}
Grading
- Overall Structure: 3 points.
- Details:
- Allocating sufficient storage for the directory name: 1 point
- Calling opendir(): 1 point
- Testing the return value: 1 point
- Traversing the directory using readdir(): 1 point
- Testing for "." and "..": 1 point
- Constructing the correct file name per directory entry: 1 point
- Testing the return value of lstat(): 1 point
- Printing out the name and making the recursive call: 1 point
- Calling closedir(): 1 point
- Calling free() to free the allocated storage: 1 point
- Making the initial call from main() correctly: 1 point
- Points were taken off for random egregious errors.