0

Here is a piece of C code that seems very peculiar. For some strange reason, the DT_DIR should ignore folders but it doesn't:

char** getFiles(char* pathToScan, int size)
{
    DIR *d;
    struct dirent *dir;
    char** filesName;
    int i=0;
    filesName = malloc(sizeof(char*)*size);
    d = opendir(pathToScan);

    if (d)
    {
        while ((dir = readdir(d)) != NULL)
        {
            if (dir->d_type != DT_DIR)
            {
                *(filesName + i-2) = malloc(sizeof(char)*strlen(pathToScan) + sizeof(char)*strlen(dir->d_name));
                strcpy(*(filesName + i-2), pathToScan);
                strcat(*(filesName + i-2), "\\");
                strcat(*(filesName + i-2), dir->d_name);
            }
            i++;
        }
        closedir(d);
    }
    return filesName;
    }
  • If I add to the file that I scan a folder, the program crashes.
  • If there are no folders in the folder that I scan, it works perfectly.

My first thought was that there is a problem with the DT_DIR (read about DT_DIR), but I don't know what to do about it.

What is going on? Why is it that DT_DIR doesn't ignore folders?

Jongware
  • 22,200
  • 8
  • 54
  • 100
theOtherOne
  • 21
  • 1
  • 4

1 Answers1

1

I understand size as a limit of the number of file to returns. asprintf seems more simple

#include <stdio.h>                                                             
#include <stdlib.h>                                                            
#include <string.h>                                                            
#include <dirent.h>                                                            

static char** getFiles(char* pathToScan, int *size)                            
{                                                                              
    DIR *d;                                                                    
    struct dirent *dir;                                                        
    char** filesName;                                                          
    char *f;                                                                   
    int i = 0;                                                                 
    filesName = malloc(sizeof(char*) * *size);                                 
    d = opendir(pathToScan);                                                   

    if (d)                                                                     
    {                                                                          
        while ((dir = readdir(d)) != NULL && i < *size)                        
        {                                                                      
            if (dir->d_type != DT_DIR)                                         
            {                                                                  
                char file[PATH_MAX];                                           
                snprintf(file, sizeof(file), "%s\\%s",                         
                         pathToScan, dir->d_name);                             
                filesName[i++] = strdup(file);                   
                filesName[i++] = f;                                            
            }                                                                  
        }                                                                      
        closedir(d);                                                           
    }                                                                          
    *size = i;                                                                 
    return filesName;                                                          
}

example of use:

int main(void) {                                                               
   int n = 10;                                                                 
   char **files = getFiles("/home/foo", &n);                                 
   for (int i = 0; i < n; i++) {                                               
       printf("%s\n", files[i]);                                               
   }                                                                           
}  
Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • ...and if there are more than `*size` files in the directory? – EOF May 30 '15 at 10:31
  • @Ôrel thnx for your time. but it is crashes. what is asprintf? my compiler doesn't find it. (and size is the number of files :)) – theOtherOne May 30 '15 at 10:43
  • This function is GNU extensions, not in C or POSIX. Add `#define _GNU_SOURCE` at the top of your file or malloc a char* and use `snprintf` – Ôrel May 30 '15 at 10:47
  • I remove asprintf. What do you really want to do ? – Ôrel May 30 '15 at 10:54
  • @Ôrel thank you. i am very appreciating you. but it doesn't ignore folders and that my purpose after all.. – theOtherOne May 30 '15 at 10:55
  • Have you add `#define _BSD_SOURCE` looks like `DT_DIR` is not defined. Add warning when you compile to be sure. – Ôrel May 30 '15 at 11:01