1 /* Copyright (C) 1992-1998, 2000, 2002-2003, 2009-2018 Free Software
3 This file is part of the GNU C Library.
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
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.
15 You should have received a copy of the GNU 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. */
69 #ifndef SCANDIR_CANCEL
70 # define SCANDIR_CANCEL
71 struct scandir_cancel_struct
80 cancel_handler (void *arg
)
82 struct scandir_cancel_struct
*cp
= arg
;
86 for (i
= 0; i
< cp
->cnt
; ++i
)
89 (void) __closedir (cp
->dp
);
97 SCANDIR (const char *dir
,
98 DIRENT_TYPE
***namelist
,
99 int (*select
) (const DIRENT_TYPE
*),
100 int (*cmp
) (const DIRENT_TYPE
**, const DIRENT_TYPE
**))
102 /* On OS/2 kLIBC, scandir() declaration is different from POSIX. See
103 <https://trac.netlabs.org/libc/browser/branches/libc-0.6/src/emx/include/dirent.h#L141>. */
104 SCANDIR (const char *dir
,
105 DIRENT_TYPE
***namelist
,
106 int (*select
) (DIRENT_TYPE
*),
107 int (*cmp
) (const void *, const void *))
110 DIR *dp
= __opendir (dir
);
111 DIRENT_TYPE
**v
= NULL
;
113 struct scandir_cancel_struct c
;
127 __libc_cleanup_push (cancel_handler
, &c
);
130 while ((d
= READDIR (dp
)) != NULL
)
132 int use_it
= select
== NULL
;
137 /* The select function might have changed errno. It was
138 zero before and it need to be again to make the latter
148 /* Ignore errors from select or readdir */
151 if (__builtin_expect (c
.cnt
== vsize
, 0))
158 new = (DIRENT_TYPE
**) realloc (v
, vsize
* sizeof (*v
));
165 dsize
= &d
->d_name
[_D_ALLOC_NAMLEN (d
)] - (char *) d
;
166 vnew
= (DIRENT_TYPE
*) malloc (dsize
);
170 v
[c
.cnt
++] = (DIRENT_TYPE
*) memcpy (vnew
, d
, dsize
);
174 if (__builtin_expect (errno
, 0) != 0)
185 /* Sort the list if we have a comparison function to sort with. */
187 qsort (v
, c
.cnt
, sizeof (*v
), (int (*) (const void *, const void *)) cmp
);
193 __libc_cleanup_pop (0);
196 (void) __closedir (dp
);