1 /* Template for tests of the GNU extension GLOB_ALTDIRFUNC.
2 Copyright (C) 2001-2023 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 <https://www.gnu.org/licenses/>. */
19 /* To use this skeleton, the following macros need to be defined
20 before inclusion of this file:
22 GLOB_FUNC The glob function to test (glob or glob64)
23 GLOB_TYPE The glob type expected by the function (glob_t, glob64_t)
24 GLOBFREE_FUNC The corresponding deallocation function
25 DIRENT_STRUCT The struct tag of the dirent type
26 STAT_STRUCT The struct tag of the stat type
38 #include <support/test-driver.h>
50 { "file1lev1", 1, DT_REG
},
51 { "file2lev1", 1, DT_UNKNOWN
},
52 { "dir1lev1", 1, DT_UNKNOWN
},
55 { "file1lev2", 2, DT_REG
},
56 { "dir1lev2", 2, DT_DIR
},
59 { "dir2lev2", 2, DT_DIR
},
62 { ".foo", 3, DT_REG
},
63 { "dir1lev3", 3, 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
},
74 { "file3lev3", 3, DT_REG
},
75 { "file4lev3", 3, DT_REG
},
76 { "dir2lev1", 1, DT_DIR
},
79 { "dir1lev2", 2, DT_UNKNOWN
},
82 { ".foo", 3, DT_REG
},
83 { ".dir", 3, DT_DIR
},
86 { "hidden", 4, DT_REG
}
88 #define nfiles (sizeof (filesystem) / sizeof (filesystem[0]))
95 struct DIRENT_STRUCT d
;
96 char room_for_dirent
[NAME_MAX
];
101 find_file (const char *s
)
116 if (strcmp (s
, ".") == 0)
119 if (s
[0] == '.' && s
[1] == '/')
124 char *endp
= strchrnul (s
, '/');
127 printf ("info: looking for %.*s, level %d\n",
128 (int) (endp
- s
), s
, level
);
130 while (idx
< nfiles
&& filesystem
[idx
].level
>= level
)
132 if (filesystem
[idx
].level
== level
133 && memcmp (s
, filesystem
[idx
].name
, endp
- s
) == 0
134 && filesystem
[idx
].name
[endp
- s
] == '\0')
139 if (idx
== nfiles
|| filesystem
[idx
].level
< level
)
148 if (filesystem
[idx
].type
!= DT_DIR
149 && (idx
+ 1 >= nfiles
150 || filesystem
[idx
].level
>= filesystem
[idx
+ 1].level
))
168 my_opendir (const char *s
)
170 long int idx
= find_file (s
);
174 if (idx
== -1 || filesystem
[idx
].type
!= DT_DIR
)
176 if (test_verbose
> 0)
177 printf ("info: my_opendir(\"%s\") == NULL\n", s
);
181 dir
= (my_DIR
*) malloc (sizeof (my_DIR
));
183 error (EXIT_FAILURE
, errno
, "cannot allocate directory handle");
185 dir
->level
= filesystem
[idx
].level
;
188 if (test_verbose
> 0)
189 printf ("info: my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
190 s
, filesystem
[idx
].level
, idx
);
196 static struct DIRENT_STRUCT
*
197 my_readdir (void *gdir
)
203 if (test_verbose
> 0)
204 printf ("info: my_readdir ({ level: %d, idx: %ld }) = NULL\n",
205 dir
->level
, (long int) dir
->idx
);
209 while (dir
->idx
< nfiles
&& filesystem
[dir
->idx
].level
> dir
->level
)
212 if (dir
->idx
== nfiles
|| filesystem
[dir
->idx
].level
< dir
->level
)
215 if (test_verbose
> 0)
216 printf ("info: my_readdir ({ level: %d, idx: %ld }) = NULL\n",
217 dir
->level
, (long int) dir
->idx
);
221 dir
->d
.d_ino
= 1; /* glob should not skip this entry. */
223 dir
->d
.d_type
= filesystem
[dir
->idx
].type
;
225 __strcpy_chk (dir
->d
.d_name
, filesystem
[dir
->idx
].name
, NAME_MAX
);
227 if (test_verbose
> 0)
228 printf ("info: my_readdir ({ level: %d, idx: %ld })"
229 " = { d_ino: %lld, d_type: %d, d_name: \"%s\" }\n",
230 dir
->level
, (long int) dir
->idx
,
231 (long long) dir
->d
.d_ino
, dir
->d
.d_type
,
241 my_closedir (void *dir
)
243 if (test_verbose
> 0)
244 printf ("info: my_closedir ()\n");
249 /* We use this function for lstat as well since we don't have any. */
251 my_stat (const char *name
, struct STAT_STRUCT
*st
)
253 long int idx
= find_file (name
);
257 if (test_verbose
> 0)
258 printf ("info: my_stat (\"%s\", ...) = -1 (%s)\n",
259 name
, strerror (errno
));
263 memset (st
, '\0', sizeof (*st
));
265 if (filesystem
[idx
].type
== DT_UNKNOWN
)
266 st
->st_mode
= DTTOIF (idx
+ 1 < nfiles
267 && filesystem
[idx
].level
< filesystem
[idx
+ 1].level
268 ? DT_DIR
: DT_REG
) | 0777;
270 st
->st_mode
= DTTOIF (filesystem
[idx
].type
) | 0777;
272 if (test_verbose
> 0)
273 printf ("info: my_stat (\"%s\", { st_mode: %o }) = 0\n", name
, st
->st_mode
);
279 static const char *glob_errstring
[] =
281 [GLOB_NOSPACE
] = "out of memory",
282 [GLOB_ABORTED
] = "read error",
283 [GLOB_NOMATCH
] = "no matches found"
285 #define nglob_errstring (sizeof (glob_errstring) / sizeof (glob_errstring[0]))
291 static const char *const strs
[] =
293 "GLOB_ERR", "GLOB_MARK", "GLOB_NOSORT", "GLOB_DOOFSS", "GLOB_NOCHECK",
294 "GLOB_APPEND", "GLOB_NOESCAPE", "GLOB_PERIOD", "GLOB_MAGCHAR",
295 "GLOB_ALTDIRFUNC", "GLOB_BRACE", "GLOB_NOMAGIC", "GLOB_TILDE",
296 "GLOB_ONLYDIR", "GLOB_TILDECHECK"
298 #define nstrs (sizeof (strs) / sizeof (strs[0]))
299 static char buf
[100];
303 for (cnt
= 0; cnt
< nstrs
; ++cnt
)
304 if (flags
& (1 << cnt
))
306 flags
&= ~(1 << cnt
);
309 cp
= stpcpy (cp
, strs
[cnt
]);
316 sprintf (cp
, "%#x", flags
);
327 static const char *const strs
[] =
329 [GLOB_NOSPACE
] = "GLOB_NOSPACE",
330 [GLOB_ABORTED
] = "GLOB_ABORTED",
331 [GLOB_NOMATCH
] = "GLOB_NOMATCH",
332 [GLOB_NOSYS
] = "GLOB_NOSYS"
334 #define nstrs (sizeof (strs) / sizeof (strs[0]))
335 static char buf
[100];
336 if (val
< 0 || val
>= nstrs
|| strs
[val
] == NULL
)
338 snprintf (buf
, sizeof (buf
), "GLOB_??? (%d)", val
);
347 test_result (const char *fmt
, int flags
, GLOB_TYPE
*gl
, const char *str
[])
352 printf ("results for glob (\"%s\", %s)\n", fmt
, flagstr (flags
));
353 for (cnt
= 0; cnt
< gl
->gl_pathc
&& str
[cnt
] != NULL
; ++cnt
)
355 int ok
= strcmp (gl
->gl_pathv
[cnt
], str
[cnt
]) == 0;
356 const char *errstr
= "";
362 for (inner
= 0; str
[inner
] != NULL
; ++inner
)
363 if (strcmp (gl
->gl_pathv
[cnt
], str
[inner
]) == 0)
366 if (str
[inner
] == NULL
)
367 errstr
= ok
? "" : " *** WRONG";
369 errstr
= ok
? "" : " * wrong position";
374 printf (" %s%s\n", gl
->gl_pathv
[cnt
], errstr
);
378 if (str
[cnt
] != NULL
|| cnt
< gl
->gl_pathc
)
380 puts (" *** incorrect number of entries");
399 memset (&gl
, '\0', sizeof (gl
));
401 gl
.gl_closedir
= my_closedir
;
402 gl
.gl_readdir
= my_readdir
;
403 gl
.gl_opendir
= my_opendir
;
404 gl
.gl_lstat
= my_stat
;
405 gl
.gl_stat
= my_stat
;
407 #define test(a, b, r, c...) \
409 flags = GLOB_ALTDIRFUNC | b; \
410 errval = GLOB_FUNC (fmt, flags, NULL, &gl); \
414 printf ("glob (\"%s\", %s) failed: %s\n", fmt, flagstr (flags), \
415 errval >= 0 && errval < nglob_errstring \
416 ? glob_errstring[errval] : "???"); \
418 printf ("glob (\"%s\", %s) did not fail\n", fmt, flagstr (flags)); \
422 result |= test_result (fmt, flags, &gl, (const char *[]) { c, NULL }); \
424 printf ("result for glob (\"%s\", %s) = %s\n\n", fmt, flagstr (flags), \
428 "dir1lev1/dir2lev2/dir1lev3",
429 "dir1lev1/dir2lev2/file1lev3",
430 "dir1lev1/dir2lev2/file2lev3",
431 "dir1lev1/dir3lev2/file3lev3",
432 "dir1lev1/dir3lev2/file4lev3");
434 test ("*/*/*", GLOB_PERIOD
, 0,
435 "dir1lev1/dir1lev2/.",
436 "dir1lev1/dir1lev2/..",
437 "dir1lev1/dir2lev2/.",
438 "dir1lev1/dir2lev2/..",
439 "dir1lev1/dir2lev2/.foo",
440 "dir1lev1/dir2lev2/dir1lev3",
441 "dir1lev1/dir2lev2/file1lev3",
442 "dir1lev1/dir2lev2/file2lev3",
443 "dir1lev1/dir3lev2/.",
444 "dir1lev1/dir3lev2/..",
445 "dir1lev1/dir3lev2/file3lev3",
446 "dir1lev1/dir3lev2/file4lev3",
447 "dir2lev1/dir1lev2/.",
448 "dir2lev1/dir1lev2/..",
449 "dir2lev1/dir1lev2/.dir",
450 "dir2lev1/dir1lev2/.foo");
452 test ("*/*/.*", 0, 0,
453 "dir1lev1/dir1lev2/.",
454 "dir1lev1/dir1lev2/..",
455 "dir1lev1/dir2lev2/.",
456 "dir1lev1/dir2lev2/..",
457 "dir1lev1/dir2lev2/.foo",
458 "dir1lev1/dir3lev2/.",
459 "dir1lev1/dir3lev2/..",
460 "dir2lev1/dir1lev2/.",
461 "dir2lev1/dir1lev2/..",
462 "dir2lev1/dir1lev2/.dir",
463 "dir2lev1/dir1lev2/.foo");
465 test ("*1*/*2*/.*", 0, 0,
466 "dir1lev1/dir1lev2/.",
467 "dir1lev1/dir1lev2/..",
468 "dir1lev1/dir2lev2/.",
469 "dir1lev1/dir2lev2/..",
470 "dir1lev1/dir2lev2/.foo",
471 "dir1lev1/dir3lev2/.",
472 "dir1lev1/dir3lev2/..",
473 "dir2lev1/dir1lev2/.",
474 "dir2lev1/dir1lev2/..",
475 "dir2lev1/dir1lev2/.dir",
476 "dir2lev1/dir1lev2/.foo");
478 test ("*1*/*1*/.*", 0, 0,
479 "dir1lev1/dir1lev2/.",
480 "dir1lev1/dir1lev2/..",
481 "dir2lev1/dir1lev2/.",
482 "dir2lev1/dir1lev2/..",
483 "dir2lev1/dir1lev2/.dir",
484 "dir2lev1/dir1lev2/.foo");
493 "dir1lev1/dir1lev2/",
494 "dir1lev1/dir2lev2/",
495 "dir1lev1/dir3lev2/",
496 "dir2lev1/dir1lev2/");
498 test ("", 0, GLOB_NOMATCH
, NULL
);
500 test ("", GLOB_NOCHECK
, 0, "");
507 #include <support/test-driver.c>