glob.c 12 KB

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