2 gcc listdir_recursive.c -o listdir_recursive -Wall -W -Wextra -ansi -pedantic */
11 /* Since listdir() uses a static variable to keep track of the call depth,
12 it is not safe to use it in a multi threading environment. If this is the
13 case, then you need to pass 'dirdepth' as an argument to listdir().
15 int listdir(const char *path
)
20 static unsigned int dirdepth
= 0;
23 /* open directory named by path, associate a directory stream
24 with it and return a pointer to it
26 if ((pdir
= opendir(path
)) == NULL
) {
31 /* get all directory entries */
32 while((pdent
= readdir(pdir
)) != NULL
) {
33 /* indent according to the depth we are */
34 for (i
= 0; i
< dirdepth
; i
++)
37 /* print current entry, or [entry] if it's a directory */
38 if (pdent
->d_type
== DT_DIR
)
39 printf("[%s]\n", pdent
->d_name
);
41 printf("%s\n", pdent
->d_name
);
43 /* Is it a directory ? If yes, list it */
44 if (pdent
->d_type
== DT_DIR
45 && strcmp(pdent
->d_name
, ".")
46 && strcmp(pdent
->d_name
, "..")) {
49 /* allocate memory for new path
50 don't forget +1 for the '\0'
52 if ((newpath
= malloc(strlen(path
) + strlen(pdent
->d_name
) + 2)) == NULL
) {
57 /* construct new path */
58 strcpy(newpath
, path
);
60 strcat(newpath
, pdent
->d_name
);
62 /* to iterate is human, to recurse, divine */
63 if (listdir(newpath
) == -1) {
79 int main(int argc
, char *argv
[])
81 /* check argument count */
83 fprintf(stderr
, "Usage: %s directory\n", argv
[0]);
87 (void)listdir(argv
[1]);