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