libc: add scandirat(3)

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 06:30:06 +03:00
parent cb6e97f4da
commit 9fb8e8eede
3 changed files with 27 additions and 0 deletions

View file

@ -131,6 +131,11 @@ int scandir_b(const char *, struct dirent ***,
int (^)(const struct dirent **, const struct dirent **));
#endif
#endif
#if __BSD_VISIBLE
int scandirat(int, const char *, struct dirent ***,
int (*)(const struct dirent *), int (*)(const struct dirent **,
const struct dirent **));
#endif
#if __XSI_VISIBLE
void seekdir(DIR *, long);
long telldir(DIR *);

View file

@ -439,6 +439,7 @@ FBSD_1.7 {
posix_spawn_file_actions_addchdir_np;
posix_spawn_file_actions_addclosefrom_np;
posix_spawn_file_actions_addfchdir_np;
scandirat;
sched_getaffinity;
sched_setaffinity;
sched_getcpu;

View file

@ -42,8 +42,10 @@ __FBSDID("$FreeBSD$");
#include "namespace.h"
#include <dirent.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "un-namespace.h"
#ifdef I_AM_SCANDIR_B
@ -159,6 +161,25 @@ scandir(const char *dirname, struct dirent ***namelist,
}
#ifndef I_AM_SCANDIR_B
int
scandirat(int dirfd, const char *dirname, struct dirent ***namelist,
int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **,
const struct dirent **))
{
DIR *dirp;
int fd;
fd = _openat(dirfd, dirname, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
if (fd == -1)
return (-1);
dirp = fdopendir(fd);
if (dirp == NULL) {
_close(fd);
return (-1);
}
return (scandir_dirp(dirp, namelist, select, dcomp));
}
/*
* Alphabetic order comparison routine for those who want it.
* POSIX 2008 requires that alphasort() uses strcoll().