xref: /csrg-svn/lib/libc/gen/scandir.c (revision 36558)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static char sccsid[] = "@(#)scandir.c	5.5 (Berkeley) 01/16/89";
20 #endif /* LIBC_SCCS and not lint */
21 
22 /*
23  * Scan the directory dirname calling select to make a list of selected
24  * directory entries then sort using qsort and compare routine dcomp.
25  * Returns the number of entries and a pointer to a list of pointers to
26  * struct dirent (through namelist). Returns -1 if there were any errors.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <dirent.h>
32 
33 /*
34  * The DIRSIZ macro gives the minimum record length which will hold
35  * the directory entry.  This requires the amount of space in struct dirent
36  * without the d_name field, plus enough space for the name with a terminating
37  * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
38  */
39 #undef DIRSIZ
40 #define DIRSIZ(dp) \
41     ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
42 
43 scandir(dirname, namelist, select, dcomp)
44 	char *dirname;
45 	struct dirent *(*namelist[]);
46 	int (*select)(), (*dcomp)();
47 {
48 	register struct dirent *d, *p, **names;
49 	register int nitems;
50 	register char *cp1, *cp2;
51 	struct stat stb;
52 	long arraysz;
53 	DIR *dirp;
54 
55 	if ((dirp = opendir(dirname)) == NULL)
56 		return(-1);
57 	if (fstat(dirp->dd_fd, &stb) < 0)
58 		return(-1);
59 
60 	/*
61 	 * estimate the array size by taking the size of the directory file
62 	 * and dividing it by a multiple of the minimum size entry.
63 	 */
64 	arraysz = (stb.st_size / 24);
65 	names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
66 	if (names == NULL)
67 		return(-1);
68 
69 	nitems = 0;
70 	while ((d = readdir(dirp)) != NULL) {
71 		if (select != NULL && !(*select)(d))
72 			continue;	/* just selected names */
73 		/*
74 		 * Make a minimum size copy of the data
75 		 */
76 		p = (struct dirent *)malloc(DIRSIZ(d));
77 		if (p == NULL)
78 			return(-1);
79 		p->d_ino = d->d_ino;
80 		p->d_reclen = d->d_reclen;
81 		p->d_namlen = d->d_namlen;
82 		for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; );
83 		/*
84 		 * Check to make sure the array has space left and
85 		 * realloc the maximum size.
86 		 */
87 		if (++nitems >= arraysz) {
88 			if (fstat(dirp->dd_fd, &stb) < 0)
89 				return(-1);	/* just might have grown */
90 			arraysz = stb.st_size / 12;
91 			names = (struct dirent **)realloc((char *)names,
92 				arraysz * sizeof(struct dirent *));
93 			if (names == NULL)
94 				return(-1);
95 		}
96 		names[nitems-1] = p;
97 	}
98 	closedir(dirp);
99 	if (nitems && dcomp != NULL)
100 		qsort(names, nitems, sizeof(struct dirent *), dcomp);
101 	*namelist = names;
102 	return(nitems);
103 }
104 
105 /*
106  * Alphabetic order comparison routine for those who want it.
107  */
108 alphasort(d1, d2)
109 	struct dirent **d1, **d2;
110 {
111 	return(strcmp((*d1)->d_name, (*d2)->d_name));
112 }
113