pthread-cond: Fix compilation error on native Windows.
[gnulib.git] / lib / scandir.c
blob3811b337706c8827bc08499fd8fd1ceb52dc6158
1 /* Copyright (C) 1992-1998, 2000, 2002-2003, 2009-2024 Free Software
2 Foundation, Inc.
3 This file is part of the GNU C Library.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 3 of the
8 License, or (at your option) any later version.
10 This file 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://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 #undef select
31 #ifndef _D_EXACT_NAMLEN
32 # define _D_EXACT_NAMLEN(d) strlen ((d)->d_name)
33 #endif
34 #ifndef _D_ALLOC_NAMLEN
35 # ifndef __KLIBC__
36 # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
37 # else
38 /* On OS/2 kLIBC, d_name is not the last field of struct dirent. See
39 <https://trac.netlabs.org/libc/browser/branches/libc-0.6/src/emx/include/sys/dirent.h#L68>. */
40 # include <stddef.h>
41 # define _D_ALLOC_NAMLEN(d) (sizeof (struct dirent) - \
42 offsetof (struct dirent, d_name))
43 # endif
44 #endif
46 #if _LIBC
47 # ifndef SCANDIR
48 # define SCANDIR scandir
49 # define READDIR __readdir
50 # define DIRENT_TYPE struct dirent
51 # endif
52 #else
53 # define SCANDIR scandir
54 # define READDIR readdir
55 # define DIRENT_TYPE struct dirent
56 # define __opendir opendir
57 # define __closedir closedir
58 # define __set_errno(val) errno = (val)
60 /* The results of opendir() in this file are not used with dirfd and fchdir,
61 and we do not leak fds to any single-threaded code that could use stdio,
62 therefore save some unnecessary recursion in fchdir.c and opendir_safer.c.
63 FIXME - if the kernel ever adds support for multi-thread safety for
64 avoiding standard fds, then we should use opendir_safer. */
65 # ifdef GNULIB_defined_DIR
66 # undef DIR
67 # undef opendir
68 # undef closedir
69 # undef readdir
70 # else
71 # ifdef GNULIB_defined_opendir
72 # undef opendir
73 # endif
74 # ifdef GNULIB_defined_closedir
75 # undef closedir
76 # endif
77 # endif
78 #endif
80 #ifndef SCANDIR_CANCEL
81 # define SCANDIR_CANCEL
82 struct scandir_cancel_struct
84 DIR *dp;
85 void *v;
86 size_t cnt;
89 # if _LIBC
90 static void
91 cancel_handler (void *arg)
93 struct scandir_cancel_struct *cp = arg;
94 size_t i;
95 void **v = cp->v;
97 for (i = 0; i < cp->cnt; ++i)
98 free (v[i]);
99 free (v);
100 (void) __closedir (cp->dp);
102 # endif
103 #endif
107 #ifndef __KLIBC__
108 SCANDIR (const char *dir,
109 DIRENT_TYPE ***namelist,
110 int (*select) (const DIRENT_TYPE *),
111 int (*cmp) (const DIRENT_TYPE **, const DIRENT_TYPE **))
112 #else
113 /* On OS/2 kLIBC, scandir() declaration is different from POSIX. See
114 <https://trac.netlabs.org/libc/browser/branches/libc-0.6/src/emx/include/dirent.h#L141>. */
115 SCANDIR (const char *dir,
116 DIRENT_TYPE ***namelist,
117 int (*select) (DIRENT_TYPE *),
118 int (*cmp) (const void *, const void *))
119 #endif
121 DIR *dp = __opendir (dir);
122 DIRENT_TYPE **v = NULL;
123 size_t vsize = 0;
124 struct scandir_cancel_struct c;
125 DIRENT_TYPE *d;
126 int save;
128 if (dp == NULL)
129 return -1;
131 save = errno;
132 __set_errno (0);
134 c.dp = dp;
135 c.v = NULL;
136 c.cnt = 0;
137 #if _LIBC
138 __libc_cleanup_push (cancel_handler, &c);
139 #endif
141 while ((d = READDIR (dp)) != NULL)
143 int use_it = select == NULL;
145 if (! use_it)
147 use_it = select (d);
148 /* The select function might have changed errno. It was
149 zero before and it need to be again to make the latter
150 tests work. */
151 __set_errno (0);
154 if (use_it)
156 DIRENT_TYPE *vnew;
157 size_t dsize;
159 /* Ignore errors from select or readdir */
160 __set_errno (0);
162 if (__builtin_expect (c.cnt == vsize, 0))
164 DIRENT_TYPE **new;
165 if (vsize == 0)
166 vsize = 10;
167 else
168 vsize *= 2;
169 new = (DIRENT_TYPE **) realloc (v, vsize * sizeof (*v));
170 if (new == NULL)
171 break;
172 v = new;
173 c.v = (void *) v;
176 dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
177 vnew = (DIRENT_TYPE *) malloc (dsize);
178 if (vnew == NULL)
179 break;
181 v[c.cnt++] = (DIRENT_TYPE *) memcpy (vnew, d, dsize);
185 if (__builtin_expect (errno, 0) != 0)
187 save = errno;
189 while (c.cnt > 0)
190 free (v[--c.cnt]);
191 free (v);
192 c.cnt = -1;
194 else
196 /* Sort the list if we have a comparison function to sort with. */
197 if (cmp != NULL)
198 qsort (v, c.cnt, sizeof (*v), (int (*) (const void *, const void *)) cmp);
200 *namelist = v;
203 #if _LIBC
204 __libc_cleanup_pop (0);
205 #endif
207 (void) __closedir (dp);
208 __set_errno (save);
210 return c.cnt;