Blob


1 /* $OpenBSD: scandir.c,v 1.21 2019/06/28 13:32:41 deraadt Exp $ */
3 /*
4 * This is a modified verision of scandir that takes an explicit
5 * directory file descriptor as parameter.
6 */
8 /*
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 */
37 /*
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.
42 */
44 #include "gmid.h"
46 #include <sys/types.h>
47 #include <sys/stat.h>
49 #include <dirent.h>
50 #include <errno.h>
51 #include <stdint.h>
52 #include <stdlib.h>
53 #include <string.h>
55 #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
57 /*
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.
62 */
63 #undef DIRSIZ
64 #define DIRSIZ(dp, namlen) \
65 ((sizeof(struct dirent) - sizeof(dp)->d_name) + \
66 ((namlen + 1 + 3) &~ 3))
68 int
69 scandir_fd(int fd, struct dirent ***namelist,
70 int (*select)(const struct dirent *),
71 int (*dcomp)(const struct dirent **, const struct dirent **))
72 {
73 struct dirent *d, *p, **names = NULL;
74 size_t arraysz, namlen, nitems = 0;
75 struct stat stb;
76 DIR *dirp;
78 if ((dirp = fdopendir(fd)) == NULL)
79 return (-1);
80 if (fstat(fd, &stb) == -1)
81 goto fail;
83 /*
84 * estimate the array size by taking the size of the directory file
85 * and dividing it by a multiple of the minimum size entry.
86 */
87 arraysz = MAXIMUM(stb.st_size / 24, 16);
88 if (arraysz > SIZE_MAX / sizeof(struct dirent *)) {
89 errno = ENOMEM;
90 goto fail;
91 }
92 names = calloc(arraysz, sizeof(struct dirent *));
93 if (names == NULL)
94 goto fail;
96 while ((d = readdir(dirp)) != NULL) {
97 if (select != NULL && !(*select)(d))
98 continue; /* just selected names */
100 /*
101 * Check to make sure the array has space left and
102 * realloc the maximum size.
103 */
104 if (nitems >= arraysz) {
105 struct dirent **nnames;
107 if (fstat(fd, &stb) == -1)
108 goto fail;
110 arraysz *= 2;
111 if (SIZE_MAX / sizeof(struct dirent *) < arraysz)
112 goto fail;
113 nnames = reallocarray(names,
114 arraysz, sizeof(struct dirent *));
115 if (nnames == NULL)
116 goto fail;
118 names = nnames;
121 /*
122 * Make a minimum size copy of the data
123 */
124 namlen = strlen(d->d_name);
125 p = malloc(DIRSIZ(d, namlen));
126 if (p == NULL)
127 goto fail;
129 p->d_ino = d->d_ino;
130 p->d_type = d->d_type;
131 memcpy(p->d_name, d->d_name, namlen + 1);
132 names[nitems++] = p;
134 closedir(dirp);
135 if (nitems && dcomp != NULL)
136 qsort(names, nitems, sizeof(struct dirent *),
137 (int(*)(const void *, const void *))dcomp);
138 *namelist = names;
139 return (nitems);
141 fail:
142 while (nitems > 0)
143 free(names[--nitems]);
144 free(names);
145 closedir(dirp);
146 return (-1);
149 int
150 select_non_dot(const struct dirent *d)
152 return strcmp(d->d_name, ".");
155 int
156 select_non_dotdot(const struct dirent *d)
158 return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");