summaryrefslogtreecommitdiff
path: root/libc/misc/dirent/scandir.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2002-01-08 09:00:17 +0000
committerEric Andersen <andersen@codepoet.org>2002-01-08 09:00:17 +0000
commit351727917b49ddd9e5b7924e8cb7d95c126e4200 (patch)
treea11e4b70c0cb9d5d7d711d78654d71ec85449859 /libc/misc/dirent/scandir.c
parentb3f5e468f2c257dac3121984c096ade30e3adf30 (diff)
Scrub malloc handling
Diffstat (limited to 'libc/misc/dirent/scandir.c')
-rw-r--r--libc/misc/dirent/scandir.c20
1 files changed, 12 insertions, 8 deletions
diff --git a/libc/misc/dirent/scandir.c b/libc/misc/dirent/scandir.c
index a460e161f..1b4d7ec6d 100644
--- a/libc/misc/dirent/scandir.c
+++ b/libc/misc/dirent/scandir.c
@@ -30,11 +30,6 @@
#include <sys/types.h>
#include "dirstream.h"
-/*
- * FIXME: This is a simple hack version which doesn't sort the data, and
- * just passes all unsorted.
- */
-
int scandir(const char *dir, struct dirent ***namelist,
int (*selector) (const struct dirent *),
int (*compar) (const __ptr_t, const __ptr_t))
@@ -52,7 +47,10 @@ int scandir(const char *dir, struct dirent ***namelist,
while (NULL != readdir(d))
count++;
- names = malloc(sizeof (struct dirent *) * count);
+ if (!(names = malloc(sizeof (struct dirent *) * count))) {
+ closedir(d);
+ return -1;
+ }
rewinddir(d);
@@ -68,8 +66,14 @@ int scandir(const char *dir, struct dirent ***namelist,
}
result = closedir(d);
- if (pos != count)
- names = realloc(names, sizeof (struct dirent *) * pos);
+ if (pos != count) {
+ struct dirent **tmp;
+ if (!(tmp = realloc(names, sizeof (struct dirent *) * pos))) {
+ free(names);
+ return -1;
+ }
+ names = tmp;
+ }
if (compar != NULL) {
qsort(names, pos, sizeof (struct dirent *), compar);