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

The new helper scandir_dirp() takes DIR *, i.e. a pre-opened directory,
instead of the directory name.

Reviewed by:	emaste, imp, kevans, markj, Aymeric Wibo <obiwac@gmail.com>
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D36301
This commit is contained in:
Konstantin Belousov 2022-08-23 07:30:40 +03:00
parent ea448a0a43
commit cb6e97f4da

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.