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) \
65 ((sizeof(struct dirent) - sizeof(dp)->d_name) + \
66 (((dp)->d_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, */ nitems = 0;
75 long arraysz;
76 struct stat stb;
77 DIR *dirp;
79 if ((dirp = fdopendir(fd)) == NULL)
80 return (-1);
81 if (fstat(fd, &stb) == -1)
82 goto fail;
84 /*
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.
87 */
88 arraysz = MAXIMUM(stb.st_size / 24, 16);
89 if (arraysz > SIZE_MAX / sizeof(struct dirent *)) {
90 errno = ENOMEM;
91 goto fail;
92 }
93 names = calloc(arraysz, sizeof(struct dirent *));
94 if (names == NULL)
95 goto fail;
97 while ((d = readdir(dirp)) != NULL) {
98 if (select != NULL && !(*select)(d))
99 continue; /* just selected names */
101 /*
102 * Check to make sure the array has space left and
103 * realloc the maximum size.
104 */
105 if (nitems >= arraysz) {
106 struct dirent **nnames;
108 if (fstat(fd, &stb) == -1)
109 goto fail;
111 arraysz *= 2;
112 if (SIZE_MAX / sizeof(struct dirent *) < arraysz)
113 goto fail;
114 nnames = reallocarray(names,
115 arraysz, sizeof(struct dirent *));
116 if (nnames == NULL)
117 goto fail;
119 names = nnames;
122 /*
123 * Make a minimum size copy of the data
124 */
125 p = malloc(DIRSIZ(d));
126 if (p == NULL)
127 goto fail;
129 p->d_ino = d->d_ino;
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);
134 names[nitems++] = p;
136 closedir(dirp);
137 if (nitems && dcomp != NULL)
138 qsort(names, nitems, sizeof(struct dirent *),
139 (int(*)(const void *, const void *))dcomp);
140 *namelist = names;
141 return (nitems);
143 fail:
144 while (nitems > 0)
145 free(names[--nitems]);
146 free(names);
147 closedir(dirp);
148 return (-1);
151 int
152 select_non_dot(const struct dirent *d)
154 return strcmp(d->d_name, ".");
157 int
158 select_non_dotdot(const struct dirent *d)
160 return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");