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?