Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // by Cooler_
- #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,char *buffer)
- {
- DIR *d;
- char *dir_name=(char *)malloc(PATH_MAX*sizeof(char));
- char *tmp;
- if( dir2find[0] != '/')
- {
- getcwd(dir_name, PATH_MAX);
- strncat(dir_name,"/",PATH_MAX);
- }
- strncat(dir_name,dir2find,PATH_MAX);
- 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;
- tmp=(char *)malloc(PATH_MAX*sizeof(char));
- entry=readdir(d);
- if(!entry)
- break;
- d_name = entry->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);
- if(path_length >= PATH_MAX)
- {
- fprintf (stderr, "Path length has got too long.\n");
- exit (EXIT_FAILURE);
- }
- list_dir(path,buffer);
- }
- } else {
- sprintf(tmp,"%s/%s\n",dir_name,d_name);
- strncat(buffer,tmp,PATH_MAX);
- }
- free(tmp);
- }
- if(closedir(d))
- {
- fprintf (stderr, "Could not close '%s': %s\n",dir_name, strerror (errno));
- exit (EXIT_FAILURE);
- }
- if(dir_name)
- free(dir_name);
- }
- int main ()
- {
- char *buffer=malloc(1024*sizeof(char));
- list_dir ("TEST",buffer);
- puts("teste");
- puts(buffer);
- free(buffer);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment