Changeset View
Changeset View
Standalone View
Standalone View
lib/libc/gen/scandir.c
Show First 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | |||||
#define SELECT(x) select(x) | #define SELECT(x) select(x) | ||||
#endif | #endif | ||||
#ifdef I_AM_SCANDIR_B | #ifdef I_AM_SCANDIR_B | ||||
typedef DECLARE_BLOCK(int, select_block, const struct dirent *); | typedef DECLARE_BLOCK(int, select_block, const struct dirent *); | ||||
typedef DECLARE_BLOCK(int, dcomp_block, const struct dirent **, | typedef DECLARE_BLOCK(int, dcomp_block, const struct dirent **, | ||||
const struct dirent **); | const struct dirent **); | ||||
#else | #else | ||||
static int scandir_thunk_cmp(void *thunk, const void *p1, const void *p2); | static int scandir_thunk_cmp(const void *p1, const void *p2, void *thunk); | ||||
#endif | #endif | ||||
static int | static int | ||||
#ifdef I_AM_SCANDIR_B | #ifdef I_AM_SCANDIR_B | ||||
scandir_b_dirp(DIR *dirp, struct dirent ***namelist, select_block select, | scandir_b_dirp(DIR *dirp, struct dirent ***namelist, select_block select, | ||||
dcomp_block dcomp) | dcomp_block dcomp) | ||||
#else | #else | ||||
scandir_dirp(DIR *dirp, struct dirent ***namelist, | scandir_dirp(DIR *dirp, struct dirent ***namelist, | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | while ((d = readdir(dirp)) != NULL) { | ||||
names[numitems++] = p; | names[numitems++] = p; | ||||
} | } | ||||
closedir(dirp); | closedir(dirp); | ||||
if (numitems && dcomp != NULL) | if (numitems && dcomp != NULL) | ||||
#ifdef I_AM_SCANDIR_B | #ifdef I_AM_SCANDIR_B | ||||
qsort_b(names, numitems, sizeof(struct dirent *), (void*)dcomp); | qsort_b(names, numitems, sizeof(struct dirent *), (void*)dcomp); | ||||
#else | #else | ||||
qsort_r(names, numitems, sizeof(struct dirent *), | qsort_r(names, numitems, sizeof(struct dirent *), | ||||
&dcomp, scandir_thunk_cmp); | scandir_thunk_cmp, &dcomp); | ||||
#endif | #endif | ||||
*namelist = names; | *namelist = names; | ||||
return (numitems); | return (numitems); | ||||
fail: | fail: | ||||
while (numitems > 0) | while (numitems > 0) | ||||
free(names[--numitems]); | free(names[--numitems]); | ||||
free(names); | free(names); | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | |||||
int | int | ||||
versionsort(const struct dirent **d1, const struct dirent **d2) | versionsort(const struct dirent **d1, const struct dirent **d2) | ||||
{ | { | ||||
return (strverscmp((*d1)->d_name, (*d2)->d_name)); | return (strverscmp((*d1)->d_name, (*d2)->d_name)); | ||||
} | } | ||||
static int | static int | ||||
scandir_thunk_cmp(void *thunk, const void *p1, const void *p2) | scandir_thunk_cmp(const void *p1, const void *p2, void *thunk) | ||||
{ | { | ||||
int (*dc)(const struct dirent **, const struct dirent **); | int (*dc)(const struct dirent **, const struct dirent **); | ||||
dc = *(int (**)(const struct dirent **, const struct dirent **))thunk; | dc = *(int (**)(const struct dirent **, const struct dirent **))thunk; | ||||
return (dc((const struct dirent **)p1, (const struct dirent **)p2)); | return (dc((const struct dirent **)p1, (const struct dirent **)p2)); | ||||
} | } | ||||
#endif | #endif |