proxmark3/client/src/scandir.c

103 lines
2.7 KiB
C
Raw Normal View History

2022-01-07 08:58:03 +08:00
//-----------------------------------------------------------------------------
// Borrowed initially from
// https://github.com/msysgit/msys/blob/master/winsup/cygwin/scandir.cc
// Copyright (C) 1998-2001 Red Hat, Inc. Corinna Vinschen <corinna.vinschen@cityweb.de>
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// See LICENSE.txt for the text of the license.
//-----------------------------------------------------------------------------
#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