kolibrios/contrib/sdk/sources/newlib/libc/dirent/scandir.c
turbocat 2287b1e29b newlibc:
Added new functions:
- dir.h: mkdir, rmdir, chdir
- dirent.h:  alphasort, opendir, closedir, readdir, rewinddir, seekdir, telldir, scandir

git-svn-id: svn://kolibrios.org@8759 a494cfbc-eb01-0410-851d-a64ba20cac60
2021-06-02 00:58:56 +00:00

47 lines
1017 B
C

#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stddef.h>
#define SIZE_MAX 256
int scandir(const char *path, struct dirent ***res,
int (*sel)(const struct dirent *),
int (*cmp)(const struct dirent **, const struct dirent **))
{
DIR *d = opendir(path);
struct dirent *de, **names=0, **tmp;
size_t cnt=0, len=0;
int old_errno = errno;
if (!d) return -1;
while ((errno=0), (de = readdir(d))) {
if (sel && !sel(de)) continue;
if (cnt >= len) {
len = 2*len+1;
if (len > SIZE_MAX/sizeof *names) break;
tmp = realloc(names, len * sizeof *names);
if (!tmp) break;
names = tmp;
}
names[cnt] = malloc(sizeof(struct dirent));
if (!names[cnt]) break;
memcpy(names[cnt++], de, sizeof(struct dirent));
}
closedir(d);
if (errno) {
if (names) while (cnt-->0) free(names[cnt]);
free(names);
return -1;
}
errno = old_errno;
if (cmp) qsort(names, cnt, sizeof *names, (int (*)(const void *, const void *))cmp);
*res = names;
return cnt;
}