1 /* Copyright (C) 1992-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 /* We need to avoid the header declaration of scandir64, because
19 the types don't match scandir and then the compiler will
20 complain about the mismatch when we do the alias below. */
21 #define scandirat64 __renamed_scandirat64
30 #include <bits/libc-lock.h>
33 # define SCANDIRAT __scandirat
34 # define READDIR __readdir
35 # define DIRENT_TYPE struct dirent
36 # define SCANDIRAT_WEAK_ALIAS
39 #ifndef SKIP_SCANDIR_CANCEL
41 __scandir_cancel_handler (void *arg
)
43 struct scandir_cancel_struct
*cp
= arg
;
47 for (i
= 0; i
< cp
->cnt
; ++i
)
50 (void) __closedir (cp
->dp
);
56 SCANDIRAT (dfd
, dir
, namelist
, select
, cmp
)
59 DIRENT_TYPE
***namelist
;
60 int (*select
) (const DIRENT_TYPE
*);
61 int (*cmp
) (const DIRENT_TYPE
**, const DIRENT_TYPE
**);
63 DIR *dp
= __opendirat (dfd
, dir
);
64 DIRENT_TYPE
**v
= NULL
;
66 struct scandir_cancel_struct c
;
79 __libc_cleanup_push (__scandir_cancel_handler
, &c
);
81 while ((d
= READDIR (dp
)) != NULL
)
83 int use_it
= select
== NULL
;
88 /* The select function might have changed errno. It was
89 zero before and it need to be again to make the latter
99 /* Ignore errors from select or readdir */
102 if (__glibc_unlikely (c
.cnt
== vsize
))
109 new = (DIRENT_TYPE
**) realloc (v
, vsize
* sizeof (*v
));
116 dsize
= &d
->d_name
[_D_ALLOC_NAMLEN (d
)] - (char *) d
;
117 vnew
= (DIRENT_TYPE
*) malloc (dsize
);
121 v
[c
.cnt
++] = (DIRENT_TYPE
*) memcpy (vnew
, d
, dsize
);
125 if (__builtin_expect (errno
, 0) != 0)
136 /* Sort the list if we have a comparison function to sort with. */
138 qsort (v
, c
.cnt
, sizeof (*v
),
139 (int (*) (const void *, const void *)) cmp
);
144 __libc_cleanup_pop (0);
146 (void) __closedir (dp
);
151 libc_hidden_def (SCANDIRAT
)
152 #ifdef SCANDIRAT_WEAK_ALIAS
153 weak_alias (__scandirat
, scandirat
)
156 #ifdef _DIRENT_MATCHES_DIRENT64
157 weak_alias (scandirat
, scandirat64
)