Define wordsize.h macros everywhere
[glibc.git] / posix / bug-glob2.c
blob5873e08550e87cec921ce4a06891240af7431530
1 /* Test glob memory management.
2 for the filesystem access functions.
3 Copyright (C) 2001-2016 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <errno.h>
21 #include <error.h>
22 #include <dirent.h>
23 #include <glob.h>
24 #include <mcheck.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/stat.h>
30 // #define DEBUG
31 #ifdef DEBUG
32 # define PRINTF(fmt, args...) \
33 do \
34 { \
35 int save_errno = errno; \
36 printf (fmt, ##args); \
37 errno = save_errno; \
38 } while (0)
39 #else
40 # define PRINTF(fmt, args...)
41 #endif
43 #define LONG_NAME \
44 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
45 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
46 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
47 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
48 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
49 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
50 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
51 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
52 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
53 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
55 static struct
57 const char *name;
58 int level;
59 int type;
60 mode_t mode;
61 } filesystem[] =
63 { ".", 1, DT_DIR, 0755 },
64 { "..", 1, DT_DIR, 0755 },
65 { "dir", 1, DT_DIR, 0755 },
66 { ".", 2, DT_DIR, 0755 },
67 { "..", 2, DT_DIR, 0755 },
68 { "readable", 2, DT_DIR, 0755 },
69 { ".", 3, DT_DIR, 0755 },
70 { "..", 3, DT_DIR, 0755 },
71 { "a", 3, DT_REG, 0644 },
72 { LONG_NAME, 3, DT_REG, 0644 },
73 { "unreadable", 2, DT_DIR, 0111 },
74 { ".", 3, DT_DIR, 0111 },
75 { "..", 3, DT_DIR, 0755 },
76 { "a", 3, DT_REG, 0644 },
77 { "zz-readable", 2, DT_DIR, 0755 },
78 { ".", 3, DT_DIR, 0755 },
79 { "..", 3, DT_DIR, 0755 },
80 { "a", 3, DT_REG, 0644 }
82 #define nfiles (sizeof (filesystem) / sizeof (filesystem[0]))
85 typedef struct
87 int level;
88 int idx;
89 struct dirent d;
90 char room_for_dirent[sizeof (LONG_NAME)];
91 } my_DIR;
94 static long int
95 find_file (const char *s)
97 int level = 1;
98 long int idx = 0;
100 if (strcmp (s, ".") == 0)
101 return 0;
103 if (s[0] == '.' && s[1] == '/')
104 s += 2;
106 while (*s != '\0')
108 char *endp = strchrnul (s, '/');
110 PRINTF ("looking for %.*s, level %d\n", (int) (endp - s), s, level);
112 while (idx < nfiles && filesystem[idx].level >= level)
114 if (filesystem[idx].level == level
115 && memcmp (s, filesystem[idx].name, endp - s) == 0
116 && filesystem[idx].name[endp - s] == '\0')
117 break;
118 ++idx;
121 if (idx == nfiles || filesystem[idx].level < level)
123 errno = ENOENT;
124 return -1;
127 if (*endp == '\0')
128 return idx + 1;
130 if (filesystem[idx].type != DT_DIR
131 && (idx + 1 >= nfiles
132 || filesystem[idx].level >= filesystem[idx + 1].level))
134 errno = ENOTDIR;
135 return -1;
138 ++idx;
140 s = endp + 1;
141 ++level;
144 errno = ENOENT;
145 return -1;
149 static void *
150 my_opendir (const char *s)
152 long int idx = find_file (s);
153 my_DIR *dir;
155 if (idx == -1)
157 PRINTF ("my_opendir(\"%s\") == NULL (%m)\n", s);
158 return NULL;
161 if ((filesystem[idx].mode & 0400) == 0)
163 errno = EACCES;
164 PRINTF ("my_opendir(\"%s\") == NULL (%m)\n", s);
165 return NULL;
168 dir = (my_DIR *) malloc (sizeof (my_DIR));
169 if (dir == NULL)
171 printf ("cannot allocate directory handle: %m\n");
172 exit (EXIT_FAILURE);
175 dir->level = filesystem[idx].level;
176 dir->idx = idx;
178 PRINTF ("my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
179 s, filesystem[idx].level, idx);
181 return dir;
185 static struct dirent *
186 my_readdir (void *gdir)
188 my_DIR *dir = gdir;
190 if (dir->idx == -1)
192 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
193 dir->level, (long int) dir->idx);
194 return NULL;
197 while (dir->idx < nfiles && filesystem[dir->idx].level > dir->level)
198 ++dir->idx;
200 if (dir->idx == nfiles || filesystem[dir->idx].level < dir->level)
202 dir->idx = -1;
203 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
204 dir->level, (long int) dir->idx);
205 return NULL;
208 dir->d.d_ino = 1; /* glob should not skip this entry. */
210 #ifdef _DIRENT_HAVE_D_TYPE
211 dir->d.d_type = filesystem[dir->idx].type;
212 #endif
214 strcpy (dir->d.d_name, filesystem[dir->idx].name);
216 #ifdef _DIRENT_HAVE_D_TYPE
217 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_type: %d, d_name: \"%s\" }\n",
218 dir->level, (long int) dir->idx, dir->d.d_ino, dir->d.d_type,
219 dir->d.d_name);
220 #else
221 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_name: \"%s\" }\n",
222 dir->level, (long int) dir->idx, dir->d.d_ino,
223 dir->d.d_name);
224 #endif
226 ++dir->idx;
228 return &dir->d;
232 static void
233 my_closedir (void *dir)
235 PRINTF ("my_closedir ()\n");
236 free (dir);
240 /* We use this function for lstat as well since we don't have any. */
241 static int
242 my_stat (const char *name, struct stat *st)
244 long int idx = find_file (name);
246 if (idx == -1)
248 PRINTF ("my_stat (\"%s\", ...) = -1 (%m)\n", name);
249 return -1;
252 memset (st, '\0', sizeof (*st));
254 if (filesystem[idx].type == DT_UNKNOWN)
255 st->st_mode = DTTOIF (idx + 1 < nfiles
256 && filesystem[idx].level < filesystem[idx + 1].level
257 ? DT_DIR : DT_REG) | filesystem[idx].mode;
258 else
259 st->st_mode = DTTOIF (filesystem[idx].type) | filesystem[idx].mode;
261 PRINTF ("my_stat (\"%s\", { st_mode: %o }) = 0\n", name, st->st_mode);
263 return 0;
267 static void
268 init_glob_altdirfuncs (glob_t *pglob)
270 pglob->gl_closedir = my_closedir;
271 pglob->gl_readdir = my_readdir;
272 pglob->gl_opendir = my_opendir;
273 pglob->gl_lstat = my_stat;
274 pglob->gl_stat = my_stat;
279 do_test (void)
281 mtrace ();
283 glob_t gl;
284 memset (&gl, 0, sizeof (gl));
285 init_glob_altdirfuncs (&gl);
287 if (glob ("dir/*able/*", GLOB_ERR | GLOB_ALTDIRFUNC, NULL, &gl)
288 != GLOB_ABORTED)
290 puts ("glob did not fail with GLOB_ABORTED");
291 exit (EXIT_FAILURE);
294 globfree (&gl);
296 memset (&gl, 0, sizeof (gl));
297 init_glob_altdirfuncs (&gl);
299 gl.gl_offs = 3;
300 if (glob ("dir2/*", GLOB_DOOFFS, NULL, &gl) != GLOB_NOMATCH)
302 puts ("glob did not fail with GLOB_NOMATCH");
303 exit (EXIT_FAILURE);
306 globfree (&gl);
308 muntrace ();
310 return 0;
313 #define TEST_FUNCTION do_test ()
314 #include "../test-skeleton.c"