1 |
nmav |
1.1 |
/* Copyright (C) 1992-1998, 2000 Free Software Foundation, Inc. |
2 |
|
|
This file is part of the GNU C Library. |
3 |
|
|
|
4 |
|
|
The GNU C Library is free software; you can redistribute it and/or |
5 |
|
|
modify it under the terms of the GNU Lesser General Public |
6 |
|
|
License as published by the Free Software Foundation; either |
7 |
|
|
version 2.1 of the License, or (at your option) any later version. |
8 |
|
|
|
9 |
|
|
The GNU C Library is distributed in the hope that it will be useful, |
10 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
|
|
Lesser General Public License for more details. |
13 |
|
|
|
14 |
|
|
You should have received a copy of the GNU Lesser General Public |
15 |
|
|
License along with the GNU C Library; if not, write to the Free |
16 |
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
17 |
|
|
02111-1307 USA. */ |
18 |
|
|
|
19 |
|
|
/* This was modified for use in Hydra web server. --nmav |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#include <dirent.h> |
23 |
|
|
#include <stdlib.h> |
24 |
|
|
#include <string.h> |
25 |
|
|
#include <errno.h> |
26 |
|
|
|
27 |
|
|
#include "compat.h" |
28 |
|
|
|
29 |
|
|
#ifndef HAVE_SCANDIR |
30 |
|
|
|
31 |
|
|
int |
32 |
|
|
scandir ( const char *dir, struct dirent ***namelist, |
33 |
|
|
int (*select) (const struct dirent *), |
34 |
|
|
int (*cmp) (const void *, const void *)) |
35 |
|
|
{ |
36 |
|
|
DIR *dp = opendir (dir); |
37 |
|
|
struct dirent **v = NULL; |
38 |
|
|
size_t vsize = 0, i; |
39 |
|
|
struct dirent *d; |
40 |
|
|
int save; |
41 |
|
|
|
42 |
|
|
if (dp == NULL) |
43 |
|
|
return -1; |
44 |
|
|
|
45 |
|
|
save = errno; |
46 |
|
|
errno = (0); |
47 |
|
|
|
48 |
|
|
i = 0; |
49 |
|
|
while ((d = readdir (dp)) != NULL) |
50 |
|
|
if (select == NULL || (*select) (d)) |
51 |
|
|
{ |
52 |
|
|
struct dirent *vnew; |
53 |
|
|
size_t dsize; |
54 |
|
|
|
55 |
|
|
/* Ignore errors from select or readdir */ |
56 |
|
|
errno = (0); |
57 |
|
|
|
58 |
|
|
if ( i == vsize) |
59 |
|
|
{ |
60 |
|
|
struct dirent **new; |
61 |
|
|
if (vsize == 0) |
62 |
|
|
vsize = 10; |
63 |
|
|
else |
64 |
|
|
vsize *= 2; |
65 |
|
|
new = (struct dirent **) realloc (v, vsize * sizeof (*v)); |
66 |
|
|
if (new == NULL) |
67 |
|
|
break; |
68 |
|
|
v = new; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
/* dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d; |
72 |
|
|
*/ |
73 |
|
|
dsize = d->d_reclen; |
74 |
|
|
vnew = (struct dirent *) malloc (dsize); |
75 |
|
|
if (vnew == NULL) |
76 |
|
|
break; |
77 |
|
|
|
78 |
|
|
v[i++] = (struct dirent *) memcpy (vnew, d, dsize); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
if ( errno != 0) |
82 |
|
|
{ |
83 |
|
|
save = errno; |
84 |
|
|
(void) closedir (dp); |
85 |
|
|
while (i > 0) |
86 |
|
|
free (v[--i]); |
87 |
|
|
free (v); |
88 |
|
|
errno = (save); |
89 |
|
|
return -1; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
(void) closedir (dp); |
93 |
|
|
errno = (save); |
94 |
|
|
|
95 |
|
|
/* Sort the list if we have a comparison function to sort with. */ |
96 |
|
|
#ifdef HAVE_QSORT |
97 |
|
|
if (cmp != NULL) |
98 |
|
|
qsort (v, i, sizeof (*v), cmp); |
99 |
|
|
#endif |
100 |
|
|
|
101 |
|
|
*namelist = v; |
102 |
|
|
return i; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
#endif |