xref: /openbsd-src/lib/libc/gen/scandir.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: scandir.c,v 1.16 2013/04/17 17:40:35 tedu 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/param.h>
39 #include <sys/stat.h>
40 #include <dirent.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include "telldir.h"
46 
47 /*
48  * The DIRSIZ macro is the minimum record length which will hold the directory
49  * entry.  This requires the amount of space in struct dirent without the
50  * d_name field, plus enough space for the name and a terminating nul byte
51  * (dp->d_namlen + 1), rounded up to a 4 byte boundary.
52  */
53 #undef DIRSIZ
54 #define DIRSIZ(dp)							\
55 	((sizeof(struct dirent) - sizeof(dp)->d_name) +			\
56 	    (((dp)->d_namlen + 1 + 3) &~ 3))
57 
58 int
59 scandir(const char *dirname, struct dirent ***namelist,
60     int (*select)(const struct dirent *),
61     int (*dcomp)(const struct dirent **, const struct dirent **))
62 {
63 	struct dirent *d, *p, **names = NULL;
64 	size_t nitems = 0;
65 	struct stat stb;
66 	long arraysz;
67 	DIR *dirp;
68 
69 	if ((dirp = opendir(dirname)) == NULL)
70 		return (-1);
71 	if (fstat(dirp->dd_fd, &stb) < 0)
72 		goto fail;
73 
74 	/*
75 	 * estimate the array size by taking the size of the directory file
76 	 * and dividing it by a multiple of the minimum size entry.
77 	 */
78 	arraysz = MAX(stb.st_size / 24, 16);
79 	if (arraysz > SIZE_MAX / sizeof(struct dirent *)) {
80 		errno = ENOMEM;
81 		goto fail;
82 	}
83 	names = (struct dirent **)calloc(arraysz, sizeof(struct dirent *));
84 	if (names == NULL)
85 		goto fail;
86 
87 	while ((d = readdir(dirp)) != NULL) {
88 		if (select != NULL && !(*select)(d))
89 			continue;	/* just selected names */
90 
91 		/*
92 		 * Check to make sure the array has space left and
93 		 * realloc the maximum size.
94 		 */
95 		if (nitems >= arraysz) {
96 			struct dirent **nnames;
97 
98 			if (fstat(dirp->dd_fd, &stb) < 0)
99 				goto fail;
100 
101 			arraysz *= 2;
102 			if (SIZE_MAX / sizeof(struct dirent *) < arraysz)
103 				goto fail;
104 			nnames = (struct dirent **)realloc((char *)names,
105 				arraysz * sizeof(struct dirent *));
106 			if (nnames == NULL)
107 				goto fail;
108 
109 			names = nnames;
110 		}
111 
112 		/*
113 		 * Make a minimum size copy of the data
114 		 */
115 		p = (struct dirent *)malloc(DIRSIZ(d));
116 		if (p == NULL)
117 			goto fail;
118 
119 		p->d_ino = d->d_ino;
120 		p->d_type = d->d_type;
121 		p->d_reclen = d->d_reclen;
122 		p->d_namlen = d->d_namlen;
123 		bcopy(d->d_name, p->d_name, p->d_namlen + 1);
124 		names[nitems++] = p;
125 	}
126 	closedir(dirp);
127 	if (nitems && dcomp != NULL)
128 		qsort(names, nitems, sizeof(struct dirent *),
129 		    (int(*)(const void *, const void *))dcomp);
130 	*namelist = names;
131 	return (nitems);
132 
133 fail:
134 	while (nitems > 0)
135 		free(names[--nitems]);
136 	free(names);
137 	closedir(dirp);
138 	return (-1);
139 }
140 
141 /*
142  * Alphabetic order comparison routine for those who want it.
143  */
144 int
145 alphasort(const struct dirent **d1, const struct dirent **d2)
146 {
147 	return(strcmp((*d1)->d_name, (*d2)->d_name));
148 }
149