textual
[RRG-proxmark3.git] / client / src / scandir.c
blobb06befe8e2459633c41d6e11e3bfad28893a678c
1 /* scandir.cc
3 Copyright 1998, 1999, 2000, 2001 Red Hat, Inc.
5 Written by Corinna Vinschen <corinna.vinschen@cityweb.de>
7 This file is part of Cygwin.
9 This software is a copyrighted work licensed under the terms of the
10 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
11 details. */
13 #include "scandir.h"
15 #ifdef _WIN32
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 int scandir(const char *dir,
22 struct dirent ***namelist,
23 int (*select)(const struct dirent *),
24 int (*compar)(const struct dirent **, const struct dirent **)) {
25 DIR *dirp;
26 struct dirent *ent, *etmp, **nl = NULL, **ntmp;
27 int count = 0;
28 int allocated = 0;
29 int err_no = 0;
31 if (!(dirp = opendir(dir)))
32 return -1;
34 while ((ent = readdir(dirp))) {
35 if (!select || select(ent)) {
37 err_no = 0;
39 if (count == allocated) {
40 if (allocated == 0)
41 allocated = 10;
42 else
43 allocated *= 2;
45 ntmp = (struct dirent **) realloc(nl, allocated * sizeof * nl);
46 if (!ntmp) {
47 err_no = 1;
48 break;
50 nl = ntmp;
53 etmp = (struct dirent *) calloc(sizeof * ent, sizeof(char));
54 if (!etmp) {
55 err_no = 1;
56 break;
58 *etmp = *ent;
59 nl[count++] = etmp;
63 if (err_no != 0) {
64 closedir(dirp);
65 if (nl) {
66 while (count > 0) {
67 free(nl[--count]);
69 free(nl);
71 return -1;
74 closedir(dirp);
76 qsort(nl, count, sizeof * nl, (int (*)(const void *, const void *)) compar);
77 if (namelist)
78 *namelist = nl;
79 return count;
81 #ifdef __cplusplus
83 #endif
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 int alphasort(const struct dirent **a, const struct dirent **b) {
89 return strcoll((*a)->d_name, (*b)->d_name);
91 #ifdef __cplusplus
93 #endif
95 #endif // win32