1 /* Copyright (C) 1992-1998, 2000, 2002-2003, 2009-2025 Free Software
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/>. */
26 # include <bits/libc-lock.h>
31 #ifndef _D_EXACT_NAMLEN
32 # define _D_EXACT_NAMLEN(d) strlen ((d)->d_name)
34 #ifndef _D_ALLOC_NAMLEN
36 # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
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>. */
41 # define _D_ALLOC_NAMLEN(d) (sizeof (struct dirent) - \
42 offsetof (struct dirent, d_name))
48 # define SCANDIR scandir
49 # define READDIR __readdir
50 # define DIRENT_TYPE struct dirent
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
71 # ifdef GNULIB_defined_opendir
74 # ifdef GNULIB_defined_closedir
80 #ifndef SCANDIR_CANCEL
81 # define SCANDIR_CANCEL
82 struct scandir_cancel_struct
91 cancel_handler (void *arg
)
93 struct scandir_cancel_struct
*cp
= arg
;
97 for (i
= 0; i
< cp
->cnt
; ++i
)
100 (void) __closedir (cp
->dp
);
108 SCANDIR (const char *dir
,
109 DIRENT_TYPE
***namelist
,
110 int (*select
) (const DIRENT_TYPE
*),
111 int (*cmp
) (const DIRENT_TYPE
**, const DIRENT_TYPE
**))
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 *))
121 DIR *dp
= __opendir (dir
);
122 DIRENT_TYPE
**v
= NULL
;
124 struct scandir_cancel_struct c
;
138 __libc_cleanup_push (cancel_handler
, &c
);
141 while ((d
= READDIR (dp
)) != NULL
)
143 int use_it
= select
== NULL
;
148 /* The select function might have changed errno. It was
149 zero before and it need to be again to make the latter
159 /* Ignore errors from select or readdir */
162 if (__builtin_expect (c
.cnt
== vsize
, 0))
169 new = (DIRENT_TYPE
**) realloc (v
, vsize
* sizeof (*v
));
176 dsize
= &d
->d_name
[_D_ALLOC_NAMLEN (d
)] - (char *) d
;
177 vnew
= (DIRENT_TYPE
*) malloc (dsize
);
181 v
[c
.cnt
++] = (DIRENT_TYPE
*) memcpy (vnew
, d
, dsize
);
185 if (__builtin_expect (errno
, 0) != 0)
196 /* Sort the list if we have a comparison function to sort with. */
198 qsort (v
, c
.cnt
, sizeof (*v
), (int (*) (const void *, const void *)) cmp
);
204 __libc_cleanup_pop (0);
207 (void) __closedir (dp
);