glob.c 13 KB

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