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