glob.c 13 KB

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