tst-gnuglob.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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.
  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.
  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.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17. #include <dirent.h>
  18. #include <errno.h>
  19. #include <error.h>
  20. #include <glob.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. // #define DEBUG
  26. #ifdef DEBUG
  27. # define PRINTF(fmt, args...) printf (fmt, ##args)
  28. #else
  29. # define PRINTF(fmt, args...)
  30. #endif
  31. #ifdef GLOB_ALTDIRFUNC
  32. static struct
  33. {
  34. const char *name;
  35. int level;
  36. int type;
  37. } filesystem[] =
  38. {
  39. { ".", 1, DT_DIR },
  40. { "..", 1, DT_DIR },
  41. { "file1lev1", 1, DT_REG },
  42. { "file2lev1", 1, DT_UNKNOWN },
  43. { "dir1lev1", 1, DT_UNKNOWN },
  44. { ".", 2, DT_DIR },
  45. { "..", 2, DT_DIR },
  46. { "file1lev2", 2, DT_REG },
  47. { "dir1lev2", 2, DT_DIR },
  48. { ".", 3, DT_DIR },
  49. { "..", 3, DT_DIR },
  50. { "dir2lev2", 2, DT_DIR },
  51. { ".", 3, DT_DIR },
  52. { "..", 3, DT_DIR },
  53. { ".foo", 3, DT_REG },
  54. { "dir1lev3", 3, DT_DIR },
  55. { ".", 4, DT_DIR },
  56. { "..", 4, DT_DIR },
  57. { "file1lev4", 4, DT_REG },
  58. { "file1lev3", 3, DT_REG },
  59. { "file2lev3", 3, DT_REG },
  60. { "file2lev2", 2, DT_REG },
  61. { "file3lev2", 2, DT_REG },
  62. { "dir3lev2", 2, DT_DIR },
  63. { ".", 3, DT_DIR },
  64. { "..", 3, DT_DIR },
  65. { "file3lev3", 3, DT_REG },
  66. { "file4lev3", 3, DT_REG },
  67. { "dir2lev1", 1, DT_DIR },
  68. { ".", 2, DT_DIR },
  69. { "..", 2, DT_DIR },
  70. { "dir1lev2", 2, DT_UNKNOWN },
  71. { ".", 3, DT_DIR },
  72. { "..", 3, DT_DIR },
  73. { ".foo", 3, DT_REG },
  74. { ".dir", 3, DT_DIR },
  75. { ".", 4, DT_DIR },
  76. { "..", 4, DT_DIR },
  77. { "hidden", 4, DT_REG }
  78. };
  79. #define nfiles (sizeof (filesystem) / sizeof (filesystem[0]))
  80. typedef struct
  81. {
  82. int level;
  83. int idx;
  84. struct dirent d;
  85. char room_for_dirent[NAME_MAX];
  86. } my_DIR;
  87. static long int
  88. find_file (const char *s)
  89. {
  90. int level = 1;
  91. long int idx = 0;
  92. if (strcmp (s, ".") == 0)
  93. return 0;
  94. if (s[0] == '.' && s[1] == '/')
  95. s += 2;
  96. while (*s != '\0')
  97. {
  98. char *endp = strchrnul (s, '/');
  99. PRINTF ("looking for %.*s, level %d\n", (int) (endp - s), s, level);
  100. while (idx < nfiles && filesystem[idx].level >= level)
  101. {
  102. if (filesystem[idx].level == level
  103. && memcmp (s, filesystem[idx].name, endp - s) == 0
  104. && filesystem[idx].name[endp - s] == '\0')
  105. break;
  106. ++idx;
  107. }
  108. if (idx == nfiles || filesystem[idx].level < level)
  109. {
  110. errno = ENOENT;
  111. return -1;
  112. }
  113. if (*endp == '\0')
  114. return idx + 1;
  115. if (filesystem[idx].type != DT_DIR
  116. && (idx + 1 >= nfiles
  117. || filesystem[idx].level >= filesystem[idx + 1].level))
  118. {
  119. errno = ENOTDIR;
  120. return -1;
  121. }
  122. ++idx;
  123. s = endp + 1;
  124. ++level;
  125. }
  126. errno = ENOENT;
  127. return -1;
  128. }
  129. static void *
  130. my_opendir (const char *s)
  131. {
  132. long int idx = find_file (s);
  133. my_DIR *dir;
  134. if (idx == -1)
  135. {
  136. PRINTF ("my_opendir(\"%s\") == NULL\n", s);
  137. return NULL;
  138. }
  139. dir = (my_DIR *) malloc (sizeof (my_DIR));
  140. if (dir == NULL)
  141. error (EXIT_FAILURE, errno, "cannot allocate directory handle");
  142. dir->level = filesystem[idx].level;
  143. dir->idx = idx;
  144. PRINTF ("my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
  145. s, filesystem[idx].level, idx);
  146. return dir;
  147. }
  148. static struct dirent *
  149. my_readdir (void *gdir)
  150. {
  151. my_DIR *dir = gdir;
  152. if (dir->idx == -1)
  153. {
  154. PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
  155. dir->level, (long int) dir->idx);
  156. return NULL;
  157. }
  158. while (dir->idx < nfiles && filesystem[dir->idx].level > dir->level)
  159. ++dir->idx;
  160. if (dir->idx == nfiles || filesystem[dir->idx].level < dir->level)
  161. {
  162. dir->idx = -1;
  163. PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
  164. dir->level, (long int) dir->idx);
  165. return NULL;
  166. }
  167. dir->d.d_ino = dir->idx;
  168. #ifdef _DIRENT_HAVE_D_TYPE
  169. dir->d.d_type = filesystem[dir->idx].type;
  170. #endif
  171. strcpy (dir->d.d_name, filesystem[dir->idx].name);
  172. #ifdef _DIRENT_HAVE_D_TYPE
  173. PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_type: %d, d_name: \"%s\" }\n",
  174. dir->level, (long int) dir->idx, dir->d.d_ino, dir->d.d_type,
  175. dir->d.d_name);
  176. #else
  177. PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_name: \"%s\" }\n",
  178. dir->level, (long int) dir->idx, dir->d.d_ino,
  179. dir->d.d_name);
  180. #endif
  181. ++dir->idx;
  182. return &dir->d;
  183. }
  184. static void
  185. my_closedir (void *dir)
  186. {
  187. PRINTF ("my_closedir ()\n");
  188. free (dir);
  189. }
  190. /* We use this function for lstat as well since we don't have any. */
  191. static int
  192. my_stat (const char *name, struct stat *st)
  193. {
  194. long int idx = find_file (name);
  195. if (idx == -1)
  196. {
  197. PRINTF ("my_stat (\"%s\", ...) = -1 (%s)\n", name, strerror (errno));
  198. return -1;
  199. }
  200. memset (st, '\0', sizeof (*st));
  201. if (filesystem[idx].type == DT_UNKNOWN)
  202. st->st_mode = DTTOIF (idx + 1 < nfiles
  203. && filesystem[idx].level < filesystem[idx + 1].level
  204. ? DT_DIR : DT_REG) | 0777;
  205. else
  206. st->st_mode = DTTOIF (filesystem[idx].type) | 0777;
  207. PRINTF ("my_stat (\"%s\", { st_mode: %o }) = 0\n", name, st->st_mode);
  208. return 0;
  209. }
  210. static const char *glob_errstring[] =
  211. {
  212. [GLOB_NOSPACE] = "out of memory",
  213. [GLOB_ABORTED] = "read error",
  214. [GLOB_NOMATCH] = "no matches found"
  215. };
  216. #define nglob_errstring (sizeof (glob_errstring) / sizeof (glob_errstring[0]))
  217. static const char *
  218. flagstr (int flags)
  219. {
  220. const char *strs[] =
  221. {
  222. "GLOB_ERR", "GLOB_MARK", "GLOB_NOSORT", "GLOB_DOOFSS", "GLOB_NOCHECK",
  223. "GLOB_APPEND", "GLOB_NOESCAPE", "GLOB_PERIOD", "GLOB_MAGCHAR",
  224. "GLOB_ALTDIRFUNC", "GLOB_BRACE", "GLOB_NOMAGIC", "GLOB_TILDE",
  225. "GLOB_ONLYDIR", "GLOB_TILDECHECK"
  226. };
  227. #define nstrs (sizeof (strs) / sizeof (strs[0]))
  228. static char buf[100];
  229. char *cp = buf;
  230. int cnt;
  231. for (cnt = 0; cnt < nstrs; ++cnt)
  232. if (flags & (1 << cnt))
  233. {
  234. flags &= ~(1 << cnt);
  235. if (cp != buf)
  236. *cp++ = '|';
  237. cp = stpcpy (cp, strs[cnt]);
  238. }
  239. if (flags != 0)
  240. {
  241. if (cp != buf)
  242. *cp++ = '|';
  243. sprintf (cp, "%#x", flags);
  244. }
  245. return buf;
  246. }
  247. static int
  248. test_result (const char *fmt, int flags, glob_t *gl, const char *str[])
  249. {
  250. size_t cnt;
  251. int result = 0;
  252. printf ("results for glob (\"%s\", %s)\n", fmt, flagstr (flags));
  253. for (cnt = 0; cnt < gl->gl_pathc && str[cnt] != NULL; ++cnt)
  254. {
  255. int ok = strcmp (gl->gl_pathv[cnt], str[cnt]) == 0;
  256. const char *errstr = "";
  257. if (! ok)
  258. {
  259. size_t inner;
  260. for (inner = 0; str[inner] != NULL; ++inner)
  261. if (strcmp (gl->gl_pathv[cnt], str[inner]) == 0)
  262. break;
  263. if (str[inner] == NULL)
  264. errstr = ok ? "" : " *** WRONG";
  265. else
  266. errstr = ok ? "" : " * wrong position";
  267. result = 1;
  268. }
  269. printf (" %s%s\n", gl->gl_pathv[cnt], errstr);
  270. }
  271. puts ("");
  272. if (str[cnt] != NULL || cnt < gl->gl_pathc)
  273. {
  274. puts (" *** incorrect number of entries");
  275. result = 1;
  276. }
  277. return result;
  278. }
  279. int
  280. main (void)
  281. {
  282. glob_t gl;
  283. int errval;
  284. int result = 0;
  285. const char *fmt;
  286. int flags;
  287. memset (&gl, '\0', sizeof (gl));
  288. gl.gl_closedir = my_closedir;
  289. gl.gl_readdir = my_readdir;
  290. gl.gl_opendir = my_opendir;
  291. gl.gl_lstat = my_stat;
  292. gl.gl_stat = my_stat;
  293. #define test(a, b, c...) \
  294. fmt = a; \
  295. flags = b; \
  296. errval = glob (fmt, flags, NULL, &gl); \
  297. if (errval != 0) \
  298. { \
  299. printf ("glob (\"%s\", %s) failed: %s\n", fmt, flagstr (flags), \
  300. errval >= 0 && errval < nglob_errstring \
  301. ? glob_errstring[errval] : "???"); \
  302. result = 1; \
  303. } \
  304. else \
  305. result |= test_result (fmt, flags, &gl, (const char *[]) { c, NULL })
  306. test ("*/*/*", GLOB_ALTDIRFUNC,
  307. "dir1lev1/dir2lev2/dir1lev3",
  308. "dir1lev1/dir2lev2/file1lev3",
  309. "dir1lev1/dir2lev2/file2lev3",
  310. "dir1lev1/dir3lev2/file3lev3",
  311. "dir1lev1/dir3lev2/file4lev3");
  312. test ("*/*/*", GLOB_ALTDIRFUNC | GLOB_PERIOD,
  313. "dir1lev1/dir1lev2/.",
  314. "dir1lev1/dir1lev2/..",
  315. "dir1lev1/dir2lev2/.",
  316. "dir1lev1/dir2lev2/..",
  317. "dir1lev1/dir2lev2/.foo",
  318. "dir1lev1/dir2lev2/dir1lev3",
  319. "dir1lev1/dir2lev2/file1lev3",
  320. "dir1lev1/dir2lev2/file2lev3",
  321. "dir1lev1/dir3lev2/.",
  322. "dir1lev1/dir3lev2/..",
  323. "dir1lev1/dir3lev2/file3lev3",
  324. "dir1lev1/dir3lev2/file4lev3",
  325. "dir2lev1/dir1lev2/.",
  326. "dir2lev1/dir1lev2/..",
  327. "dir2lev1/dir1lev2/.dir",
  328. "dir2lev1/dir1lev2/.foo");
  329. test ("*/*/.*", GLOB_ALTDIRFUNC,
  330. "dir1lev1/dir1lev2/.",
  331. "dir1lev1/dir1lev2/..",
  332. "dir1lev1/dir2lev2/.",
  333. "dir1lev1/dir2lev2/..",
  334. "dir1lev1/dir2lev2/.foo",
  335. "dir1lev1/dir3lev2/.",
  336. "dir1lev1/dir3lev2/..",
  337. "dir2lev1/dir1lev2/.",
  338. "dir2lev1/dir1lev2/..",
  339. "dir2lev1/dir1lev2/.dir",
  340. "dir2lev1/dir1lev2/.foo");
  341. test ("*1*/*2*/.*", GLOB_ALTDIRFUNC,
  342. "dir1lev1/dir1lev2/.",
  343. "dir1lev1/dir1lev2/..",
  344. "dir1lev1/dir2lev2/.",
  345. "dir1lev1/dir2lev2/..",
  346. "dir1lev1/dir2lev2/.foo",
  347. "dir1lev1/dir3lev2/.",
  348. "dir1lev1/dir3lev2/..",
  349. "dir2lev1/dir1lev2/.",
  350. "dir2lev1/dir1lev2/..",
  351. "dir2lev1/dir1lev2/.dir",
  352. "dir2lev1/dir1lev2/.foo");
  353. test ("*1*/*1*/.*", GLOB_ALTDIRFUNC,
  354. "dir1lev1/dir1lev2/.",
  355. "dir1lev1/dir1lev2/..",
  356. "dir2lev1/dir1lev2/.",
  357. "dir2lev1/dir1lev2/..",
  358. "dir2lev1/dir1lev2/.dir",
  359. "dir2lev1/dir1lev2/.foo");
  360. globfree (&gl);
  361. return result;
  362. }
  363. #else
  364. int main(void) { return 0; }
  365. #endif