Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <string.h>
- #include <errno.h>
- #include <dirent.h>
- #include <limits.h>
- #include <unistd.h>
- static void
- list_dir (char * dir2find)
- {
- DIR *d;
- char *dir_name=(char *)malloc(sizeof(char)*512);
- if( *(dir2find+0) != '/')
- {
- getcwd(dir_name, 512);
- strncat(dir_name,"/",512);
- }
- strncat(dir_name,dir2find,512);
- d=opendir(dir_name);
- 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;
- entry = readdir(d);
- if(!entry)
- break;
- d_name = entry->d_name;
- fprintf (stdout,"%s/%s\n", dir_name, d_name);
- if(entry->d_type & DT_DIR)
- {
- if(*(d_name+0)!='.')
- {
- int path_length;
- char path[PATH_MAX];
- path_length = snprintf(path, PATH_MAX, "%s/%s", dir_name, d_name);
- fprintf(stdout,"Dir : %s \n",path);
- if(path_length >= PATH_MAX)
- {
- fprintf (stderr, "Path length has got too long.\n");
- exit (EXIT_FAILURE);
- }
- list_dir(path);
- }
- }
- }
- if(closedir(d))
- {
- fprintf (stderr, "Could not close '%s': %s\n",dir_name, strerror (errno));
- exit (EXIT_FAILURE);
- }
- free(dir_name);
- }
- int main ()
- {
- list_dir ("TEST");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment