Recursively list directories in C
This is an example C program demonstrating recursively printing out
directories using readdir
. See dirent
for
the structure of a Unix directory entry.
#include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <string.h> #include <errno.h> /* "readdir" etc. are defined here. */ #include <dirent.h> /* limits.h defines "PATH_MAX". */ #include <limits.h> /* List the files in "dir_name". */ static void list_dir (const char * dir_name) { DIR * d; /* Open the directory specified by "dir_name". */ d = opendir (dir_name); /* Check it was opened. */ if (! d) { fprintf (stderr, "Cannot open directory '%s': %s\n", dir_name, strerror (errno)); exit (EXIT_FAILURE); } while (1) { struct dirent * entry; const char * d_name; /* "Readdir" gets subsequent entries from "d". */ entry = readdir (d); if (! entry) { /* There are no more entries in this directory, so break out of the while loop. */ break; } d_name = entry->d_name; /* Print the name of the file and directory. */ printf ("%s/%s\n", dir_name, d_name); #if 0 /* If you don't want to print the directories, use the following line: */ if (! (entry->d_type & DT_DIR)) { printf ("%s/%s\n", dir_name, d_name); } #endif /* 0 */ if (entry->d_type & DT_DIR) { /* Check that the directory is not "d" or d's parent. */ if (strcmp (d_name, "..") != 0 && strcmp (d_name, ".") != 0) { int path_length; char path[PATH_MAX]; path_length = snprintf (path, PATH_MAX, "%s/%s", dir_name, d_name); printf ("%s\n", path); if (path_length >= PATH_MAX) { fprintf (stderr, "Path length has got too long.\n"); exit (EXIT_FAILURE); } /* Recursively call "list_dir" with the new path. */ list_dir (path); } } } /* After going through all the entries, close the directory. */ if (closedir (d)) { fprintf (stderr, "Could not close '%s': %s\n", dir_name, strerror (errno)); exit (EXIT_FAILURE); } } int main () { list_dir ("/usr/share/games"); return 0; }
The output of the example looks like this:
/usr/share/games/. /usr/share/games/.. /usr/share/games/fortune /usr/share/games/fortune /usr/share/games/fortune/. /usr/share/games/fortune/.. /usr/share/games/fortune/fortunes /usr/share/games/fortune/fortunes.dat /usr/share/games/fortune/freebsd-tips /usr/share/games/fortune/freebsd-tips.dat /usr/share/games/fortune/gerrold.limerick /usr/share/games/fortune/gerrold.limerick.dat /usr/share/games/fortune/limerick /usr/share/games/fortune/limerick.dat /usr/share/games/fortune/murphy /usr/share/games/fortune/murphy-o /usr/share/games/fortune/murphy-o.dat /usr/share/games/fortune/murphy.dat /usr/share/games/fortune/startrek /usr/share/games/fortune/startrek.dat /usr/share/games/fortune/zippy /usr/share/games/fortune/zippy.dat
Copyright © Ben Bullock 2009-2024. All
rights reserved.
For comments, questions, and corrections, please email
Ben Bullock
(benkasminbullock@gmail.com).
/
Privacy /
Disclaimer