1 /* Test the GNU extensions in glob which allow the user to provide callbacks
2 for the filesystem access functions.
3 Copyright (C) 2001 Free Software Foundation, Inc.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
34 # define PRINTF(fmt, args...) printf (fmt, ##args)
36 # define PRINTF(fmt, args...)
49 { "file1lev1", 1, DT_REG
},
50 { "file2lev1", 1, DT_UNKNOWN
},
51 { "dir1lev1", 1, DT_UNKNOWN
},
54 { "file1lev2", 2, DT_REG
},
55 { "dir1lev2", 2, DT_DIR
},
58 { "dir2lev2", 2, DT_DIR
},
61 { ".foo", 3, DT_REG
},
62 { "dir1lev3", 3, DT_DIR
},
65 { "file1lev4", 4, DT_REG
},
66 { "file1lev3", 3, DT_REG
},
67 { "file2lev3", 3, DT_REG
},
68 { "file2lev2", 2, DT_REG
},
69 { "file3lev2", 2, DT_REG
},
70 { "dir3lev2", 2, DT_DIR
},
73 { "file3lev3", 3, DT_REG
},
74 { "file4lev3", 3, DT_REG
},
75 { "dir2lev1", 1, DT_DIR
},
78 { "dir1lev2", 2, DT_UNKNOWN
},
81 { ".foo", 3, DT_REG
},
82 { ".dir", 3, DT_DIR
},
85 { "hidden", 4, DT_REG
}
87 #define nfiles (sizeof (filesystem) / sizeof (filesystem[0]))
99 find_file (const char *s
)
104 if (strcmp (s
, ".") == 0)
109 char *endp
= strchrnul (s
, '/');
111 PRINTF ("looking for %.*s, level %d\n", (int) (endp
- s
), s
, level
);
113 while (idx
< nfiles
&& filesystem
[idx
].level
>= level
)
115 if (filesystem
[idx
].level
== level
116 && memcmp (s
, filesystem
[idx
].name
, endp
- s
) == 0
117 && filesystem
[idx
].name
[endp
- s
] == '\0')
122 if (idx
== nfiles
|| filesystem
[idx
].level
< level
)
127 if (filesystem
[idx
].type
!= DT_DIR
128 && (idx
+ 1 >= nfiles
129 || filesystem
[idx
].level
>= filesystem
[idx
+ 1].level
))
150 my_opendir (const char *s
)
152 long int idx
= find_file (s
);
158 PRINTF ("my_opendir(\"%s\") == NULL\n", s
);
162 dir
= (my_DIR
*) malloc (sizeof (my_DIR
));
164 error (EXIT_FAILURE
, errno
, "cannot allocate directory handle");
166 dir
->level
= filesystem
[idx
].level
;
169 PRINTF ("my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
170 s
, filesystem
[idx
].level
, idx
);
176 static struct dirent
*
177 my_readdir (void *gdir
)
183 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
184 dir
->level
, (long int) dir
->idx
);
188 while (dir
->idx
< nfiles
&& filesystem
[dir
->idx
].level
> dir
->level
)
191 if (dir
->idx
== nfiles
|| filesystem
[dir
->idx
].level
< dir
->level
)
194 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
195 dir
->level
, (long int) dir
->idx
);
199 dir
->d
.d_ino
= dir
->idx
;
201 #ifdef _DIRENT_HAVE_D_TYPE
202 dir
->d
.d_type
= filesystem
[dir
->idx
].type
;
205 strcpy (dir
->d
.d_name
, filesystem
[dir
->idx
].name
);
207 #ifdef _DIRENT_HAVE_D_TYPE
208 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_type: %d, d_name: \"%s\" }\n",
209 dir
->level
, (long int) dir
->idx
, dir
->d
.d_ino
, dir
->d
.d_type
,
212 PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_name: \"%s\" }\n",
213 dir
->level
, (long int) dir
->idx
, dir
->d
.d_ino
,
224 my_closedir (void *dir
)
226 PRINTF ("my_closedir ()\n");
231 /* We use this function for lstat as well since we don't have any. */
233 my_stat (const char *name
, struct stat
*st
)
235 long int idx
= find_file (name
);
239 PRINTF ("my_stat (\"%s\", ...) = -1 (%s)\n", name
, strerror (errno
));
243 memset (st
, '\0', sizeof (*st
));
245 if (filesystem
[idx
].type
== DT_UNKNOWN
)
246 st
->st_mode
= DTTOIF (idx
+ 1 < nfiles
247 && filesystem
[idx
].level
< filesystem
[idx
+ 1].level
248 ? DT_DIR
: DT_REG
) | 0777;
250 st
->st_mode
= DTTOIF (filesystem
[idx
].type
) | 0777;
252 PRINTF ("my_stat (\"%s\", { st_mode: %o }) = 0\n", name
, st
->st_mode
);
258 static const char *glob_errstring
[] =
260 [GLOB_NOSPACE
] = "out of memory",
261 [GLOB_ABORTED
] = "read error",
262 [GLOB_NOMATCH
] = "no matches found"
264 #define nglob_errstring (sizeof (glob_errstring) / sizeof (glob_errstring[0]))
272 "GLOB_ERR", "GLOB_MARK", "GLOB_NOSORT", "GLOB_DOOFSS", "GLOB_NOCHECK",
273 "GLOB_APPEND", "GLOB_NOESCAPE", "GLOB_PERIOD", "GLOB_MAGCHAR",
274 "GLOB_ALTDIRFUNC", "GLOB_BRACE", "GLOB_NOMAGIC", "GLOB_TILDE",
275 "GLOB_ONLYDIR", "GLOB_TILDECHECK"
277 #define nstrs (sizeof (strs) / sizeof (strs[0]))
278 static char buf
[100];
282 for (cnt
= 0; cnt
< nstrs
; ++cnt
)
283 if (flags
& (1 << cnt
))
285 flags
&= ~(1 << cnt
);
288 cp
= stpcpy (cp
, strs
[cnt
]);
295 sprintf (cp
, "%#x", flags
);
303 test_result (const char *fmt
, int flags
, glob_t
*gl
, const char *str
[])
308 printf ("results for glob (\"%s\", %s)\n", fmt
, flagstr (flags
));
309 for (cnt
= 0; cnt
< gl
->gl_pathc
&& str
[cnt
] != NULL
; ++cnt
)
311 int ok
= strcmp (gl
->gl_pathv
[cnt
], str
[cnt
]) == 0;
312 const char *errstr
= "";
318 for (inner
= 0; str
[inner
] != NULL
; ++inner
)
319 if (strcmp (gl
->gl_pathv
[cnt
], str
[inner
]) == 0)
322 if (str
[inner
] == NULL
)
323 errstr
= ok
? "" : " *** WRONG";
325 errstr
= ok
? "" : " * wrong position";
330 printf (" %s%s\n", gl
->gl_pathv
[cnt
], errstr
);
334 if (str
[cnt
] != NULL
|| cnt
< gl
->gl_pathc
)
336 puts (" *** incorrect number of entries");
355 memset (&gl
, '\0', sizeof (gl
));
357 gl
.gl_closedir
= my_closedir
;
358 gl
.gl_readdir
= my_readdir
;
359 gl
.gl_opendir
= my_opendir
;
360 gl
.gl_lstat
= my_stat
;
361 gl
.gl_stat
= my_stat
;
363 #define test(a, b, c...) \
366 errval = glob (fmt, flags, NULL, &gl); \
369 printf ("glob (\"%s\", %s) failed: %s\n", fmt, flagstr (flags), \
370 errval >= 0 && errval < nglob_errstring \
371 ? glob_errstring[errval] : "???"); \
375 result |= test_result (fmt, flags, &gl, (const char *[]) { c, NULL })
377 test ("*/*/*", GLOB_ALTDIRFUNC
,
378 "dir1lev1/dir2lev2/dir1lev3",
379 "dir1lev1/dir2lev2/file1lev3",
380 "dir1lev1/dir2lev2/file2lev3",
381 "dir1lev1/dir3lev2/file3lev3",
382 "dir1lev1/dir3lev2/file4lev3");
384 test ("*/*/*", GLOB_ALTDIRFUNC
| GLOB_PERIOD
,
385 "dir1lev1/dir1lev2/.",
386 "dir1lev1/dir1lev2/..",
387 "dir1lev1/dir2lev2/.",
388 "dir1lev1/dir2lev2/..",
389 "dir1lev1/dir2lev2/.foo",
390 "dir1lev1/dir2lev2/dir1lev3",
391 "dir1lev1/dir2lev2/file1lev3",
392 "dir1lev1/dir2lev2/file2lev3",
393 "dir1lev1/dir3lev2/.",
394 "dir1lev1/dir3lev2/..",
395 "dir1lev1/dir3lev2/file3lev3",
396 "dir1lev1/dir3lev2/file4lev3",
397 "dir2lev1/dir1lev2/.",
398 "dir2lev1/dir1lev2/..",
399 "dir2lev1/dir1lev2/.dir",
400 "dir2lev1/dir1lev2/.foo");
402 test ("*/*/.*", GLOB_ALTDIRFUNC
,
403 "dir1lev1/dir1lev2/.",
404 "dir1lev1/dir1lev2/..",
405 "dir1lev1/dir2lev2/.",
406 "dir1lev1/dir2lev2/..",
407 "dir1lev1/dir2lev2/.foo",
408 "dir1lev1/dir3lev2/.",
409 "dir1lev1/dir3lev2/..",
410 "dir2lev1/dir1lev2/.",
411 "dir2lev1/dir1lev2/..",
412 "dir2lev1/dir1lev2/.dir",
413 "dir2lev1/dir1lev2/.foo");
415 test ("*1*/*2*/.*", GLOB_ALTDIRFUNC
,
416 "dir1lev1/dir1lev2/.",
417 "dir1lev1/dir1lev2/..",
418 "dir1lev1/dir2lev2/.",
419 "dir1lev1/dir2lev2/..",
420 "dir1lev1/dir2lev2/.foo",
421 "dir1lev1/dir3lev2/.",
422 "dir1lev1/dir3lev2/..",
423 "dir2lev1/dir1lev2/.",
424 "dir2lev1/dir1lev2/..",
425 "dir2lev1/dir1lev2/.dir",
426 "dir2lev1/dir1lev2/.foo");
428 test ("*1*/*1*/.*", GLOB_ALTDIRFUNC
,
429 "dir1lev1/dir1lev2/.",
430 "dir1lev1/dir1lev2/..",
431 "dir2lev1/dir1lev2/.",
432 "dir2lev1/dir1lev2/..",
433 "dir2lev1/dir1lev2/.dir",
434 "dir2lev1/dir1lev2/.foo");