1 /* Copyright (C) 1992-1998,2000,2002,2003,2009,2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
23 #include <bits/libc-lock.h>
26 # define SCANDIRAT scandirat
27 # define READDIR __readdir
28 # define DIRENT_TYPE struct dirent
31 #ifndef SKIP_SCANDIR_CANCEL
33 __scandir_cancel_handler (void *arg
)
35 struct scandir_cancel_struct
*cp
= arg
;
39 for (i
= 0; i
< cp
->cnt
; ++i
)
42 (void) __closedir (cp
->dp
);
48 SCANDIRAT (dfd
, dir
, namelist
, select
, cmp
)
51 DIRENT_TYPE
***namelist
;
52 int (*select
) (const DIRENT_TYPE
*);
53 int (*cmp
) (const DIRENT_TYPE
**, const DIRENT_TYPE
**);
55 DIR *dp
= __opendirat (dfd
, dir
);
56 DIRENT_TYPE
**v
= NULL
;
58 struct scandir_cancel_struct c
;
71 __libc_cleanup_push (__scandir_cancel_handler
, &c
);
73 while ((d
= READDIR (dp
)) != NULL
)
75 int use_it
= select
== NULL
;
80 /* The select function might have changed errno. It was
81 zero before and it need to be again to make the latter
91 /* Ignore errors from select or readdir */
94 if (__builtin_expect (c
.cnt
== vsize
, 0))
101 new = (DIRENT_TYPE
**) realloc (v
, vsize
* sizeof (*v
));
108 dsize
= &d
->d_name
[_D_ALLOC_NAMLEN (d
)] - (char *) d
;
109 vnew
= (DIRENT_TYPE
*) malloc (dsize
);
113 v
[c
.cnt
++] = (DIRENT_TYPE
*) memcpy (vnew
, d
, dsize
);
117 if (__builtin_expect (errno
, 0) != 0)
128 /* Sort the list if we have a comparison function to sort with. */
130 qsort (v
, c
.cnt
, sizeof (*v
),
131 (int (*) (const void *, const void *)) cmp
);
136 __libc_cleanup_pop (0);
138 (void) __closedir (dp
);
143 libc_hidden_def (SCANDIRAT
)