X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=filematch.c;h=758575f2e8329d4369e8be1449678ff9a95837bf;hp=a4f670f9732b5b906a9e227be050bb6712d1765a;hb=e572ffa56f0883c1ac7c9cca37da359ddbac551c;hpb=5f287be4a74858793ea36ca8144e2b1720d99d9f diff --git a/filematch.c b/filematch.c index a4f670f9..758575f2 100644 --- a/filematch.c +++ b/filematch.c @@ -85,6 +85,9 @@ stringlist_t *stringlistsort(stringlist_t *start) { int notdone; stringlist_t *current, *previous, *temp2, *temp3, *temp4; + // exit early if there's nothing to sort + if (start == NULL || start->next == NULL) + return start; notdone = 1; while (notdone) { @@ -122,33 +125,29 @@ stringlist_t *listdirectory(const char *path) { char pattern[4096], *c; struct _finddata_t n_file; - long hFile; + long hFile; stringlist_t *start, *current; strlcpy (pattern, path, sizeof (pattern)); - strlcat (pattern, "\\*", sizeof (pattern)); + strlcat (pattern, "*", sizeof (pattern)); // ask for the directory listing handle hFile = _findfirst(pattern, &n_file); - if(hFile != -1) - { - // start a new chain with the the first name - start = current = stringlistappend(NULL, n_file.name); - // iterate through the directory - while (_findnext(hFile, &n_file) == 0) - current = stringlistappend(current, n_file.name); - _findclose(hFile); + if(hFile == -1) + return NULL; + // start a new chain with the the first name + start = current = stringlistappend(NULL, n_file.name); + // iterate through the directory + while (_findnext(hFile, &n_file) == 0) + current = stringlistappend(current, n_file.name); + _findclose(hFile); - // convert names to lowercase because windows does not care, but pattern matching code often does - for (current = start;current;current = current->next) - for (c = current->text;*c;c++) - if (*c >= 'A' && *c <= 'Z') - *c += 'a' - 'A'; + // convert names to lowercase because windows does not care, but pattern matching code often does + for (current = start;current;current = current->next) + for (c = current->text;*c;c++) + if (*c >= 'A' && *c <= 'Z') + *c += 'a' - 'A'; - // sort the list alphanumerically - start = stringlistsort(start); - return start; - } - else - return NULL; + // sort the list alphanumerically + return stringlistsort(start); } #else #include @@ -160,15 +159,16 @@ stringlist_t *listdirectory(const char *path) dir = opendir(path); if (!dir) return NULL; - ent = readdir(dir); - if (!ent) + start = current = NULL; + while ((ent = readdir(dir))) { - closedir(dir); - return NULL; + if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, "..")) + { + current = stringlistappend(current, ent->d_name); + if (!start) + start = current; + } } - start = current = stringlistappend(NULL, ent->d_name); - while((ent = readdir(dir))) - current = stringlistappend(current, ent->d_name); closedir(dir); // sort the list alphanumerically return stringlistsort(start);