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