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>
- char **list_dir(char * dir2find)
- {
- DIR *d;
- char *dir_name=(char *)malloc(sizeof(char)*512);
- char **list=(char **)malloc(1*sizeof(char *));
- short counter=0;
- if( *(dir2find+0) != '/')
- {
- getcwd(dir_name, 512);
- strncat(dir_name,"/",512);
- strncat(dir_name,dir2find,512);
- } else strcpy(dir_name,dir2find);
- HELL:
- 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)!='.')
- {
- char *tmp=dir_name;
- snprintf(tmp, 511, "%s/%s", dir_name, d_name);
- list[counter]=tmp;
- counter++;
- list=realloc(list,(counter+1)*sizeof(char *));
- goto HELL;
- }
- } else {
- char *tmp=dir_name;
- sprintf (tmp,"%s/%s\n", dir_name, d_name);
- list[counter]=tmp;
- counter++;
- list=realloc(list,(counter+1)*sizeof(char *));
- }
- }
- if(closedir(d))
- {
- fprintf (stderr, "Could not close '%s': %s\n",dir_name, strerror (errno));
- exit (EXIT_FAILURE);
- }
- puts("exit func");
- free(dir_name);
- return list;
- }
- int
- main()
- {
- char **test= (char **)malloc(1*sizeof(char *));
- test=list_dir ("TEST");
- puts("ok");
- fprintf(stdout,"%s \n",test[0]);
- free(test);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment