1 /* $OpenBSD: scandir.c,v 1.21 2019/06/28 13:32:41 deraadt Exp $ */
4 * This is a modified verision of scandir that takes an explicit
5 * directory file descriptor as parameter.
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * Scan the directory fd calling select to make a list of selected
39 * directory entries then sort using qsort and compare routine dcomp.
40 * Returns the number of entries and a pointer to a list of pointers to
41 * struct dirent (through namelist). Returns -1 if there were any errors.
46 #include <sys/types.h>
55 #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
58 * The DIRSIZ macro is the minimum record length which will hold the directory
59 * entry. This requires the amount of space in struct dirent without the
60 * d_name field, plus enough space for the name and a terminating nul byte
61 * (dp->d_namlen + 1), rounded up to a 4 byte boundary.
65 ((sizeof(struct dirent) - sizeof(dp)->d_name) + \
66 (((dp)->d_namlen + 1 + 3) &~ 3))
69 scandir_fd(int fd, struct dirent ***namelist,
70 int (*select)(const struct dirent *),
71 int (*dcomp)(const struct dirent **, const struct dirent **))
73 struct dirent *d, *p, **names = NULL;
74 size_t /* arraysz, */ nitems = 0;
79 if ((dirp = fdopendir(fd)) == NULL)
81 if (fstat(fd, &stb) == -1)
85 * estimate the array size by taking the size of the directory file
86 * and dividing it by a multiple of the minimum size entry.
88 arraysz = MAXIMUM(stb.st_size / 24, 16);
89 if (arraysz > SIZE_MAX / sizeof(struct dirent *)) {
93 names = calloc(arraysz, sizeof(struct dirent *));
97 while ((d = readdir(dirp)) != NULL) {
98 if (select != NULL && !(*select)(d))
99 continue; /* just selected names */
102 * Check to make sure the array has space left and
103 * realloc the maximum size.
105 if (nitems >= arraysz) {
106 struct dirent **nnames;
108 if (fstat(fd, &stb) == -1)
112 if (SIZE_MAX / sizeof(struct dirent *) < arraysz)
114 nnames = reallocarray(names,
115 arraysz, sizeof(struct dirent *));
123 * Make a minimum size copy of the data
125 p = malloc(DIRSIZ(d));
130 p->d_type = d->d_type;
131 p->d_reclen = d->d_reclen;
132 p->d_namlen = d->d_namlen;
133 bcopy(d->d_name, p->d_name, p->d_namlen + 1);
137 if (nitems && dcomp != NULL)
138 qsort(names, nitems, sizeof(struct dirent *),
139 (int(*)(const void *, const void *))dcomp);
145 free(names[--nitems]);
152 select_non_dot(const struct dirent *d)
154 return strcmp(d->d_name, ".");
158 select_non_dotdot(const struct dirent *d)
160 return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");