Advertisement
93aef0ce4dd141ece6f5

Recursive Directory Listing

Jan 10th, 2016
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.79 KB | None | 0 0
  1. /*
  2.  * Downloaded from Lemoda.net [http://www.lemoda.net/c/recursive-directory/rd.c]
  3.  * Author: Ben Bullock
  4.  * Copyright (C) Ben Bullock 2009-2015 All Rights
  5.  * Disclaimer: http://www.lemoda.net/disclaimer.html
  6.  */
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <sys/types.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. /* "readdir" etc. are defined here. */
  14. #include <dirent.h>
  15. /* limits.h defines "PATH_MAX". */
  16. #include <limits.h>
  17.  
  18. /* List the files in "dir_name". */
  19. /*
  20.  * static in functions mean that
  21.  * only the functions inside the
  22.  * same file can see and use it
  23. */
  24. static void list_dir (const char * dir_name)
  25. {
  26.     /*
  27.      * DIR * is similar to FILE *
  28.      * except it is a directory stream
  29.      * instead of a file stream
  30.      */
  31.     DIR * d;
  32.  
  33.     /* Open the directory specified by "dir_name". */
  34.  
  35.     /*
  36.      * dir_name is the string of
  37.      * the base directory we want
  38.      * to access and it's a
  39.      * parameter passed from main
  40.      */
  41.     d = opendir (dir_name);
  42.  
  43.     /* Check it was opened. */
  44.     if (! d) {
  45.         fprintf (stderr, "Cannot open directory '%s': %s\n",
  46.                  dir_name, strerror (errno));
  47.         exit (EXIT_FAILURE);
  48.     }
  49.     while (1) {
  50.         /*
  51.          * here is our dirent struct
  52.          * given the variable name "entry"
  53.          * and it holds the information of
  54.          * the file/folder it has queried
  55.          * such as type (file, directory, etc.)
  56.          * and the name
  57.          */
  58.         struct dirent * entry;
  59.  
  60.         /*
  61.          * d_name is a variable to store
  62.          * the name of the file for some
  63.          * convenience otherwise we would
  64.          * constantly need to refer to the
  65.          * struct to access the name member
  66.          */
  67.         const char * d_name;
  68.  
  69.         /* "Readdir" gets subsequent entries from "d". */
  70.         entry = readdir (d);
  71.         if (! entry) {
  72.             /* There are no more entries in this directory, so break
  73.                out of the while loop. */
  74.             break;
  75.         }
  76.         d_name = entry->d_name;
  77.         /* Print the name of the file and directory. */
  78.     printf ("%s/%s\n", dir_name, d_name);
  79.  
  80. #if 0
  81.     /* If you don't want to print the directories, use the
  82.        following line: */
  83.  
  84.         if (! (entry->d_type & DT_DIR)) {
  85.         printf ("%s/%s\n", dir_name, d_name);
  86.     }
  87.  
  88. #endif /* 0 */
  89.  
  90.         /*
  91.          * check if entry is a directory
  92.          */
  93.         if (entry->d_type & DT_DIR) {
  94.  
  95.             /* Check that the directory is not "d" or d's parent. */
  96.             /*
  97.              * we do not want these directories
  98.              */
  99.             if (strcmp (d_name, "..") != 0 &&
  100.                 strcmp (d_name, ".") != 0) {
  101.                 int path_length;
  102.                 char path[PATH_MAX];
  103.  
  104.                 /*
  105.                  * if it is a directory and not either
  106.                  * of the above, we retrieve it for
  107.                  * a recursive call to access its
  108.                  * contents and repeat the process
  109.                  */
  110.                 path_length = snprintf (path, PATH_MAX,
  111.                                         "%s/%s", dir_name, d_name);
  112.                 printf ("%s\n", path);
  113.  
  114.                 /*
  115.                  * error checking to see if path is
  116.                  * too large to fit
  117.                  */
  118.                 if (path_length >= PATH_MAX) {
  119.                     fprintf (stderr, "Path length has got too long.\n");
  120.                     exit (EXIT_FAILURE);
  121.                 }
  122.                 /* Recursively call "list_dir" with the new path. */
  123.                 list_dir (path);
  124.             }
  125.     }
  126.     }
  127.     /* After going through all the entries, close the directory. */
  128.     /*
  129.      * similar to closing the
  130.      * file stream with FILE *
  131.      * with error checking
  132.      */
  133.     if (closedir (d)) {
  134.         fprintf (stderr, "Could not close '%s': %s\n",
  135.                  dir_name, strerror (errno));
  136.         exit (EXIT_FAILURE);
  137.     }
  138. }
  139.  
  140. int main ()
  141. {
  142.     /*
  143.      * call a recursive function with
  144.      * the base directory
  145.      */
  146.     list_dir ("/usr/share/games");
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement