I need to create a file/dir list sorted alphabetically and with directories first.
That was pretty easy with scandir() but it doesn't work with fat and ntfs drives
I thought about using stat() but with scandir() I don't get the full path.
Any ideas how to proceed?
BR, Thomas Fogh
Code:
int file_select(const struct dirent* entry)
{
if ( (strcmp(entry->d_name, ".") == 0) ||
(strcmp(entry->d_name, "..") == 0) ||
((entry->d_type != DT_DIR) &&
(entry->d_type != DT_REG)) )
return 0;
else
return 1;
}
int file_sort(const struct dirent** d1, const struct dirent** d2)
{
if ((*d1)->d_type == DT_DIR && (*d2)->d_type == DT_REG)
return -1;
else if ((*d1)->d_type == DT_REG && (*d2)->d_type == DT_DIR)
return 1;
else return strcmp((*d1)->d_name, (*d2)->d_name);
}
void print_dirs(char* dir)
{
struct dirent **files;
int count, i;
count = scandir(dir, &files, file_select, file_sort);
for (i = 0; i < count; i++) {
if (files[i]->d_type == DT_DIR) {
char* temp_name = malloc(strlen(dir)+strlen(files[i]->d_name)+2);
strcpy(temp_name, dir);
strcat(temp_name, "/");
strcat(temp_name, files[i]->d_name);
printf("[%s]\n", temp_name);
print_dirs(temp_name);
free(temp_name);
} else if (files[i]->d_type == DT_REG) {
printf("%s\n", files[i]->d_name);
}
free(files[i]);
}
}
int main(int argc, char** argv)
{
print_dirs("/svn");
}