glob.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  2. This library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Library General Public License as
  4. published by the Free Software Foundation; either version 2 of the
  5. License, or (at your option) any later version.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Library General Public License for more details.
  10. You should have received a copy of the GNU Library General Public
  11. License along with this library; see the file COPYING.LIB. If
  12. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  13. Cambridge, MA 02139, USA. */
  14. #define strrchr __strrchr
  15. #define strcoll __strcoll
  16. #define qsort __qsort
  17. #define fnmatch __fnmatch
  18. #include <features.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include <dirent.h>
  26. #include <malloc.h>
  27. #include <fnmatch.h>
  28. #define _GNU_SOURCE
  29. #include <glob.h>
  30. extern DIR *__opendir (__const char *__name) __nonnull ((1)) attribute_hidden;
  31. extern int __closedir (DIR *__dirp) __nonnull ((1)) attribute_hidden;
  32. extern __ptr_t (*__glob_opendir_hook) __P ((const char *directory));
  33. extern void (*__glob_closedir_hook) __P ((__ptr_t stream));
  34. extern const char *(*__glob_readdir_hook) __P ((__ptr_t stream));
  35. static int glob_in_dir __P ((const char *pattern, const char *directory,
  36. int flags,
  37. int (*errfunc) __P ((const char *, int)),
  38. glob_t *pglob));
  39. static int prefix_array __P ((const char *prefix, char **array, size_t n,
  40. int add_slash));
  41. static int collated_compare __P ((const __ptr_t, const __ptr_t));
  42. #ifdef __GLOB64
  43. extern int __glob_pattern_p(const char *pattern, int quote) attribute_hidden;
  44. #else
  45. extern struct dirent *__readdir (DIR *__dirp) __nonnull ((1)) attribute_hidden;
  46. extern int __glob (__const char *__restrict __pattern, int __flags,
  47. int (*__errfunc) (__const char *, int),
  48. glob_t *__restrict __pglob) __THROW attribute_hidden;
  49. extern void __globfree (glob_t *__pglob) __THROW attribute_hidden;
  50. /* Return nonzero if PATTERN contains any metacharacters.
  51. Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
  52. int attribute_hidden __glob_pattern_p(const char *pattern, int quote)
  53. {
  54. const char *p;
  55. int open = 0;
  56. for (p = pattern; *p != '\0'; ++p)
  57. switch (*p)
  58. {
  59. case '?':
  60. case '*':
  61. return 1;
  62. case '\\':
  63. if (quote && p[1] != '\0')
  64. ++p;
  65. break;
  66. case '[':
  67. open = 1;
  68. break;
  69. case ']':
  70. if (open)
  71. return 1;
  72. break;
  73. }
  74. return 0;
  75. }
  76. strong_alias(__glob_pattern_p,glob_pattern_p)
  77. #endif
  78. /* Do glob searching for PATTERN, placing results in PGLOB.
  79. The bits defined above may be set in FLAGS.
  80. If a directory cannot be opened or read and ERRFUNC is not nil,
  81. it is called with the pathname that caused the error, and the
  82. `errno' value from the failing call; if it returns non-zero
  83. `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
  84. If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
  85. Otherwise, `glob' returns zero. */
  86. int attribute_hidden
  87. __glob (pattern, flags, errfunc, pglob)
  88. const char *pattern;
  89. int flags;
  90. int (*errfunc) __P ((const char *, int));
  91. glob_t *pglob;
  92. {
  93. const char *filename;
  94. char *dirname;
  95. size_t dirlen;
  96. int status;
  97. int oldcount;
  98. if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
  99. {
  100. errno = EINVAL;
  101. return -1;
  102. }
  103. /* Find the filename. */
  104. filename = strrchr (pattern, '/');
  105. if (filename == NULL)
  106. {
  107. filename = pattern;
  108. dirname = (char *) ".";
  109. dirlen = 0;
  110. }
  111. else if (filename == pattern)
  112. {
  113. /* "/pattern". */
  114. dirname = (char *) "/";
  115. dirlen = 1;
  116. ++filename;
  117. }
  118. else
  119. {
  120. dirlen = filename - pattern;
  121. dirname = (char *) alloca (dirlen + 1);
  122. __memcpy (dirname, pattern, dirlen);
  123. dirname[dirlen] = '\0';
  124. ++filename;
  125. }
  126. if (filename[0] == '\0' && dirlen > 1)
  127. /* "pattern/". Expand "pattern", appending slashes. */
  128. {
  129. int val = __glob (dirname, flags | GLOB_MARK, errfunc, pglob);
  130. if (val == 0)
  131. pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK);
  132. return val;
  133. }
  134. if (!(flags & GLOB_APPEND))
  135. {
  136. pglob->gl_pathc = 0;
  137. pglob->gl_pathv = NULL;
  138. }
  139. oldcount = pglob->gl_pathc;
  140. if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
  141. {
  142. /* The directory name contains metacharacters, so we
  143. have to glob for the directory, and then glob for
  144. the pattern in each directory found. */
  145. glob_t dirs;
  146. register int i;
  147. status = __glob (dirname,
  148. ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) |
  149. GLOB_NOSORT),
  150. errfunc, &dirs);
  151. if (status != 0)
  152. return status;
  153. /* We have successfully globbed the preceding directory name.
  154. For each name we found, call glob_in_dir on it and FILENAME,
  155. appending the results to PGLOB. */
  156. for (i = 0; i < dirs.gl_pathc; ++i)
  157. {
  158. int oldcount;
  159. #ifdef SHELL
  160. {
  161. /* Make globbing interruptible in the bash shell. */
  162. extern int interrupt_state;
  163. if (interrupt_state)
  164. {
  165. __globfree (&dirs);
  166. __globfree (&files);
  167. return GLOB_ABEND;
  168. }
  169. }
  170. #endif /* SHELL. */
  171. oldcount = pglob->gl_pathc;
  172. status = glob_in_dir (filename, dirs.gl_pathv[i],
  173. (flags | GLOB_APPEND) & ~GLOB_NOCHECK,
  174. errfunc, pglob);
  175. if (status == GLOB_NOMATCH)
  176. /* No matches in this directory. Try the next. */
  177. continue;
  178. if (status != 0)
  179. {
  180. __globfree (&dirs);
  181. __globfree (pglob);
  182. return status;
  183. }
  184. /* Stick the directory on the front of each name. */
  185. if (prefix_array (dirs.gl_pathv[i],
  186. &pglob->gl_pathv[oldcount],
  187. pglob->gl_pathc - oldcount,
  188. flags & GLOB_MARK))
  189. {
  190. __globfree (&dirs);
  191. __globfree (pglob);
  192. return GLOB_NOSPACE;
  193. }
  194. }
  195. flags |= GLOB_MAGCHAR;
  196. if (pglob->gl_pathc == oldcount)
  197. {
  198. /* No matches. */
  199. if (flags & GLOB_NOCHECK)
  200. {
  201. size_t len = __strlen (pattern) + 1;
  202. char *patcopy = (char *) malloc (len);
  203. if (patcopy == NULL)
  204. return GLOB_NOSPACE;
  205. __memcpy (patcopy, pattern, len);
  206. pglob->gl_pathv
  207. = (char **) realloc (pglob->gl_pathv,
  208. (pglob->gl_pathc +
  209. ((flags & GLOB_DOOFFS) ?
  210. pglob->gl_offs : 0) +
  211. 1 + 1) *
  212. sizeof (char *));
  213. if (pglob->gl_pathv == NULL)
  214. {
  215. free (patcopy);
  216. return GLOB_NOSPACE;
  217. }
  218. if (flags & GLOB_DOOFFS)
  219. while (pglob->gl_pathc < pglob->gl_offs)
  220. pglob->gl_pathv[pglob->gl_pathc++] = NULL;
  221. pglob->gl_pathv[pglob->gl_pathc++] = patcopy;
  222. pglob->gl_pathv[pglob->gl_pathc] = NULL;
  223. pglob->gl_flags = flags;
  224. }
  225. else
  226. {
  227. return GLOB_NOMATCH;
  228. }
  229. }
  230. }
  231. else
  232. {
  233. status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
  234. if (status != 0)
  235. return status;
  236. if (dirlen > 0)
  237. {
  238. /* Stick the directory on the front of each name. */
  239. if (prefix_array (dirname,
  240. &pglob->gl_pathv[oldcount],
  241. pglob->gl_pathc - oldcount,
  242. flags & GLOB_MARK))
  243. {
  244. __globfree (pglob);
  245. return GLOB_NOSPACE;
  246. }
  247. }
  248. }
  249. if (flags & GLOB_MARK)
  250. {
  251. /* Append slashes to directory names. glob_in_dir has already
  252. allocated the extra character for us. */
  253. int i;
  254. struct stat st;
  255. for (i = oldcount; i < pglob->gl_pathc; ++i)
  256. if (__lstat (pglob->gl_pathv[i], &st) == 0 &&
  257. S_ISDIR (st.st_mode))
  258. __strcat (pglob->gl_pathv[i], "/");
  259. }
  260. if (!(flags & GLOB_NOSORT))
  261. /* Sort the vector. */
  262. qsort ((__ptr_t) &pglob->gl_pathv[oldcount],
  263. pglob->gl_pathc - oldcount,
  264. sizeof (char *), (__compar_fn_t)collated_compare);
  265. return 0;
  266. }
  267. #ifdef __GLOB64
  268. strong_alias(__glob64,glob64)
  269. #else
  270. strong_alias(__glob,glob)
  271. #endif
  272. /* Free storage allocated in PGLOB by a previous `glob' call. */
  273. void attribute_hidden
  274. __globfree (pglob)
  275. register glob_t *pglob;
  276. {
  277. if (pglob->gl_pathv != NULL)
  278. {
  279. register int i = pglob->gl_flags & GLOB_DOOFFS? pglob->gl_offs : 0;
  280. for (; i < pglob->gl_pathc; ++i)
  281. if (pglob->gl_pathv[i] != NULL)
  282. free ((__ptr_t) pglob->gl_pathv[i]);
  283. free ((__ptr_t) pglob->gl_pathv);
  284. }
  285. }
  286. strong_alias(__globfree,globfree)
  287. /* Do a collated comparison of A and B. */
  288. static int
  289. collated_compare (a, b)
  290. const __ptr_t a;
  291. const __ptr_t b;
  292. {
  293. const char *const s1 = *(const char *const *) a;
  294. const char *const s2 = *(const char *const *) b;
  295. if (s1 == s2)
  296. return 0;
  297. if (s1 == NULL)
  298. return 1;
  299. if (s2 == NULL)
  300. return -1;
  301. return strcoll (s1, s2);
  302. }
  303. /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
  304. elements in place. Return nonzero if out of memory, zero if successful.
  305. A slash is inserted between DIRNAME and each elt of ARRAY,
  306. unless DIRNAME is just "/". Each old element of ARRAY is freed.
  307. If ADD_SLASH is non-zero, allocate one character more than
  308. necessary, so that a slash can be appended later. */
  309. static int
  310. prefix_array (dirname, array, n, add_slash)
  311. const char *dirname;
  312. char **array;
  313. size_t n;
  314. int add_slash;
  315. {
  316. register size_t i;
  317. size_t dirlen = __strlen (dirname);
  318. if (dirlen == 1 && dirname[0] == '/')
  319. /* DIRNAME is just "/", so normal prepending would get us "//foo".
  320. We want "/foo" instead, so don't prepend any chars from DIRNAME. */
  321. dirlen = 0;
  322. for (i = 0; i < n; ++i)
  323. {
  324. size_t eltlen = __strlen (array[i]) + 1;
  325. char *new = (char *) malloc (dirlen + 1 + eltlen + (add_slash ? 1 : 0));
  326. if (new == NULL)
  327. {
  328. while (i > 0)
  329. free ((__ptr_t) array[--i]);
  330. return 1;
  331. }
  332. __memcpy (new, dirname, dirlen);
  333. new[dirlen] = '/';
  334. __memcpy (&new[dirlen + 1], array[i], eltlen);
  335. free ((__ptr_t) array[i]);
  336. array[i] = new;
  337. }
  338. return 0;
  339. }
  340. /* Like `glob', but PATTERN is a final pathname component,
  341. and matches are searched for in DIRECTORY.
  342. The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
  343. The GLOB_APPEND flag is assumed to be set (always appends). */
  344. static int
  345. glob_in_dir (pattern, directory, flags, errfunc, pglob)
  346. const char *pattern;
  347. const char *directory;
  348. int flags;
  349. int (*errfunc) __P ((const char *, int));
  350. glob_t *pglob;
  351. {
  352. __ptr_t stream;
  353. struct globlink
  354. {
  355. struct globlink *next;
  356. char *name;
  357. };
  358. struct globlink *names = NULL;
  359. size_t nfound = 0;
  360. int meta;
  361. stream = (__glob_opendir_hook ? (*__glob_opendir_hook) (directory)
  362. : (__ptr_t) __opendir (directory));
  363. if (stream == NULL)
  364. {
  365. if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
  366. (flags & GLOB_ERR))
  367. return GLOB_ABORTED;
  368. }
  369. meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
  370. if (meta)
  371. flags |= GLOB_MAGCHAR;
  372. while (1)
  373. {
  374. const char *name;
  375. size_t len;
  376. if (__glob_readdir_hook)
  377. {
  378. name = (*__glob_readdir_hook) (stream);
  379. if (name == NULL)
  380. break;
  381. len = 0;
  382. }
  383. else
  384. {
  385. struct dirent *d = __readdir ((DIR *) stream);
  386. if (d == NULL)
  387. break;
  388. if (! (d->d_ino != 0))
  389. continue;
  390. name = d->d_name;
  391. #ifdef _DIRENT_HAVE_D_NAMLEN
  392. len = d->d_namlen;
  393. #else
  394. len = 0;
  395. #endif
  396. }
  397. if ((!meta && __strcmp (pattern, name) == 0)
  398. || fnmatch (pattern, name,
  399. (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) |
  400. ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0)
  401. {
  402. struct globlink *new
  403. = (struct globlink *) alloca (sizeof (struct globlink));
  404. if (len == 0)
  405. len = __strlen (name);
  406. new->name
  407. = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1);
  408. if (new->name == NULL)
  409. goto memory_error;
  410. __memcpy ((__ptr_t) new->name, name, len);
  411. new->name[len] = '\0';
  412. new->next = names;
  413. names = new;
  414. ++nfound;
  415. if (!meta)
  416. break;
  417. }
  418. }
  419. if (nfound == 0 && (flags & GLOB_NOCHECK))
  420. {
  421. size_t len = __strlen (pattern);
  422. nfound = 1;
  423. names = (struct globlink *) alloca (sizeof (struct globlink));
  424. names->next = NULL;
  425. names->name = (char *) malloc (len + (flags & GLOB_MARK ? 1 : 0) + 1);
  426. if (names->name == NULL)
  427. goto memory_error;
  428. __memcpy (names->name, pattern, len);
  429. names->name[len] = '\0';
  430. }
  431. pglob->gl_pathv
  432. = (char **) realloc (pglob->gl_pathv,
  433. (pglob->gl_pathc +
  434. ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) +
  435. nfound + 1) *
  436. sizeof (char *));
  437. if (pglob->gl_pathv == NULL)
  438. goto memory_error;
  439. if (flags & GLOB_DOOFFS)
  440. while (pglob->gl_pathc < pglob->gl_offs)
  441. pglob->gl_pathv[pglob->gl_pathc++] = NULL;
  442. for (; names != NULL; names = names->next)
  443. pglob->gl_pathv[pglob->gl_pathc++] = names->name;
  444. pglob->gl_pathv[pglob->gl_pathc] = NULL;
  445. pglob->gl_flags = flags;
  446. {
  447. int save = errno;
  448. if (__glob_closedir_hook)
  449. (*__glob_closedir_hook) (stream);
  450. else
  451. (void) __closedir ((DIR *) stream);
  452. errno = save;
  453. }
  454. return nfound == 0 ? GLOB_NOMATCH : 0;
  455. memory_error:
  456. {
  457. int save = errno;
  458. if (__glob_closedir_hook)
  459. (*__glob_closedir_hook) (stream);
  460. else
  461. (void) __closedir ((DIR *) stream);
  462. errno = save;
  463. }
  464. while (names != NULL)
  465. {
  466. if (names->name != NULL)
  467. free ((__ptr_t) names->name);
  468. names = names->next;
  469. }
  470. return GLOB_NOSPACE;
  471. }