1 //-----------------------------------------------------------------------------
2 // Borrowed initially from
3 // https://github.com/msysgit/msys/blob/master/winsup/cygwin/scandir.cc
4 // Copyright (C) 1998-2001 Red Hat, Inc. Corinna Vinschen <corinna.vinschen@cityweb.de>
5 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // See LICENSE.txt for the text of the license.
18 //-----------------------------------------------------------------------------
28 int scandir(const char *dir
,
29 struct dirent
***namelist
,
30 int (*select
)(const struct dirent
*),
31 int (*compar
)(const struct dirent
**, const struct dirent
**)) {
33 struct dirent
*ent
, *etmp
, **nl
= NULL
, **ntmp
;
38 if (!(dirp
= opendir(dir
)))
41 while ((ent
= readdir(dirp
))) {
42 if (!select
|| select(ent
)) {
46 if (count
== allocated
) {
52 ntmp
= (struct dirent
**) realloc(nl
, allocated
* sizeof * nl
);
60 etmp
= (struct dirent
*) calloc(sizeof * ent
, sizeof(char));
83 qsort(nl
, count
, sizeof * nl
, (int (*)(const void *, const void *)) compar
);
95 int alphasort(const struct dirent
**a
, const struct dirent
**b
) {
96 return strcoll((*a
)->d_name
, (*b
)->d_name
);