Guest User

Untitled

a guest
Oct 16th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. // This is my code:
  2. typedef struct {
  3. char name[255];
  4. int size;
  5. int attr;
  6. } dir_ent;
  7. #define TYPE_FILTER(x) (strstr(x, ".nds") || strstr(x, ".NDS"))
  8. //...
  9. //////////////////
  10. // SECOND ATTEMPT:
  11. //////////////////
  12. DIR* dp = opendir(file_struct->path);
  13.  
  14. if(!dp)
  15. return BROWSER_FILE_NOT_FOUND;
  16.  
  17. struct stat fstat;
  18.  
  19. s32 pathLen = strlen(file_struct->path);
  20. s32 num_entries = 1, I = 0;
  21. dir_ent* dir = (dir_ent*) malloc( num_entries * sizeof(dir_ent) );
  22. struct dirent *tdir;
  23.  
  24. // Read each entry of the directory
  25.  
  26. while ((tdir=readdir(dp))!=NULL) {
  27.  
  28. u32 tdirNameLen = strlen(tdir->d_name);
  29. char filename[MAXPATHLEN];
  30. char div = '/';
  31. memset(filename, 0, MAXPATHLEN);
  32. if(MAXPATHLEN - pathLen - tdirNameLen <= 0){
  33. continue; // TOO LONG!
  34. // Print an error
  35. }
  36.  
  37. strncat(filename, file_struct->path, pathLen);
  38. strncat(filename, &div, 1);
  39. strncat(filename, tdir->d_name, tdirNameLen);
  40. stat(filename,&fstat);
  41.  
  42. // Let's see
  43. fprintf(stderr, "\nName:%s | Mode: %i\n",filename, fstat.st_mode);
  44.  
  45.  
  46. if((strcmp(filename, ".") != 0 && (fstat.st_mode & S_IFDIR)) || TYPE_FILTER(filename))
  47.  
  48. {
  49. // Make sure we have room for this one
  50. if(I == num_entries){
  51. ++num_entries;
  52. dir = (dir_ent*) realloc( dir, num_entries * sizeof(dir_ent) );
  53. }
  54.  
  55. strcpy(dir[I].name, tdir->d_name);
  56. dir[I].size = fstat.st_size;
  57. dir[I].attr = fstat.st_mode;
  58. ++I;
  59. }
  60. }
  61.  
  62. closedir(dp);
  63.  
  64.  
  65. ///////////////////
  66. // FIRST ATTEMPT:
  67. ///////////////////
  68.  
  69. DIR* dp = opendir(file_struct->path);
  70.  
  71. // file_struct->path = the path to where our files are.
  72. if(!dp)
  73. return BROWSER_FILE_NOT_FOUND;
  74.  
  75. struct stat fstat;
  76.  
  77. s32 num_entries = 1, I = 0;
  78. dir_ent* dir = (dir_ent*) malloc( num_entries * sizeof(dir_ent) );
  79. struct dirent *tdir;
  80.  
  81. // Read each entry of the directory
  82.  
  83. while ((tdir=readdir(dp))!=NULL) {
  84.  
  85. stat(tdir->d_name,&fstat);
  86.  
  87. if((strcmp(tdir->d_name, ".") != 0 && (fstat.st_mode & S_IFDIR)) || TYPE_FILTER(tdir->d_name))
  88. {
  89. // Make sure we have room for this one
  90. if(I == num_entries){
  91. ++num_entries;
  92. dir = (dir_ent*) realloc( dir, num_entries * sizeof(dir_ent) );
  93. }
  94. strcpy(dir[I].name, tdir->d_name);
  95. dir[I].size = fstat.st_size;
  96. dir[I].attr = fstat.st_mode;
  97. ++I;
  98. }
  99. }
  100.  
  101. closedir(dp);
  102.  
  103. /////////////////////////////////////////////////
  104. //
  105. // And this is the example in directory.c:
  106. //
  107.  
  108. DIR *pdir;
  109. struct dirent *pent;
  110. struct stat statbuf;
  111.  
  112. pdir=opendir("/");
  113.  
  114. if (!pdir){
  115. printf ("opendir() failure; terminating\n");
  116. goto error;
  117. }
  118.  
  119. while ((pent=readdir(pdir))!=NULL) {
  120.  
  121. stat(pent->d_name,&statbuf);
  122. if(strcmp(".", pent->d_name) == 0 || strcmp("..", pent->d_name) == 0)
  123. continue;
  124. if(S_ISDIR(statbuf.st_mode))
  125. printf("%s <dir>\n", pent->d_name);
  126. if(!(S_ISDIR(statbuf.st_mode)))
  127. printf("%s %lld\n", pent->d_name, statbuf.st_size);
  128. }
  129. closedir(pdir);
Add Comment
Please, Sign In to add comment