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