]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/i386/scandir64.c
Clean up PLT use for scandirat
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / i386 / scandir64.c
1 /* Copyright (C) 2000, 2004, 2011 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
18
19 #include <dirent.h>
20
21 #define SCANDIR __scandir64
22 #define SCANDIRAT scandirat64
23 #define READDIR __readdir64
24 #define DIRENT_TYPE struct dirent64
25 #define SKIP_SCANDIR_CANCEL 1
26
27 #include <dirent/scandir.c>
28
29 #undef SCANDIR
30 #undef READDIR
31 #undef DIRENT_TYPE
32
33 #include <shlib-compat.h>
34
35 versioned_symbol (libc, __scandir64, scandir64, GLIBC_2_2);
36
37 #if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2)
38 # include <errno.h>
39 # include "olddirent.h"
40
41 int
42 __old_scandir64 (dir, namelist, select, cmp)
43 const char *dir;
44 struct __old_dirent64 ***namelist;
45 int (*select) (const struct __old_dirent64 *);
46 int (*cmp) (const struct __old_dirent64 **,
47 const struct __old_dirent64 **);
48 {
49 DIR *dp = __opendir (dir);
50 struct __old_dirent64 **v = NULL;
51 size_t vsize = 0;
52 struct scandir_cancel_struct c;
53 struct __old_dirent64 *d;
54 int save;
55
56 if (dp == NULL)
57 return -1;
58
59 save = errno;
60 __set_errno (0);
61
62 c.dp = dp;
63 c.v = NULL;
64 c.cnt = 0;
65 __libc_cleanup_push (__scandir_cancel_handler, &c);
66
67 while ((d = __old_readdir64 (dp)) != NULL)
68 {
69 int use_it = select == NULL;
70
71 if (! use_it)
72 {
73 use_it = select (d);
74 /* The select function might have changed errno. It was
75 zero before and it need to be again to make the latter
76 tests work. */
77 __set_errno (0);
78 }
79
80 if (use_it)
81 {
82 struct __old_dirent64 *vnew;
83 size_t dsize;
84
85 /* Ignore errors from select or readdir */
86 __set_errno (0);
87
88 if (__builtin_expect (c.cnt == vsize, 0))
89 {
90 struct __old_dirent64 **new;
91 if (vsize == 0)
92 vsize = 10;
93 else
94 vsize *= 2;
95 new = (struct __old_dirent64 **) realloc (v,
96 vsize * sizeof (*v));
97 if (new == NULL)
98 break;
99 v = new;
100 c.v = (void *) v;
101 }
102
103 dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
104 vnew = (struct __old_dirent64 *) malloc (dsize);
105 if (vnew == NULL)
106 break;
107
108 v[c.cnt++] = (struct __old_dirent64 *) memcpy (vnew, d, dsize);
109 }
110 }
111
112 if (__builtin_expect (errno, 0) != 0)
113 {
114 save = errno;
115
116 while (c.cnt > 0)
117 free (v[--c.cnt]);
118 free (v);
119 c.cnt = -1;
120 }
121 else
122 {
123 /* Sort the list if we have a comparison function to sort with. */
124 if (cmp != NULL)
125 qsort (v, c.cnt, sizeof (*v),
126 (int (*) (const void *, const void *)) cmp);
127
128 *namelist = v;
129 }
130
131 __libc_cleanup_pop (0);
132
133 (void) __closedir (dp);
134 __set_errno (save);
135
136 return c.cnt;
137 }
138 compat_symbol (libc, __old_scandir64, scandir64, GLIBC_2_1);
139
140 #endif