4 * Copyright (c) 1983 Regents of the University of California.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid
[] = "@(#)scandir.c 5.10 (Berkeley) 2/23/91";
38 #endif /* LIBC_SCCS and not lint */
41 * Scan the directory dirname calling select to make a list of selected
42 * directory entries then sort using qsort and compare routine dcomp.
43 * Returns the number of entries and a pointer to a list of pointers to
44 * struct dirent (through namelist). Returns -1 if there were any errors.
47 #include <sys/types.h>
55 * The DIRSIZ macro gives the minimum record length which will hold
56 * the directory entry. This requires the amount of space in struct dirent
57 * without the d_name field, plus enough space for the name with a terminating
58 * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
61 #ifdef _DIRENT_HAVE_D_NAMLEN
63 ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
66 ((sizeof (struct dirent) - (MAXNAMLEN+1)) + ((strlen((dp)->d_name)+1 + 3) &~ 3))
74 _DEFUN(scandir
, (dirname
, namelist
, select
, dcomp
),
75 const char *dirname _AND
76 struct dirent
***namelist _AND
77 int (*select
) __P((struct dirent
*)) _AND
78 int (*dcomp
) __P((const void *, const void *)))
80 register struct dirent
*d
, *p
, **names
;
81 register size_t nitems
;
86 if ((dirp
= opendir(dirname
)) == NULL
)
89 __lock_acquire_recursive(dirp
->dd_lock
);
91 if (fstat(dirp
->dd_fd
, &stb
) < 0) {
93 __lock_release_recursive(dirp
->dd_lock
);
99 * estimate the array size by taking the size of the directory file
100 * and dividing it by a multiple of the minimum size entry.
102 arraysz
= (stb
.st_size
/ 24);
103 names
= (struct dirent
**)malloc(arraysz
* sizeof(struct dirent
*));
106 __lock_release_recursive(dirp
->dd_lock
);
112 while ((d
= readdir(dirp
)) != NULL
) {
113 if (select
!= NULL
&& !(*select
)(d
))
114 continue; /* just selected names */
116 * Make a minimum size copy of the data
118 p
= (struct dirent
*)malloc(DIRSIZ(d
));
121 __lock_release_recursive(dirp
->dd_lock
);
126 p
->d_reclen
= d
->d_reclen
;
127 #ifdef _DIRENT_HAVE_D_NAMLEN
128 p
->d_namlen
= d
->d_namlen
;
129 bcopy(d
->d_name
, p
->d_name
, p
->d_namlen
+ 1);
131 strcpy(p
->d_name
, d
->d_name
);
134 * Check to make sure the array has space left and
135 * realloc the maximum size.
137 if (++nitems
>= arraysz
) {
138 if (fstat(dirp
->dd_fd
, &stb
) < 0) {
140 __lock_release_recursive(dirp
->dd_lock
);
142 return(-1); /* just might have grown */
144 arraysz
= stb
.st_size
/ 12;
145 names
= (struct dirent
**)realloc((char *)names
,
146 arraysz
* sizeof(struct dirent
*));
149 __lock_release_recursive(dirp
->dd_lock
);
157 if (nitems
&& dcomp
!= NULL
)
158 qsort(names
, nitems
, sizeof(struct dirent
*), dcomp
);
161 __lock_release_recursive(dirp
->dd_lock
);
167 * Alphabetic order comparison routine for those who want it.
170 _DEFUN(alphasort
, (d1
, d2
),
171 const struct dirent
**d1 _AND
172 const struct dirent
**d2
)
174 return(strcmp((*d1
)->d_name
, (*d2
)->d_name
));
177 #endif /* ! HAVE_OPENDIR */