Wed, 22 May 2013 10:26:22 +0200
file list is now sorted
src/scanner.c | file | annotate | diff | comparison | revisions |
--- a/src/scanner.c Tue May 21 13:19:37 2013 +0200 +++ b/src/scanner.c Wed May 22 10:26:22 2013 +0200 @@ -52,7 +52,6 @@ DIR *dirf; struct dirent *entry; struct stat statbuf; - filelist_t *listentry = list; if ((dirf = opendir(scanner.dir)) == NULL) { printf("%s", scanner.dir); @@ -65,40 +64,51 @@ /* Create new filelist entry */ filelist_t *newentry = (filelist_t*) malloc(sizeof(filelist_t)); - // TODO: don't just append - create a sorted list! - if (listentry) { - listentry->next = newentry; - } - listentry = newentry; - if (!list) { - list = listentry; - } + newentry->next = NULL; - listentry->next = NULL; + newentry->displayname_len = strlen(entry->d_name); + newentry->displayname = (char*) malloc(newentry->displayname_len+1); + memcpy(newentry->displayname, entry->d_name, newentry->displayname_len); + newentry->displayname[newentry->displayname_len] = 0; - listentry->displayname_len = strlen(entry->d_name); - listentry->displayname = (char*) malloc(listentry->displayname_len+1); - memcpy(listentry->displayname, entry->d_name, listentry->displayname_len); - listentry->displayname[listentry->displayname_len] = 0; - - listentry->st_mode = 0; + newentry->st_mode = 0; /* Construct absolute pathname string */ size_t dirnamelen = strlen(scanner.dir); - char *filename = (char*) malloc(2+dirnamelen+listentry->displayname_len); + char *filename = (char*) malloc(2+dirnamelen+newentry->displayname_len); memcpy(filename, scanner.dir, dirnamelen); filename[dirnamelen] = settings->fileSeparator; - memcpy(filename+dirnamelen+1, entry->d_name, listentry->displayname_len); - filename[1+dirnamelen+listentry->displayname_len] = 0; - listentry->filename = filename; + memcpy(filename+dirnamelen+1, entry->d_name, newentry->displayname_len); + filename[1+dirnamelen+newentry->displayname_len] = 0; + newentry->filename = filename; /* Check for subdirectory */ if (stat(filename, &statbuf) == 0) { - listentry->st_mode = statbuf.st_mode; + newentry->st_mode = statbuf.st_mode; } else { perror(" Error in stat call"); continue; } + + if (list) { + // create fake root to have a pointer on the true root + filelist_t root; + root.next = list; + filelist_t *parent = &root; + while (parent->next && + (strcasecmp(parent->next->displayname, newentry->displayname) < 0 || + (!S_ISDIR(newentry->st_mode) && S_ISDIR(parent->next->st_mode)) + ) && + (!S_ISDIR(newentry->st_mode) || S_ISDIR(parent->next->st_mode)) + ) { + parent = parent->next; + } + newentry->next = parent->next; + parent->next = newentry; + list = root.next; + } else { + list = newentry; + } } } @@ -118,10 +128,10 @@ while (filelist != NULL) { /* Scan subdirectories */ - if (!(filelist->st_mode & S_IFREG)) { + if (!S_ISREG(filelist->st_mode)) { printf("%*s\n", filelist->displayname_len+scanner.spaces, filelist->displayname); - if (settings->recursive && (filelist->st_mode & S_IFDIR)) { + if (settings->recursive && S_ISDIR(filelist->st_mode)) { scanDirectory((scanner_t) {filelist->filename, scanner.spaces+1}, settings); }