Merge branch 'upstream' into stable
[gnulib.git] / lib / scandir.c
1 /* Copyright (C) 1992-1998, 2000, 2002, 2003, 2009 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    This program is free software; you can redistribute it and/or modify it
5    under the terms of the GNU General Public License as published by the
6    Free Software Foundation; either version 2, or (at your option) any
7    later version.
8
9    This program 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
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 #include <config.h>
19
20 #include <dirent.h>
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #if _LIBC
26 # include <bits/libc-lock.h>
27 #endif
28
29 #if ! defined __builtin_expect && __GNUC__ < 3
30 # define __builtin_expect(expr, expected) (expr)
31 #endif
32
33 #undef select
34
35 #if _LIBC
36 # ifndef SCANDIR
37 #  define SCANDIR scandir
38 #  define READDIR __readdir
39 #  define DIRENT_TYPE struct dirent
40 # endif
41 #else
42 # define SCANDIR scandir
43 # define READDIR readdir
44 # define DIRENT_TYPE struct dirent
45 # define __opendir opendir
46 # define __closedir closedir
47 # define __set_errno(val) errno = (val)
48
49 /* The results of opendir() in this file are not used with dirfd and fchdir,
50    and we do not leak fds to any single-threaded code that could use stdio,
51    therefore save some unnecessary recursion in fchdir.c and opendir_safer.c.
52    FIXME - if the kernel ever adds support for multi-thread safety for
53    avoiding standard fds, then we should use opendir_safer.  */
54 # undef opendir
55 # undef closedir
56 #endif
57
58 #ifndef SCANDIR_CANCEL
59 # define SCANDIR_CANCEL
60 struct scandir_cancel_struct
61 {
62   DIR *dp;
63   void *v;
64   size_t cnt;
65 };
66
67 # if _LIBC
68 static void
69 cancel_handler (void *arg)
70 {
71   struct scandir_cancel_struct *cp = arg;
72   size_t i;
73   void **v = cp->v;
74
75   for (i = 0; i < cp->cnt; ++i)
76     free (v[i]);
77   free (v);
78   (void) __closedir (cp->dp);
79 }
80 # endif
81 #endif
82
83
84 int
85 SCANDIR (const char *dir,
86          DIRENT_TYPE ***namelist,
87          int (*select) (const DIRENT_TYPE *),
88          int (*cmp) (const DIRENT_TYPE **, const DIRENT_TYPE **))
89 {
90   DIR *dp = __opendir (dir);
91   DIRENT_TYPE **v = NULL;
92   size_t vsize = 0;
93   struct scandir_cancel_struct c;
94   DIRENT_TYPE *d;
95   int save;
96
97   if (dp == NULL)
98     return -1;
99
100   save = errno;
101   __set_errno (0);
102
103   c.dp = dp;
104   c.v = NULL;
105   c.cnt = 0;
106 #if _LIBC
107   __libc_cleanup_push (cancel_handler, &c);
108 #endif
109
110   while ((d = READDIR (dp)) != NULL)
111     {
112       int use_it = select == NULL;
113
114       if (! use_it)
115         {
116           use_it = select (d);
117           /* The select function might have changed errno.  It was
118              zero before and it need to be again to make the latter
119              tests work.  */
120           __set_errno (0);
121         }
122
123       if (use_it)
124         {
125           DIRENT_TYPE *vnew;
126           size_t dsize;
127
128           /* Ignore errors from select or readdir */
129           __set_errno (0);
130
131           if (__builtin_expect (c.cnt == vsize, 0))
132             {
133               DIRENT_TYPE **new;
134               if (vsize == 0)
135                 vsize = 10;
136               else
137                 vsize *= 2;
138               new = (DIRENT_TYPE **) realloc (v, vsize * sizeof (*v));
139               if (new == NULL)
140                 break;
141               v = new;
142               c.v = (void *) v;
143             }
144
145           dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
146           vnew = (DIRENT_TYPE *) malloc (dsize);
147           if (vnew == NULL)
148             break;
149
150           v[c.cnt++] = (DIRENT_TYPE *) memcpy (vnew, d, dsize);
151         }
152     }
153
154   if (__builtin_expect (errno, 0) != 0)
155     {
156       save = errno;
157
158       while (c.cnt > 0)
159         free (v[--c.cnt]);
160       free (v);
161       c.cnt = -1;
162     }
163   else
164     {
165       /* Sort the list if we have a comparison function to sort with.  */
166       if (cmp != NULL)
167         qsort (v, c.cnt, sizeof (*v), (int (*) (const void *, const void *)) cmp);
168
169       *namelist = v;
170     }
171
172 #if _LIBC
173   __libc_cleanup_pop (0);
174 #endif
175
176   (void) __closedir (dp);
177   __set_errno (save);
178
179   return c.cnt;
180 }