1 /*	$OpenBSD: scandir.c,v 1.21 2019/06/28 13:32:41 deraadt Exp $ */
2 
3 /*
4  * This is a modified verision of scandir that takes an explicit
5  * directory file descriptor as parameter.
6  */
7 
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  */
36 
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  */
43 
44 #include "gmid.h"
45 
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 
49 #include <dirent.h>
50 #include <errno.h>
51 #include <stdint.h>
52 #include <stdlib.h>
53 #include <string.h>
54 
55 #define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))
56 
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))
67 
68 int
scandir_fd(int fd,struct dirent *** namelist,int (* select)(const struct dirent *),int (* dcomp)(const struct dirent **,const struct dirent **))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;
77 
78 	if ((dirp = fdopendir(fd)) == NULL)
79 		return (-1);
80 	if (fstat(fd, &stb) == -1)
81 		goto fail;
82 
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;
95 
96 	while ((d = readdir(dirp)) != NULL) {
97 		if (select != NULL && !(*select)(d))
98 			continue;	/* just selected names */
99 
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;
106 
107 			if (fstat(fd, &stb) == -1)
108 				goto fail;
109 
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;
117 
118 			names = nnames;
119 		}
120 
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;
128 
129 		p->d_ino = d->d_ino;
130 		p->d_type = d->d_type;
131 #ifndef __DragonFly__
132 		p->d_reclen = d->d_reclen;
133 #endif
134 		memcpy(p->d_name, d->d_name, namlen + 1);
135 		names[nitems++] = p;
136 	}
137 	closedir(dirp);
138 	if (nitems && dcomp != NULL)
139 		qsort(names, nitems, sizeof(struct dirent *),
140 		    (int(*)(const void *, const void *))dcomp);
141 	*namelist = names;
142 	return (nitems);
143 
144 fail:
145 	while (nitems > 0)
146 		free(names[--nitems]);
147 	free(names);
148 	closedir(dirp);
149 	return (-1);
150 }
151 
152 int
select_non_dot(const struct dirent * d)153 select_non_dot(const struct dirent *d)
154 {
155 	return strcmp(d->d_name, ".");
156 }
157 
158 int
select_non_dotdot(const struct dirent * d)159 select_non_dotdot(const struct dirent *d)
160 {
161 	return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
162 }
163