3 * gcc listdir_recursive.c -o listdir_recursive -Wall -W -Wextra -ansi -pedantic
13 * Since listdir() uses a static variable to keep track of the call depth,
14 * it is not safe to use it in a multi threading environment. If this is the
15 * case, then you need to pass 'dirdepth' as an argument to listdir().
17 int listdir(const char *path
)
22 static unsigned int dirdepth
= 0;
26 * Open directory named by path, associate a directory stream
27 * with it and return a pointer to it
29 if ((pdir
= opendir(path
)) == NULL
) {
34 /* Get all directory entries */
35 while((pdent
= readdir(pdir
)) != NULL
) {
36 /* Indent according to the depth we are */
37 for (i
= 0; i
< dirdepth
; i
++)
40 /* Print current entry, or [entry] if it's a directory */
41 if (pdent
->d_type
== DT_DIR
)
42 printf("[%s]\n", pdent
->d_name
);
44 printf("%s\n", pdent
->d_name
);
46 /* Is it a directory ? If yes, list it */
47 if (pdent
->d_type
== DT_DIR
48 && strcmp(pdent
->d_name
, ".")
49 && strcmp(pdent
->d_name
, "..")) {
52 /* Allocate memory for new path (don't forget +1 for the '\0') */
53 if ((newpath
= malloc(strlen(path
) + strlen(pdent
->d_name
) + 2)) == NULL
) {
58 /* Construct new path */
59 strcpy(newpath
, path
);
61 strcat(newpath
, pdent
->d_name
);
63 /* To iterate is human, to recurse, divine */
64 if (listdir(newpath
) == -1) {
80 int main(int argc
, char *argv
[])
82 /* Check argument count */
84 fprintf(stderr
, "Usage: %s directory\n", argv
[0]);
88 (void)listdir(argv
[1]);