Update.
[glibc.git] / posix / tst-gnuglob.c
blobb15dad12ef57fed74584a8d79446564268a2e312
1 /* Test the GNU extensions in glob which allow the user to provide callbacks
2 for the filesystem access functions.
3 Copyright (C) 2001-2002 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #include <dirent.h>
23 #include <errno.h>
24 #include <error.h>
25 #include <glob.h>
26 #include <mcheck.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/stat.h>
33 /* #define DEBUG */
34 #ifdef DEBUG
35 # define PRINTF(fmt, args...) printf (fmt, ##args)
36 #else
37 # define PRINTF(fmt, args...)
38 #endif
41 static struct
43 const char *name;
44 int level;
45 int type;
46 } filesystem[] =
48 { ".", 1, DT_DIR },
49 { "..", 1, DT_DIR },
50 { "file1lev1", 1, DT_REG },
51 { "file2lev1", 1, DT_UNKNOWN },
52 { "dir1lev1", 1, DT_UNKNOWN },
53 { ".", 2, DT_DIR },
54 { "..", 2, DT_DIR },
55 { "file1lev2", 2, DT_REG },
56 { "dir1lev2", 2, DT_DIR },
57 { ".", 3, DT_DIR },
58 { "..", 3, DT_DIR },
59 { "dir2lev2", 2, DT_DIR },
60 { ".", 3, DT_DIR },
61 { "..", 3, DT_DIR },
62 { ".foo", 3, DT_REG },
63 { "dir1lev3", 3, DT_DIR },
64 { ".", 4, DT_DIR },
65 { "..", 4, DT_DIR },
66 { "file1lev4", 4, DT_REG },
67 { "file1lev3", 3, DT_REG },
68 { "file2lev3", 3, DT_REG },
69 { "file2lev2", 2, DT_REG },
70 { "file3lev2", 2, DT_REG },
71 { "dir3lev2", 2, DT_DIR },
72 { ".", 3, DT_DIR },
73 { "..", 3, DT_DIR },
74 { "file3lev3", 3, DT_REG },
75 { "file4lev3", 3, DT_REG },
76 { "dir2lev1", 1, DT_DIR },
77 { ".", 2, DT_DIR },
78 { "..", 2, DT_DIR },
79 { "dir1lev2", 2, DT_UNKNOWN },
80 { ".", 3, DT_DIR },
81 { "..", 3, DT_DIR },
82 { ".foo", 3, DT_REG },
83 { ".dir", 3, DT_DIR },
84 { ".", 4, DT_DIR },
85 { "..", 4, DT_DIR },
86 { "hidden", 4, DT_REG }
88 #define nfiles (sizeof (filesystem) / sizeof (filesystem[0]))
91 typedef struct
93 int level;
94 int idx;
95 struct dirent d;
96 char room_for_dirent[NAME_MAX];
97 } my_DIR;
100 static long int
101 find_file (const char *s)
103 int level = 1;
104 long int idx = 0;
106 if (strcmp (s, ".") == 0)
107 return 0;
109 while (*s != '\0')
111 char *endp = strchrnul (s, '/');
113 PRINTF ("looking for %.*s, level %d\n", (int) (endp - s), s, level);
115 while (idx < nfiles && filesystem[idx].level >= level)
117 if (filesystem[idx].level == level
118 && memcmp (s, filesystem[idx].name, endp - s) == 0
119 && filesystem[idx].name[endp - s] == '\0')
120 break;
121 ++idx;
124 if (idx == nfiles || filesystem[idx].level < level)
126 errno = ENOENT;
127 return -1;
129 if (filesystem[idx].type != DT_DIR
130 && (idx + 1 >= nfiles
131 || filesystem[idx].level >= filesystem[idx + 1].level))
133 errno = ENOTDIR;
134 return -1;
137 ++idx;
139 if (*endp == '\0')
140 return idx;
142 s = endp + 1;
143 ++level;
146 errno = ENOENT;
147 return -1;
151 static void *
152 my_opendir (const char *s)
154 long int idx = find_file (s);
155 my_DIR *dir;
158 if (idx == -1)
160 PRINTF ("my_opendir(\"%s\") == NULL\n", s);
161 return NULL;
164 dir = (my_DIR *) malloc (sizeof (my_DIR));
165 if (dir == NULL)
166 error (EXIT_FAILURE, errno, "cannot allocate directory handle");
168 dir->level = filesystem[idx].level;
169 dir->idx = idx;
171 PRINTF ("my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
172 s, filesystem[idx].level, idx);
174 return dir;
178 static struct dirent *
179 my_readdir (void *gdir)
181 my_DIR *dir = gdir;
183 if (dir->idx == -1)
185 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
186 dir->level, (long int) dir->idx);
187 return NULL;
190 while (dir->idx < nfiles && filesystem[dir->idx].level > dir->level)
191 ++dir->idx;
193 if (dir->idx == nfiles || filesystem[dir->idx].level < dir->level)
195 dir->idx = -1;
196 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
197 dir->level, (long int) dir->idx);
198 return NULL;
201 dir->d.d_ino = dir->idx;
203 #ifdef _DIRENT_HAVE_D_TYPE
204 dir->d.d_type = filesystem[dir->idx].type;
205 #endif
207 strcpy (dir->d.d_name, filesystem[dir->idx].name);
209 #ifdef _DIRENT_HAVE_D_TYPE
210 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_type: %d, d_name: \"%s\" }\n",
211 dir->level, (long int) dir->idx, dir->d.d_ino, dir->d.d_type,
212 dir->d.d_name);
213 #else
214 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_name: \"%s\" }\n",
215 dir->level, (long int) dir->idx, dir->d.d_ino,
216 dir->d.d_name);
217 #endif
219 ++dir->idx;
221 return &dir->d;
225 static void
226 my_closedir (void *dir)
228 PRINTF ("my_closedir ()\n");
229 free (dir);
233 /* We use this function for lstat as well since we don't have any. */
234 static int
235 my_stat (const char *name, struct stat *st)
237 long int idx = find_file (name);
239 if (idx == -1)
241 PRINTF ("my_stat (\"%s\", ...) = -1 (%s)\n", name, strerror (errno));
242 return -1;
245 memset (st, '\0', sizeof (*st));
247 if (filesystem[idx].type == DT_UNKNOWN)
248 st->st_mode = DTTOIF (idx + 1 < nfiles
249 && filesystem[idx].level < filesystem[idx + 1].level
250 ? DT_DIR : DT_REG) | 0777;
251 else
252 st->st_mode = DTTOIF (filesystem[idx].type) | 0777;
254 PRINTF ("my_stat (\"%s\", { st_mode: %o }) = 0\n", name, st->st_mode);
256 return 0;
260 static const char *glob_errstring[] =
262 [GLOB_NOSPACE] = "out of memory",
263 [GLOB_ABORTED] = "read error",
264 [GLOB_NOMATCH] = "no matches found"
266 #define nglob_errstring (sizeof (glob_errstring) / sizeof (glob_errstring[0]))
269 static const char *
270 flagstr (int flags)
272 const char *strs[] =
274 "GLOB_ERR", "GLOB_MARK", "GLOB_NOSORT", "GLOB_DOOFSS", "GLOB_NOCHECK",
275 "GLOB_APPEND", "GLOB_NOESCAPE", "GLOB_PERIOD", "GLOB_MAGCHAR",
276 "GLOB_ALTDIRFUNC", "GLOB_BRACE", "GLOB_NOMAGIC", "GLOB_TILDE",
277 "GLOB_ONLYDIR", "GLOB_TILDECHECK"
279 #define nstrs (sizeof (strs) / sizeof (strs[0]))
280 static char buf[100];
281 char *cp = buf;
282 int cnt;
284 for (cnt = 0; cnt < nstrs; ++cnt)
285 if (flags & (1 << cnt))
287 flags &= ~(1 << cnt);
288 if (cp != buf)
289 *cp++ = '|';
290 cp = stpcpy (cp, strs[cnt]);
293 if (flags != 0)
295 if (cp != buf)
296 *cp++ = '|';
297 sprintf (cp, "%#x", flags);
300 return buf;
304 static int
305 test_result (const char *fmt, int flags, glob_t *gl, const char *str[])
307 size_t cnt;
308 int result = 0;
310 printf ("results for glob (\"%s\", %s)\n", fmt, flagstr (flags));
311 for (cnt = 0; cnt < gl->gl_pathc && str[cnt] != NULL; ++cnt)
313 int ok = strcmp (gl->gl_pathv[cnt], str[cnt]) == 0;
314 const char *errstr = "";
316 if (! ok)
318 size_t inner;
320 for (inner = 0; str[inner] != NULL; ++inner)
321 if (strcmp (gl->gl_pathv[cnt], str[inner]) == 0)
322 break;
324 if (str[inner] == NULL)
325 errstr = ok ? "" : " *** WRONG";
326 else
327 errstr = ok ? "" : " * wrong position";
329 result = 1;
332 printf (" %s%s\n", gl->gl_pathv[cnt], errstr);
334 puts ("");
336 if (str[cnt] != NULL || cnt < gl->gl_pathc)
338 puts (" *** incorrect number of entries");
339 result = 1;
342 return result;
347 main (void)
349 glob_t gl;
350 int errval;
351 int result = 0;
352 const char *fmt;
353 int flags;
355 mtrace ();
357 memset (&gl, '\0', sizeof (gl));
359 gl.gl_closedir = my_closedir;
360 gl.gl_readdir = my_readdir;
361 gl.gl_opendir = my_opendir;
362 gl.gl_lstat = my_stat;
363 gl.gl_stat = my_stat;
365 #define test(a, b, c...) \
366 fmt = a; \
367 flags = b; \
368 errval = glob (fmt, flags, NULL, &gl); \
369 if (errval != 0) \
371 printf ("glob (\"%s\", %s) failed: %s\n", fmt, flagstr (flags), \
372 errval >= 0 && errval < nglob_errstring \
373 ? glob_errstring[errval] : "???"); \
374 result = 1; \
376 else \
377 result |= test_result (fmt, flags, &gl, (const char *[]) { c, NULL })
379 test ("*/*/*", GLOB_ALTDIRFUNC,
380 "dir1lev1/dir2lev2/dir1lev3",
381 "dir1lev1/dir2lev2/file1lev3",
382 "dir1lev1/dir2lev2/file2lev3",
383 "dir1lev1/dir3lev2/file3lev3",
384 "dir1lev1/dir3lev2/file4lev3");
386 test ("*/*/*", GLOB_ALTDIRFUNC | GLOB_PERIOD,
387 "dir1lev1/dir1lev2/.",
388 "dir1lev1/dir1lev2/..",
389 "dir1lev1/dir2lev2/.",
390 "dir1lev1/dir2lev2/..",
391 "dir1lev1/dir2lev2/.foo",
392 "dir1lev1/dir2lev2/dir1lev3",
393 "dir1lev1/dir2lev2/file1lev3",
394 "dir1lev1/dir2lev2/file2lev3",
395 "dir1lev1/dir3lev2/.",
396 "dir1lev1/dir3lev2/..",
397 "dir1lev1/dir3lev2/file3lev3",
398 "dir1lev1/dir3lev2/file4lev3",
399 "dir2lev1/dir1lev2/.",
400 "dir2lev1/dir1lev2/..",
401 "dir2lev1/dir1lev2/.dir",
402 "dir2lev1/dir1lev2/.foo");
404 test ("*/*/.*", GLOB_ALTDIRFUNC,
405 "dir1lev1/dir1lev2/.",
406 "dir1lev1/dir1lev2/..",
407 "dir1lev1/dir2lev2/.",
408 "dir1lev1/dir2lev2/..",
409 "dir1lev1/dir2lev2/.foo",
410 "dir1lev1/dir3lev2/.",
411 "dir1lev1/dir3lev2/..",
412 "dir2lev1/dir1lev2/.",
413 "dir2lev1/dir1lev2/..",
414 "dir2lev1/dir1lev2/.dir",
415 "dir2lev1/dir1lev2/.foo");
417 test ("*1*/*2*/.*", GLOB_ALTDIRFUNC,
418 "dir1lev1/dir1lev2/.",
419 "dir1lev1/dir1lev2/..",
420 "dir1lev1/dir2lev2/.",
421 "dir1lev1/dir2lev2/..",
422 "dir1lev1/dir2lev2/.foo",
423 "dir1lev1/dir3lev2/.",
424 "dir1lev1/dir3lev2/..",
425 "dir2lev1/dir1lev2/.",
426 "dir2lev1/dir1lev2/..",
427 "dir2lev1/dir1lev2/.dir",
428 "dir2lev1/dir1lev2/.foo");
430 test ("*1*/*1*/.*", GLOB_ALTDIRFUNC,
431 "dir1lev1/dir1lev2/.",
432 "dir1lev1/dir1lev2/..",
433 "dir2lev1/dir1lev2/.",
434 "dir2lev1/dir1lev2/..",
435 "dir2lev1/dir1lev2/.dir",
436 "dir2lev1/dir1lev2/.foo");
438 globfree (&gl);
440 return result;