OSDN Git Service

A patch from Jon Nelson <jnelson@securepipe.com> to make scandir
authorEric Andersen <andersen@codepoet.org>
Tue, 8 May 2001 18:06:08 +0000 (18:06 -0000)
committerEric Andersen <andersen@codepoet.org>
Tue, 8 May 2001 18:06:08 +0000 (18:06 -0000)
sort things as it is supposed to do.

libc/misc/dirent/scandir.c

index 2b07e29..31ebce5 100644 (file)
@@ -53,10 +53,7 @@ int scandir(const char *dir, struct dirent ***namelist,
 
     names = malloc(sizeof (struct dirent *) * count);
 
-    closedir(d);
-    d = opendir(dir);
-    if (NULL == d)
-        return -1;
+    rewinddir(d);
 
     while (NULL != (current = readdir(d))) {
         if (NULL == selector || selector(current)) {
@@ -73,6 +70,10 @@ int scandir(const char *dir, struct dirent ***namelist,
     if (pos != count)
         names = realloc(names, sizeof (struct dirent *) * pos);
 
+    if (compar != NULL) {
+       qsort(names, pos, sizeof (struct dirent *), compar);
+    }
+
     *namelist = names;
 
     return pos;