dirshow.c 918 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <error.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <dirent.h>
  7. int
  8. main (int argc, char *argv[])
  9. {
  10. DIR *dp;
  11. struct dirent *ep;
  12. if(argc==1){
  13. perror("Usage program base/path");
  14. exit(1);
  15. }
  16. dp = opendir (argv[1]);
  17. if (dp != NULL)
  18. {
  19. while (ep = readdir (dp)) {
  20. puts (ep->d_name);
  21. if(strcmp(ep->d_name, ".")==0 || strcmp(ep->d_name, "..")==0)
  22. continue;
  23. if(ep->d_type==DT_DIR) {
  24. char *subdir = malloc(strlen(argv[1])+strlen("/")+strlen(ep->d_name)+1);
  25. strcpy(subdir, argv[1]);
  26. strcat(subdir, "/");
  27. strcat(subdir,ep->d_name);
  28. char *newargv[] = {NULL, subdir};
  29. main(2,newargv);
  30. }
  31. }
  32. (void) closedir (dp);
  33. }
  34. else
  35. perror ("Couldn't open the directory");
  36. return 0;
  37. }