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