Updated gnulib and added hash-pjw-bare
[gnutls.git] / gl / scandir.c
blob2f4dd18e8ba4849d879c52c0a144e0c2cdb4b380
1 /* Copyright (C) 1992-1998, 2000, 2002-2003, 2009-2012 Free Software
2 Foundation, Inc.
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
8 later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include <dirent.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #if _LIBC
26 # include <bits/libc-lock.h>
27 #endif
29 #if ! defined __builtin_expect && __GNUC__ < 3
30 # define __builtin_expect(expr, expected) (expr)
31 #endif
33 #undef select
35 #ifndef _D_EXACT_NAMLEN
36 # define _D_EXACT_NAMLEN(d) strlen ((d)->d_name)
37 #endif
38 #ifndef _D_ALLOC_NAMLEN
39 # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
40 #endif
42 #if _LIBC
43 # ifndef SCANDIR
44 # define SCANDIR scandir
45 # define READDIR __readdir
46 # define DIRENT_TYPE struct dirent
47 # endif
48 #else
49 # define SCANDIR scandir
50 # define READDIR readdir
51 # define DIRENT_TYPE struct dirent
52 # define __opendir opendir
53 # define __closedir closedir
54 # define __set_errno(val) errno = (val)
56 /* The results of opendir() in this file are not used with dirfd and fchdir,
57 and we do not leak fds to any single-threaded code that could use stdio,
58 therefore save some unnecessary recursion in fchdir.c and opendir_safer.c.
59 FIXME - if the kernel ever adds support for multi-thread safety for
60 avoiding standard fds, then we should use opendir_safer. */
61 # undef opendir
62 # undef closedir
63 #endif
65 #ifndef SCANDIR_CANCEL
66 # define SCANDIR_CANCEL
67 struct scandir_cancel_struct
69 DIR *dp;
70 void *v;
71 size_t cnt;
74 # if _LIBC
75 static void
76 cancel_handler (void *arg)
78 struct scandir_cancel_struct *cp = arg;
79 size_t i;
80 void **v = cp->v;
82 for (i = 0; i < cp->cnt; ++i)
83 free (v[i]);
84 free (v);
85 (void) __closedir (cp->dp);
87 # endif
88 #endif
91 int
92 SCANDIR (const char *dir,
93 DIRENT_TYPE ***namelist,
94 int (*select) (const DIRENT_TYPE *),
95 int (*cmp) (const DIRENT_TYPE **, const DIRENT_TYPE **))
97 DIR *dp = __opendir (dir);
98 DIRENT_TYPE **v = NULL;
99 size_t vsize = 0;
100 struct scandir_cancel_struct c;
101 DIRENT_TYPE *d;
102 int save;
104 if (dp == NULL)
105 return -1;
107 save = errno;
108 __set_errno (0);
110 c.dp = dp;
111 c.v = NULL;
112 c.cnt = 0;
113 #if _LIBC
114 __libc_cleanup_push (cancel_handler, &c);
115 #endif
117 while ((d = READDIR (dp)) != NULL)
119 int use_it = select == NULL;
121 if (! use_it)
123 use_it = select (d);
124 /* The select function might have changed errno. It was
125 zero before and it need to be again to make the latter
126 tests work. */
127 __set_errno (0);
130 if (use_it)
132 DIRENT_TYPE *vnew;
133 size_t dsize;
135 /* Ignore errors from select or readdir */
136 __set_errno (0);
138 if (__builtin_expect (c.cnt == vsize, 0))
140 DIRENT_TYPE **new;
141 if (vsize == 0)
142 vsize = 10;
143 else
144 vsize *= 2;
145 new = (DIRENT_TYPE **) realloc (v, vsize * sizeof (*v));
146 if (new == NULL)
147 break;
148 v = new;
149 c.v = (void *) v;
152 dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
153 vnew = (DIRENT_TYPE *) malloc (dsize);
154 if (vnew == NULL)
155 break;
157 v[c.cnt++] = (DIRENT_TYPE *) memcpy (vnew, d, dsize);
161 if (__builtin_expect (errno, 0) != 0)
163 save = errno;
165 while (c.cnt > 0)
166 free (v[--c.cnt]);
167 free (v);
168 c.cnt = -1;
170 else
172 /* Sort the list if we have a comparison function to sort with. */
173 if (cmp != NULL)
174 qsort (v, c.cnt, sizeof (*v), (int (*) (const void *, const void *)) cmp);
176 *namelist = v;
179 #if _LIBC
180 __libc_cleanup_pop (0);
181 #endif
183 (void) __closedir (dp);
184 __set_errno (save);
186 return c.cnt;