glob.c 13 KB

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