1 /*	$OpenBSD: scandir.c,v 1.21 2019/06/28 13:32:41 deraadt Exp $ */
2 /*
3  * Copyright (c) 1983, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * Scan the directory dirname calling select to make a list of selected
33  * directory entries then sort using qsort and compare routine dcomp.
34  * Returns the number of entries and a pointer to a list of pointers to
35  * struct dirent (through namelist). Returns -1 if there were any errors.
36  */
37 
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <dirent.h>
41 #include <errno.h>
42 #include <stdint.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include "telldir.h"
46 
47 #define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))
48 
49 /*
50  * The DIRSIZ macro is the minimum record length which will hold the directory
51  * entry.  This requires the amount of space in struct dirent without the
52  * d_name field, plus enough space for the name and a terminating nul byte
53  * (dp->d_namlen + 1), rounded up to a 4 byte boundary.
54  */
55 #undef DIRSIZ
56 #define DIRSIZ(dp)							\
57 	((sizeof(struct dirent) - sizeof(dp)->d_name) +			\
58 	    (((dp)->d_namlen + 1 + 3) &~ 3))
59 
60 int
scandir(const char * dirname,struct dirent *** namelist,int (* select)(const struct dirent *),int (* dcomp)(const struct dirent **,const struct dirent **))61 scandir(const char *dirname, struct dirent ***namelist,
62     int (*select)(const struct dirent *),
63     int (*dcomp)(const struct dirent **, const struct dirent **))
64 {
65 	struct dirent *d, *p, **names = NULL;
66 	size_t nitems = 0;
67 	struct stat stb;
68 	long arraysz;
69 	DIR *dirp;
70 
71 	if ((dirp = opendir(dirname)) == NULL)
72 		return (-1);
73 	if (fstat(dirp->dd_fd, &stb) == -1)
74 		goto fail;
75 
76 	/*
77 	 * estimate the array size by taking the size of the directory file
78 	 * and dividing it by a multiple of the minimum size entry.
79 	 */
80 	arraysz = MAXIMUM(stb.st_size / 24, 16);
81 	if (arraysz > SIZE_MAX / sizeof(struct dirent *)) {
82 		errno = ENOMEM;
83 		goto fail;
84 	}
85 	names = calloc(arraysz, sizeof(struct dirent *));
86 	if (names == NULL)
87 		goto fail;
88 
89 	while ((d = readdir(dirp)) != NULL) {
90 		if (select != NULL && !(*select)(d))
91 			continue;	/* just selected names */
92 
93 		/*
94 		 * Check to make sure the array has space left and
95 		 * realloc the maximum size.
96 		 */
97 		if (nitems >= arraysz) {
98 			struct dirent **nnames;
99 
100 			if (fstat(dirp->dd_fd, &stb) == -1)
101 				goto fail;
102 
103 			arraysz *= 2;
104 			if (SIZE_MAX / sizeof(struct dirent *) < arraysz)
105 				goto fail;
106 			nnames = reallocarray(names,
107 			    arraysz, sizeof(struct dirent *));
108 			if (nnames == NULL)
109 				goto fail;
110 
111 			names = nnames;
112 		}
113 
114 		/*
115 		 * Make a minimum size copy of the data
116 		 */
117 		p = malloc(DIRSIZ(d));
118 		if (p == NULL)
119 			goto fail;
120 
121 		p->d_ino = d->d_ino;
122 		p->d_type = d->d_type;
123 		p->d_reclen = d->d_reclen;
124 		p->d_namlen = d->d_namlen;
125 		bcopy(d->d_name, p->d_name, p->d_namlen + 1);
126 		names[nitems++] = p;
127 	}
128 	closedir(dirp);
129 	if (nitems && dcomp != NULL)
130 		qsort(names, nitems, sizeof(struct dirent *),
131 		    (int(*)(const void *, const void *))dcomp);
132 	*namelist = names;
133 	return (nitems);
134 
135 fail:
136 	while (nitems > 0)
137 		free(names[--nitems]);
138 	free(names);
139 	closedir(dirp);
140 	return (-1);
141 }
142 
143 /*
144  * Alphabetic order comparison routine for those who want it.
145  */
146 int
alphasort(const struct dirent ** d1,const struct dirent ** d2)147 alphasort(const struct dirent **d1, const struct dirent **d2)
148 {
149 	return(strcmp((*d1)->d_name, (*d2)->d_name));
150 }
151