proxmark3/client/scandir.c

96 lines
2 KiB
C
Raw Normal View History

/* scandir.cc
Copyright 1998, 1999, 2000, 2001 Red Hat, Inc.
Written by Corinna Vinschen <corinna.vinschen@cityweb.de>
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "scandir.h"
2017-07-28 04:10:12 +08:00
#ifdef _WIN32
#ifdef __cplusplus
extern "C" {
#endif
2019-03-10 07:00:59 +08:00
int scandir(const char *dir,
struct dirent ***namelist,
int (*select)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **)) {
2019-03-10 06:35:06 +08:00
DIR *dirp;
struct dirent *ent, *etmp, **nl = NULL, **ntmp;
int count = 0;
int allocated = 0;
int err_no = 0;
2019-03-10 07:00:59 +08:00
if (!(dirp = opendir(dir)))
2019-03-10 06:35:06 +08:00
return -1;
2019-03-10 07:00:59 +08:00
while ((ent = readdir(dirp))) {
if (!select || select(ent)) {
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
err_no = 0;
2019-03-10 06:35:06 +08:00
if (count == allocated) {
if (allocated == 0)
allocated = 10;
else
allocated *= 2;
2019-03-10 07:00:59 +08:00
ntmp = (struct dirent **) realloc(nl, allocated * sizeof * nl);
2019-03-10 06:35:06 +08:00
if (!ntmp) {
err_no = 1;
break;
}
nl = ntmp;
}
2019-03-10 07:00:59 +08:00
etmp = (struct dirent *) calloc(sizeof * ent, sizeof(char));
2019-03-10 06:35:06 +08:00
if (!etmp) {
err_no = 1;
break;
}
*etmp = *ent;
nl[count++] = etmp;
}
}
if (err_no != 0) {
2019-03-10 07:00:59 +08:00
closedir(dirp);
2019-03-10 06:35:06 +08:00
if (nl) {
while (count > 0) {
2019-03-10 07:00:59 +08:00
free(nl[--count]);
2019-03-10 06:35:06 +08:00
}
2019-03-10 07:00:59 +08:00
free(nl);
2019-03-10 06:35:06 +08:00
}
return -1;
}
2019-03-10 07:00:59 +08:00
closedir(dirp);
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
qsort(nl, count, sizeof * nl, (int (*)(const void *, const void *)) compar);
2019-03-10 06:35:06 +08:00
if (namelist)
*namelist = nl;
return count;
}
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
int alphasort(const struct dirent **a, const struct dirent **b) {
2019-03-10 07:00:59 +08:00
return strcoll((*a)->d_name, (*b)->d_name);
}
#ifdef __cplusplus
}
#endif
2017-07-28 04:10:12 +08:00
#endif // win32