glob.c 13 KB

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