]>
Commit | Line | Data |
---|---|---|
a711dd4b | 1 | /* Copyright (C) 1992-1998, 2000 Free Software Foundation, Inc. |
47707456 | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
47707456 UD |
4 | The GNU C Library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public License as | |
6 | published by the Free Software Foundation; either version 2 of the | |
7 | License, or (at your option) any later version. | |
28f540f4 | 8 | |
47707456 UD |
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 | Library General Public License for more details. | |
28f540f4 | 13 | |
47707456 UD |
14 | You should have received a copy of the GNU Library General Public |
15 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
16 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
17 | Boston, MA 02111-1307, USA. */ | |
28f540f4 | 18 | |
28f540f4 RM |
19 | #include <dirent.h> |
20 | #include <stdlib.h> | |
01cdeca0 | 21 | #include <string.h> |
60092701 | 22 | #include <errno.h> |
28f540f4 | 23 | |
56ddf355 UD |
24 | #ifndef SCANDIR |
25 | #define SCANDIR scandir | |
26 | #define READDIR __readdir | |
27 | #define DIRENT_TYPE struct dirent | |
28 | #endif | |
29 | ||
28f540f4 | 30 | int |
56ddf355 | 31 | SCANDIR (dir, namelist, select, cmp) |
c4029823 | 32 | const char *dir; |
56ddf355 UD |
33 | DIRENT_TYPE ***namelist; |
34 | int (*select) (const DIRENT_TYPE *); | |
a711dd4b | 35 | int (*cmp) (const void *, const void *); |
28f540f4 | 36 | { |
50304ef0 | 37 | DIR *dp = __opendir (dir); |
56ddf355 | 38 | DIRENT_TYPE **v = NULL; |
28f540f4 | 39 | size_t vsize = 0, i; |
56ddf355 | 40 | DIRENT_TYPE *d; |
28f540f4 RM |
41 | int save; |
42 | ||
43 | if (dp == NULL) | |
44 | return -1; | |
45 | ||
46 | save = errno; | |
c4029823 | 47 | __set_errno (0); |
28f540f4 RM |
48 | |
49 | i = 0; | |
56ddf355 | 50 | while ((d = READDIR (dp)) != NULL) |
28f540f4 RM |
51 | if (select == NULL || (*select) (d)) |
52 | { | |
56ddf355 | 53 | DIRENT_TYPE *vnew; |
01cdeca0 RM |
54 | size_t dsize; |
55 | ||
af6f3906 UD |
56 | /* Ignore errors from select or readdir */ |
57 | __set_errno (0); | |
58 | ||
a711dd4b | 59 | if (__builtin_expect (i == vsize, 0)) |
28f540f4 | 60 | { |
56ddf355 | 61 | DIRENT_TYPE **new; |
28f540f4 RM |
62 | if (vsize == 0) |
63 | vsize = 10; | |
64 | else | |
65 | vsize *= 2; | |
56ddf355 | 66 | new = (DIRENT_TYPE **) realloc (v, vsize * sizeof (*v)); |
28f540f4 | 67 | if (new == NULL) |
89a9e37b | 68 | break; |
28f540f4 RM |
69 | v = new; |
70 | } | |
71 | ||
92777700 | 72 | dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d; |
56ddf355 | 73 | vnew = (DIRENT_TYPE *) malloc (dsize); |
89a9e37b UD |
74 | if (vnew == NULL) |
75 | break; | |
28f540f4 | 76 | |
56ddf355 | 77 | v[i++] = (DIRENT_TYPE *) memcpy (vnew, d, dsize); |
28f540f4 RM |
78 | } |
79 | ||
a711dd4b | 80 | if (__builtin_expect (errno, 0) != 0) |
28f540f4 RM |
81 | { |
82 | save = errno; | |
50304ef0 | 83 | (void) __closedir (dp); |
28f540f4 RM |
84 | while (i > 0) |
85 | free (v[--i]); | |
86 | free (v); | |
c4029823 | 87 | __set_errno (save); |
28f540f4 RM |
88 | return -1; |
89 | } | |
90 | ||
50304ef0 | 91 | (void) __closedir (dp); |
c4029823 | 92 | __set_errno (save); |
28f540f4 RM |
93 | |
94 | /* Sort the list if we have a comparison function to sort with. */ | |
95 | if (cmp != NULL) | |
96 | qsort (v, i, sizeof (*v), cmp); | |
97 | *namelist = v; | |
98 | return i; | |
99 | } |