maint: update copyright
[gnulib.git] / lib / scandir.c
1 /* Copyright (C) 1992-1998, 2000, 2002-2003, 2009-2014 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, see <http://www.gnu.org/licenses/>.  */
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 #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
41
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)
55
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
64
65 #ifndef SCANDIR_CANCEL
66 # define SCANDIR_CANCEL
67 struct scandir_cancel_struct
68 {
69   DIR *dp;
70   void *v;
71   size_t cnt;
72 };
73
74 # if _LIBC
75 static void
76 cancel_handler (void *arg)
77 {
78   struct scandir_cancel_struct *cp = arg;
79   size_t i;
80   void **v = cp->v;
81
82   for (i = 0; i < cp->cnt; ++i)
83     free (v[i]);
84   free (v);
85   (void) __closedir (cp->dp);
86 }
87 # endif
88 #endif
89
90
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 **))
96 {
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;
103
104   if (dp == NULL)
105     return -1;
106
107   save = errno;
108   __set_errno (0);
109
110   c.dp = dp;
111   c.v = NULL;
112   c.cnt = 0;
113 #if _LIBC
114   __libc_cleanup_push (cancel_handler, &c);
115 #endif
116
117   while ((d = READDIR (dp)) != NULL)
118     {
119       int use_it = select == NULL;
120
121       if (! use_it)
122         {
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);
128         }
129
130       if (use_it)
131         {
132           DIRENT_TYPE *vnew;
133           size_t dsize;
134
135           /* Ignore errors from select or readdir */
136           __set_errno (0);
137
138           if (__builtin_expect (c.cnt == vsize, 0))
139             {
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;
150             }
151
152           dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
153           vnew = (DIRENT_TYPE *) malloc (dsize);
154           if (vnew == NULL)
155             break;
156
157           v[c.cnt++] = (DIRENT_TYPE *) memcpy (vnew, d, dsize);
158         }
159     }
160
161   if (__builtin_expect (errno, 0) != 0)
162     {
163       save = errno;
164
165       while (c.cnt > 0)
166         free (v[--c.cnt]);
167       free (v);
168       c.cnt = -1;
169     }
170   else
171     {
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);
175
176       *namelist = v;
177     }
178
179 #if _LIBC
180   __libc_cleanup_pop (0);
181 #endif
182
183   (void) __closedir (dp);
184   __set_errno (save);
185
186   return c.cnt;
187 }