libc: split scandir() into scandir_dirp() and proper scandir()

(cherry picked from commit cb6e97f4da)
This commit is contained in:
Konstantin Belousov 2022-08-23 07:30:40 +03:00
parent 628b801a8d
commit 066b09c2b1

View file

@ -64,22 +64,18 @@ typedef DECLARE_BLOCK(int, dcomp_block, const struct dirent **,
static int alphasort_thunk(void *thunk, const void *p1, const void *p2);
#endif
int
static int
#ifdef I_AM_SCANDIR_B
scandir_b(const char *dirname, struct dirent ***namelist, select_block select,
scandir_b_dirp(DIR *dirp, struct dirent ***namelist, select_block select,
dcomp_block dcomp)
#else
scandir(const char *dirname, struct dirent ***namelist,
scandir_dirp(DIR *dirp, struct dirent ***namelist,
int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **,
const struct dirent **))
const struct dirent **))
#endif
{
struct dirent *d, *p, **names = NULL;
size_t arraysz, numitems;
DIR *dirp;
if ((dirp = opendir(dirname)) == NULL)
return(-1);
numitems = 0;
arraysz = 32; /* initial estimate of the array size */
@ -138,6 +134,30 @@ scandir(const char *dirname, struct dirent ***namelist,
return (-1);
}
int
#ifdef I_AM_SCANDIR_B
scandir_b(const char *dirname, struct dirent ***namelist, select_block select,
dcomp_block dcomp)
#else
scandir(const char *dirname, struct dirent ***namelist,
int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **,
const struct dirent **))
#endif
{
DIR *dirp;
dirp = opendir(dirname);
if (dirp == NULL)
return (-1);
return (
#ifdef I_AM_SCANDIR_B
scandir_b_dirp
#else
scandir_dirp
#endif
(dirp, namelist, select, dcomp));
}
#ifndef I_AM_SCANDIR_B
/*
* Alphabetic order comparison routine for those who want it.