glob.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. /* Copyright (C) 1991-2002,2003,2004,2005,2006 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #undef ENABLE_GLOB_BRACE_EXPANSION
  16. #undef ENABLE_GLOB_TILDE_EXPANSION
  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. #include <glob.h>
  28. libc_hidden_proto(closedir)
  29. libc_hidden_proto(fnmatch)
  30. libc_hidden_proto(memcpy)
  31. libc_hidden_proto(mempcpy)
  32. libc_hidden_proto(opendir)
  33. libc_hidden_proto(qsort)
  34. libc_hidden_proto(readdir)
  35. libc_hidden_proto(strchr)
  36. libc_hidden_proto(strcoll)
  37. libc_hidden_proto(strcpy)
  38. libc_hidden_proto(strdup)
  39. libc_hidden_proto(strlen)
  40. libc_hidden_proto(strrchr)
  41. #ifdef ENABLE_GLOB_TILDE_EXPANSION
  42. #include <pwd.h>
  43. libc_hidden_proto(getpwnam_r)
  44. #endif
  45. #ifdef COMPILE_GLOB64
  46. #undef stat
  47. #define stat stat64
  48. #define struct_stat64 struct stat64
  49. #define __stat64(fname, buf) stat64 (fname, buf)
  50. #define dirent dirent64
  51. #define __readdir readdir64
  52. #define __readdir64 readdir64
  53. #define glob_t glob64_t
  54. #define glob(pattern, flags, errfunc, pglob) glob64 (pattern, flags, errfunc, pglob)
  55. #define globfree(pglob) globfree64 (pglob)
  56. libc_hidden_proto(stat64)
  57. libc_hidden_proto(readdir64)
  58. #else
  59. #define __readdir readdir
  60. #ifdef __UCLIBC_HAS_LFS__
  61. #define __readdir64 readdir64
  62. libc_hidden_proto(readdir64)
  63. #else
  64. #define __readdir64 readdir
  65. #endif
  66. #define struct_stat64 struct stat
  67. #define __stat64(fname, buf) stat (fname, buf)
  68. libc_hidden_proto(stat)
  69. #endif
  70. /* When used in the GNU libc the symbol _DIRENT_HAVE_D_TYPE is available
  71. if the `d_type' member for `struct dirent' is available.
  72. HAVE_STRUCT_DIRENT_D_TYPE plays the same role in GNULIB. */
  73. #if defined _DIRENT_HAVE_D_TYPE
  74. /* True if the directory entry D must be of type T. */
  75. # define DIRENT_MUST_BE(d, t) ((d)->d_type == (t))
  76. /* True if the directory entry D might be a symbolic link. */
  77. # define DIRENT_MIGHT_BE_SYMLINK(d) \
  78. ((d)->d_type == DT_UNKNOWN || (d)->d_type == DT_LNK)
  79. /* True if the directory entry D might be a directory. */
  80. # define DIRENT_MIGHT_BE_DIR(d) \
  81. ((d)->d_type == DT_DIR || DIRENT_MIGHT_BE_SYMLINK (d))
  82. #else /* !HAVE_D_TYPE */
  83. # define DIRENT_MUST_BE(d, t) false
  84. # define DIRENT_MIGHT_BE_SYMLINK(d) true
  85. # define DIRENT_MIGHT_BE_DIR(d) true
  86. #endif /* HAVE_D_TYPE */
  87. # define NAMLEN(dirent) strlen((dirent)->d_name)
  88. #ifdef _D_NAMLEN
  89. # undef NAMLEN
  90. # define NAMLEN(d) _D_NAMLEN(d)
  91. #endif
  92. # if defined _DIRENT_HAVE_D_NAMLEN
  93. # define CONVERT_D_NAMLEN(d64, d32) (d64)->d_namlen = (d32)->d_namlen;
  94. # else
  95. # define CONVERT_D_NAMLEN(d64, d32)
  96. # endif
  97. # define CONVERT_D_INO(d64, d32) (d64)->d_ino = (d32)->d_ino;
  98. # ifdef _DIRENT_HAVE_D_TYPE
  99. # define CONVERT_D_TYPE(d64, d32) (d64)->d_type = (d32)->d_type;
  100. # else
  101. # define CONVERT_D_TYPE(d64, d32)
  102. # endif
  103. # define CONVERT_DIRENT_DIRENT64(d64, d32) \
  104. memcpy ((d64)->d_name, (d32)->d_name, NAMLEN (d32) + 1); \
  105. CONVERT_D_NAMLEN (d64, d32) \
  106. CONVERT_D_INO (d64, d32) \
  107. CONVERT_D_TYPE (d64, d32)
  108. extern int __collated_compare (const void *a, const void *b) attribute_hidden;
  109. extern int __prefix_array (const char *dirname, char **array, size_t n) attribute_hidden;
  110. #if defined ENABLE_GLOB_BRACE_EXPANSION
  111. extern const char *__next_brace_sub (const char *cp, int flags) attribute_hidden;
  112. #endif
  113. libc_hidden_proto(glob_pattern_p)
  114. #ifndef COMPILE_GLOB64
  115. /* Return nonzero if PATTERN contains any metacharacters.
  116. Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
  117. int glob_pattern_p(const char *pattern, int quote)
  118. {
  119. register const char *p;
  120. int open = 0;
  121. for (p = pattern; *p != '\0'; ++p)
  122. switch (*p)
  123. {
  124. case '?':
  125. case '*':
  126. return 1;
  127. case '\\':
  128. if (quote && p[1] != '\0')
  129. ++p;
  130. break;
  131. case '[':
  132. open = 1;
  133. break;
  134. case ']':
  135. if (open)
  136. return 1;
  137. break;
  138. }
  139. return 0;
  140. }
  141. libc_hidden_def(glob_pattern_p)
  142. /* Do a collated comparison of A and B. */
  143. int __collated_compare (const void *a, const void *b)
  144. {
  145. const char *const s1 = *(const char *const * const) a;
  146. const char *const s2 = *(const char *const * const) b;
  147. if (s1 == s2)
  148. return 0;
  149. if (s1 == NULL)
  150. return 1;
  151. if (s2 == NULL)
  152. return -1;
  153. return strcoll (s1, s2);
  154. }
  155. /* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's
  156. elements in place. Return nonzero if out of memory, zero if successful.
  157. A slash is inserted between DIRNAME and each elt of ARRAY,
  158. unless DIRNAME is just "/". Each old element of ARRAY is freed.
  159. If ADD_SLASH is non-zero, allocate one character more than
  160. necessary, so that a slash can be appended later. */
  161. int __prefix_array (const char *dirname, char **array, size_t n)
  162. {
  163. register size_t i;
  164. size_t dirlen = strlen (dirname);
  165. # define DIRSEP_CHAR '/'
  166. if (dirlen == 1 && dirname[0] == '/')
  167. /* DIRNAME is just "/", so normal prepending would get us "//foo".
  168. We want "/foo" instead, so don't prepend any chars from DIRNAME. */
  169. dirlen = 0;
  170. for (i = 0; i < n; ++i)
  171. {
  172. size_t eltlen = strlen (array[i]) + 1;
  173. char *new = (char *) malloc (dirlen + 1 + eltlen);
  174. if (new == NULL)
  175. {
  176. while (i > 0)
  177. free (array[--i]);
  178. return 1;
  179. }
  180. {
  181. char *endp = mempcpy (new, dirname, dirlen);
  182. *endp++ = DIRSEP_CHAR;
  183. mempcpy (endp, array[i], eltlen);
  184. }
  185. free (array[i]);
  186. array[i] = new;
  187. }
  188. return 0;
  189. }
  190. #if defined ENABLE_GLOB_BRACE_EXPANSION
  191. /* Find the end of the sub-pattern in a brace expression. */
  192. const char *
  193. __next_brace_sub (const char *cp, int flags)
  194. {
  195. unsigned int depth = 0;
  196. while (*cp != '\0')
  197. if ((flags & GLOB_NOESCAPE) == 0 && *cp == '\\')
  198. {
  199. if (*++cp == '\0')
  200. break;
  201. ++cp;
  202. }
  203. else
  204. {
  205. if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
  206. break;
  207. if (*cp++ == '{')
  208. depth++;
  209. }
  210. return *cp != '\0' ? cp : NULL;
  211. }
  212. #endif
  213. #endif
  214. static int
  215. link_exists_p (const char *dir, size_t dirlen, const char *fname,
  216. glob_t *pglob, int flags)
  217. {
  218. size_t fnamelen = strlen (fname);
  219. char *fullname = (char *) alloca (dirlen + 1 + fnamelen + 1);
  220. struct stat st;
  221. struct_stat64 st64;
  222. mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1),
  223. fname, fnamelen + 1);
  224. return (((flags & GLOB_ALTDIRFUNC)
  225. ? (*pglob->gl_stat) (fullname, &st)
  226. : __stat64 (fullname, &st64)) == 0);
  227. }
  228. /* Like `glob', but PATTERN is a final pathname component,
  229. and matches are searched for in DIRECTORY.
  230. The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done.
  231. The GLOB_APPEND flag is assumed to be set (always appends). */
  232. static int glob_in_dir (const char *pattern, const char *directory, int flags,
  233. int (*errfunc) (const char *, int),
  234. glob_t *pglob)
  235. {
  236. size_t dirlen = strlen (directory);
  237. void *stream = NULL;
  238. struct globlink
  239. {
  240. struct globlink *next;
  241. char *name;
  242. };
  243. struct globlink *names = NULL;
  244. size_t nfound;
  245. int meta;
  246. int save;
  247. meta = glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE));
  248. if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
  249. {
  250. /* We need not do any tests. The PATTERN contains no meta
  251. characters and we must not return an error therefore the
  252. result will always contain exactly one name. */
  253. flags |= GLOB_NOCHECK;
  254. nfound = 0;
  255. }
  256. else if (meta == 0 &&
  257. ((flags & GLOB_NOESCAPE) || strchr (pattern, '\\') == NULL))
  258. {
  259. /* Since we use the normal file functions we can also use stat()
  260. to verify the file is there. */
  261. struct stat st;
  262. struct_stat64 st64;
  263. size_t patlen = strlen (pattern);
  264. char *fullname = (char *) alloca (dirlen + 1 + patlen + 1);
  265. mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
  266. "/", 1),
  267. pattern, patlen + 1);
  268. if (((flags & GLOB_ALTDIRFUNC)
  269. ? (*pglob->gl_stat) (fullname, &st)
  270. : __stat64 (fullname, &st64)) == 0)
  271. /* We found this file to be existing. Now tell the rest
  272. of the function to copy this name into the result. */
  273. flags |= GLOB_NOCHECK;
  274. nfound = 0;
  275. }
  276. else
  277. {
  278. if (pattern[0] == '\0')
  279. {
  280. /* This is a special case for matching directories like in
  281. "*a/". */
  282. names = (struct globlink *) alloca (sizeof (struct globlink));
  283. names->name = (char *) malloc (1);
  284. if (names->name == NULL)
  285. goto memory_error;
  286. names->name[0] = '\0';
  287. names->next = NULL;
  288. nfound = 1;
  289. meta = 0;
  290. }
  291. else
  292. {
  293. stream = ((flags & GLOB_ALTDIRFUNC)
  294. ? (*pglob->gl_opendir) (directory)
  295. : opendir (directory));
  296. if (stream == NULL)
  297. {
  298. if (errno != ENOTDIR
  299. && ((errfunc != NULL && (*errfunc) (directory, errno))
  300. || (flags & GLOB_ERR)))
  301. return GLOB_ABORTED;
  302. nfound = 0;
  303. meta = 0;
  304. }
  305. else
  306. {
  307. int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0)
  308. | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
  309. );
  310. nfound = 0;
  311. flags |= GLOB_MAGCHAR;
  312. while (1)
  313. {
  314. const char *name;
  315. size_t len;
  316. #if !defined COMPILE_GLOB64
  317. struct dirent64 *d;
  318. union
  319. {
  320. struct dirent64 d64;
  321. char room [offsetof (struct dirent64, d_name[0])
  322. + NAME_MAX + 1];
  323. }
  324. d64buf;
  325. if (flags & GLOB_ALTDIRFUNC)
  326. {
  327. struct dirent *d32 = (*pglob->gl_readdir) (stream);
  328. if (d32 != NULL)
  329. {
  330. CONVERT_DIRENT_DIRENT64 (&d64buf.d64, d32);
  331. d = &d64buf.d64;
  332. }
  333. else
  334. d = NULL;
  335. }
  336. else
  337. d = __readdir64 (stream);
  338. #else
  339. struct dirent *d = ((flags & GLOB_ALTDIRFUNC)
  340. ? ((struct dirent *)
  341. (*pglob->gl_readdir) (stream))
  342. : __readdir (stream));
  343. #endif
  344. if (d == NULL)
  345. break;
  346. # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
  347. if (! REAL_DIR_ENTRY (d))
  348. continue;
  349. /* If we shall match only directories use the information
  350. provided by the dirent call if possible. */
  351. if ((flags & GLOB_ONLYDIR) && !DIRENT_MIGHT_BE_DIR (d))
  352. continue;
  353. name = d->d_name;
  354. if (fnmatch (pattern, name, fnm_flags) == 0)
  355. {
  356. /* If the file we found is a symlink we have to
  357. make sure the target file exists. */
  358. if (!DIRENT_MIGHT_BE_SYMLINK (d)
  359. || link_exists_p (directory, dirlen, name, pglob,
  360. flags))
  361. {
  362. struct globlink *new = (struct globlink *)
  363. alloca (sizeof (struct globlink));
  364. len = NAMLEN (d);
  365. new->name = (char *) malloc (len + 1);
  366. if (new->name == NULL)
  367. goto memory_error;
  368. *((char *) mempcpy (new->name, name, len)) = '\0';
  369. new->next = names;
  370. names = new;
  371. ++nfound;
  372. }
  373. }
  374. }
  375. }
  376. }
  377. }
  378. if (nfound == 0 && (flags & GLOB_NOCHECK))
  379. {
  380. size_t len = strlen (pattern);
  381. nfound = 1;
  382. names = (struct globlink *) alloca (sizeof (struct globlink));
  383. names->next = NULL;
  384. names->name = (char *) malloc (len + 1);
  385. if (names->name == NULL)
  386. goto memory_error;
  387. *((char *) mempcpy (names->name, pattern, len)) = '\0';
  388. }
  389. if (nfound != 0)
  390. {
  391. char **new_gl_pathv;
  392. new_gl_pathv
  393. = (char **) realloc (pglob->gl_pathv,
  394. (pglob->gl_pathc + pglob->gl_offs + nfound + 1)
  395. * sizeof (char *));
  396. if (new_gl_pathv == NULL)
  397. goto memory_error;
  398. pglob->gl_pathv = new_gl_pathv;
  399. for (; names != NULL; names = names->next)
  400. pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name;
  401. pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
  402. pglob->gl_flags = flags;
  403. }
  404. save = errno;
  405. if (stream != NULL)
  406. {
  407. if (flags & GLOB_ALTDIRFUNC)
  408. (*pglob->gl_closedir) (stream);
  409. else
  410. closedir (stream);
  411. }
  412. __set_errno (save);
  413. return nfound == 0 ? GLOB_NOMATCH : 0;
  414. memory_error:
  415. {
  416. int save2 = errno;
  417. if (flags & GLOB_ALTDIRFUNC)
  418. (*pglob->gl_closedir) (stream);
  419. else
  420. closedir (stream);
  421. __set_errno (save2);
  422. }
  423. while (names != NULL)
  424. {
  425. if (names->name != NULL)
  426. free (names->name);
  427. names = names->next;
  428. }
  429. return GLOB_NOSPACE;
  430. }
  431. #ifdef COMPILE_GLOB64
  432. libc_hidden_proto(glob64)
  433. libc_hidden_proto(globfree64)
  434. #else
  435. libc_hidden_proto(glob)
  436. libc_hidden_proto(globfree)
  437. #endif
  438. /* Do glob searching for PATTERN, placing results in PGLOB.
  439. The bits defined above may be set in FLAGS.
  440. If a directory cannot be opened or read and ERRFUNC is not nil,
  441. it is called with the pathname that caused the error, and the
  442. `errno' value from the failing call; if it returns non-zero
  443. `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
  444. If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
  445. Otherwise, `glob' returns zero. */
  446. int
  447. glob (pattern, flags, errfunc, pglob)
  448. const char *pattern;
  449. int flags;
  450. int (*errfunc) (const char *, int);
  451. glob_t *pglob;
  452. {
  453. const char *filename;
  454. const char *dirname;
  455. size_t dirlen;
  456. int status;
  457. size_t oldcount;
  458. if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
  459. {
  460. __set_errno (EINVAL);
  461. return -1;
  462. }
  463. if (!(flags & GLOB_DOOFFS))
  464. /* Have to do this so `globfree' knows where to start freeing. It
  465. also makes all the code that uses gl_offs simpler. */
  466. pglob->gl_offs = 0;
  467. #if defined ENABLE_GLOB_BRACE_EXPANSION
  468. if (flags & GLOB_BRACE)
  469. {
  470. const char *begin;
  471. if (flags & GLOB_NOESCAPE)
  472. begin = strchr (pattern, '{');
  473. else
  474. {
  475. begin = pattern;
  476. while (1)
  477. {
  478. if (*begin == '\0')
  479. {
  480. begin = NULL;
  481. break;
  482. }
  483. if (*begin == '\\' && begin[1] != '\0')
  484. ++begin;
  485. else if (*begin == '{')
  486. break;
  487. ++begin;
  488. }
  489. }
  490. if (begin != NULL)
  491. {
  492. /* Allocate working buffer large enough for our work. Note that
  493. we have at least an opening and closing brace. */
  494. size_t firstc;
  495. char *alt_start;
  496. const char *p;
  497. const char *next;
  498. const char *rest;
  499. size_t rest_len;
  500. char onealt[strlen (pattern) - 1];
  501. /* We know the prefix for all sub-patterns. */
  502. alt_start = mempcpy (onealt, pattern, begin - pattern);
  503. /* Find the first sub-pattern and at the same time find the
  504. rest after the closing brace. */
  505. next = __next_brace_sub (begin + 1, flags);
  506. if (next == NULL)
  507. {
  508. /* It is an illegal expression. */
  509. return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
  510. }
  511. /* Now find the end of the whole brace expression. */
  512. rest = next;
  513. while (*rest != '}')
  514. {
  515. rest = __next_brace_sub (rest + 1, flags);
  516. if (rest == NULL)
  517. {
  518. /* It is an illegal expression. */
  519. return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
  520. }
  521. }
  522. /* Please note that we now can be sure the brace expression
  523. is well-formed. */
  524. rest_len = strlen (++rest) + 1;
  525. /* We have a brace expression. BEGIN points to the opening {,
  526. NEXT points past the terminator of the first element, and END
  527. points past the final }. We will accumulate result names from
  528. recursive runs for each brace alternative in the buffer using
  529. GLOB_APPEND. */
  530. if (!(flags & GLOB_APPEND))
  531. {
  532. /* This call is to set a new vector, so clear out the
  533. vector so we can append to it. */
  534. pglob->gl_pathc = 0;
  535. pglob->gl_pathv = NULL;
  536. }
  537. firstc = pglob->gl_pathc;
  538. p = begin + 1;
  539. while (1)
  540. {
  541. int result;
  542. /* Construct the new glob expression. */
  543. mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
  544. result = glob (onealt,
  545. ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
  546. | GLOB_APPEND), errfunc, pglob);
  547. /* If we got an error, return it. */
  548. if (result && result != GLOB_NOMATCH)
  549. {
  550. if (!(flags & GLOB_APPEND))
  551. {
  552. globfree (pglob);
  553. pglob->gl_pathc = 0;
  554. }
  555. return result;
  556. }
  557. if (*next == '}')
  558. /* We saw the last entry. */
  559. break;
  560. p = next + 1;
  561. next = __next_brace_sub (p, flags);
  562. /* assert (next != NULL); */
  563. }
  564. if (pglob->gl_pathc != firstc)
  565. /* We found some entries. */
  566. return 0;
  567. else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
  568. return GLOB_NOMATCH;
  569. }
  570. }
  571. #endif
  572. /* Find the filename. */
  573. filename = strrchr (pattern, '/');
  574. if (filename == NULL)
  575. {
  576. /* This can mean two things: a simple name or "~name". The latter
  577. case is nothing but a notation for a directory. */
  578. if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
  579. {
  580. dirname = pattern;
  581. dirlen = strlen (pattern);
  582. /* Set FILENAME to NULL as a special flag. This is ugly but
  583. other solutions would require much more code. We test for
  584. this special case below. */
  585. filename = NULL;
  586. }
  587. else
  588. {
  589. filename = pattern;
  590. dirname = ".";
  591. dirlen = 0;
  592. }
  593. }
  594. else if (filename == pattern)
  595. {
  596. /* "/pattern". */
  597. dirname = "/";
  598. dirlen = 1;
  599. ++filename;
  600. }
  601. else
  602. {
  603. char *newp;
  604. dirlen = filename - pattern;
  605. newp = (char *) alloca (dirlen + 1);
  606. *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
  607. dirname = newp;
  608. ++filename;
  609. if (filename[0] == '\0'
  610. && dirlen > 1)
  611. /* "pattern/". Expand "pattern", appending slashes. */
  612. {
  613. int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
  614. if (val == 0)
  615. pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
  616. | (flags & GLOB_MARK));
  617. return val;
  618. }
  619. }
  620. if (!(flags & GLOB_APPEND))
  621. {
  622. pglob->gl_pathc = 0;
  623. if (!(flags & GLOB_DOOFFS))
  624. pglob->gl_pathv = NULL;
  625. else
  626. {
  627. size_t i;
  628. pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1)
  629. * sizeof (char *));
  630. if (pglob->gl_pathv == NULL)
  631. return GLOB_NOSPACE;
  632. for (i = 0; i <= pglob->gl_offs; ++i)
  633. pglob->gl_pathv[i] = NULL;
  634. }
  635. }
  636. oldcount = pglob->gl_pathc + pglob->gl_offs;
  637. #if defined ENABLE_GLOB_TILDE_EXPANSION
  638. if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
  639. {
  640. if (dirname[1] == '\0' || dirname[1] == '/')
  641. {
  642. /* Look up home directory. */
  643. const char *home_dir = getenv ("HOME");
  644. if (home_dir == NULL || home_dir[0] == '\0')
  645. {
  646. int success;
  647. char *name;
  648. # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
  649. size_t buflen = GET_LOGIN_NAME_MAX () + 1;
  650. if (buflen == 0)
  651. /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
  652. a moderate value. */
  653. buflen = 20;
  654. name = (char *) alloca (buflen);
  655. success = getlogin_r (name, buflen) == 0;
  656. if (success)
  657. {
  658. struct passwd *p;
  659. # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
  660. long int pwbuflen = GETPW_R_SIZE_MAX ();
  661. char *pwtmpbuf;
  662. struct passwd pwbuf;
  663. int save = errno;
  664. pwtmpbuf = (char *) alloca (pwbuflen);
  665. while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
  666. != 0)
  667. {
  668. if (errno != ERANGE)
  669. {
  670. p = NULL;
  671. break;
  672. }
  673. pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
  674. 2 * pwbuflen);
  675. __set_errno (save);
  676. }
  677. if (p != NULL)
  678. home_dir = p->pw_dir;
  679. }
  680. }
  681. if (home_dir == NULL || home_dir[0] == '\0')
  682. {
  683. if (flags & GLOB_TILDE_CHECK)
  684. return GLOB_NOMATCH;
  685. else
  686. home_dir = "~"; /* No luck. */
  687. }
  688. /* Now construct the full directory. */
  689. if (dirname[1] == '\0')
  690. dirname = home_dir;
  691. else
  692. {
  693. char *newp;
  694. size_t home_len = strlen (home_dir);
  695. newp = (char *) alloca (home_len + dirlen);
  696. mempcpy (mempcpy (newp, home_dir, home_len),
  697. &dirname[1], dirlen);
  698. dirname = newp;
  699. }
  700. }
  701. else
  702. {
  703. char *end_name = strchr (dirname, '/');
  704. const char *user_name;
  705. const char *home_dir;
  706. if (end_name == NULL)
  707. user_name = dirname + 1;
  708. else
  709. {
  710. char *newp;
  711. newp = (char *) alloca (end_name - dirname);
  712. *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
  713. = '\0';
  714. user_name = newp;
  715. }
  716. /* Look up specific user's home directory. */
  717. {
  718. struct passwd *p;
  719. long int buflen = GETPW_R_SIZE_MAX ();
  720. char *pwtmpbuf;
  721. struct passwd pwbuf;
  722. int save = errno;
  723. pwtmpbuf = (char *) alloca (buflen);
  724. while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
  725. {
  726. if (errno != ERANGE)
  727. {
  728. p = NULL;
  729. break;
  730. }
  731. pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
  732. __set_errno (save);
  733. }
  734. if (p != NULL)
  735. home_dir = p->pw_dir;
  736. else
  737. home_dir = NULL;
  738. }
  739. /* If we found a home directory use this. */
  740. if (home_dir != NULL)
  741. {
  742. char *newp;
  743. size_t home_len = strlen (home_dir);
  744. size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
  745. newp = (char *) alloca (home_len + rest_len + 1);
  746. *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
  747. end_name, rest_len)) = '\0';
  748. dirname = newp;
  749. }
  750. else
  751. if (flags & GLOB_TILDE_CHECK)
  752. /* We have to regard it as an error if we cannot find the
  753. home directory. */
  754. return GLOB_NOMATCH;
  755. }
  756. }
  757. /* Now test whether we looked for "~" or "~NAME". In this case we
  758. can give the answer now. */
  759. if (filename == NULL)
  760. {
  761. struct stat st;
  762. struct_stat64 st64;
  763. /* Return the directory if we don't check for error or if it exists. */
  764. if ((flags & GLOB_NOCHECK)
  765. || (((flags & GLOB_ALTDIRFUNC)
  766. ? ((*pglob->gl_stat) (dirname, &st) == 0
  767. && S_ISDIR (st.st_mode))
  768. : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
  769. {
  770. int newcount = pglob->gl_pathc + pglob->gl_offs;
  771. char **new_gl_pathv;
  772. new_gl_pathv
  773. = (char **) realloc (pglob->gl_pathv,
  774. (newcount + 1 + 1) * sizeof (char *));
  775. if (new_gl_pathv == NULL)
  776. {
  777. nospace:
  778. free (pglob->gl_pathv);
  779. pglob->gl_pathv = NULL;
  780. pglob->gl_pathc = 0;
  781. return GLOB_NOSPACE;
  782. }
  783. pglob->gl_pathv = new_gl_pathv;
  784. pglob->gl_pathv[newcount] = strdup (dirname);
  785. if (pglob->gl_pathv[newcount] == NULL)
  786. goto nospace;
  787. pglob->gl_pathv[++newcount] = NULL;
  788. ++pglob->gl_pathc;
  789. pglob->gl_flags = flags;
  790. return 0;
  791. }
  792. /* Not found. */
  793. return GLOB_NOMATCH;
  794. }
  795. #endif
  796. if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
  797. {
  798. /* The directory name contains metacharacters, so we
  799. have to glob for the directory, and then glob for
  800. the pattern in each directory found. */
  801. glob_t dirs;
  802. size_t i;
  803. if ((flags & GLOB_ALTDIRFUNC) != 0)
  804. {
  805. /* Use the alternative access functions also in the recursive
  806. call. */
  807. dirs.gl_opendir = pglob->gl_opendir;
  808. dirs.gl_readdir = pglob->gl_readdir;
  809. dirs.gl_closedir = pglob->gl_closedir;
  810. dirs.gl_stat = pglob->gl_stat;
  811. dirs.gl_lstat = pglob->gl_lstat;
  812. }
  813. status = glob (dirname,
  814. ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
  815. | GLOB_ALTDIRFUNC))
  816. | GLOB_NOSORT | GLOB_ONLYDIR),
  817. errfunc, &dirs);
  818. if (status != 0)
  819. return status;
  820. /* We have successfully globbed the preceding directory name.
  821. For each name we found, call glob_in_dir on it and FILENAME,
  822. appending the results to PGLOB. */
  823. for (i = 0; i < dirs.gl_pathc; ++i)
  824. {
  825. int old_pathc;
  826. old_pathc = pglob->gl_pathc;
  827. status = glob_in_dir (filename, dirs.gl_pathv[i],
  828. ((flags | GLOB_APPEND)
  829. & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
  830. errfunc, pglob);
  831. if (status == GLOB_NOMATCH)
  832. /* No matches in this directory. Try the next. */
  833. continue;
  834. if (status != 0)
  835. {
  836. globfree (&dirs);
  837. globfree (pglob);
  838. pglob->gl_pathc = 0;
  839. return status;
  840. }
  841. /* Stick the directory on the front of each name. */
  842. if (__prefix_array (dirs.gl_pathv[i],
  843. &pglob->gl_pathv[old_pathc + pglob->gl_offs],
  844. pglob->gl_pathc - old_pathc))
  845. {
  846. globfree (&dirs);
  847. globfree (pglob);
  848. pglob->gl_pathc = 0;
  849. return GLOB_NOSPACE;
  850. }
  851. }
  852. flags |= GLOB_MAGCHAR;
  853. /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
  854. But if we have not found any matching entry and the GLOB_NOCHECK
  855. flag was set we must return the input pattern itself. */
  856. if (pglob->gl_pathc + pglob->gl_offs == oldcount)
  857. {
  858. /* No matches. */
  859. if (flags & GLOB_NOCHECK)
  860. {
  861. int newcount = pglob->gl_pathc + pglob->gl_offs;
  862. char **new_gl_pathv;
  863. new_gl_pathv = (char **) realloc (pglob->gl_pathv,
  864. (newcount + 2)
  865. * sizeof (char *));
  866. if (new_gl_pathv == NULL)
  867. {
  868. globfree (&dirs);
  869. return GLOB_NOSPACE;
  870. }
  871. pglob->gl_pathv = new_gl_pathv;
  872. pglob->gl_pathv[newcount] = strdup (pattern);
  873. if (pglob->gl_pathv[newcount] == NULL)
  874. {
  875. globfree (&dirs);
  876. globfree (pglob);
  877. pglob->gl_pathc = 0;
  878. return GLOB_NOSPACE;
  879. }
  880. ++pglob->gl_pathc;
  881. ++newcount;
  882. pglob->gl_pathv[newcount] = NULL;
  883. pglob->gl_flags = flags;
  884. }
  885. else
  886. {
  887. globfree (&dirs);
  888. return GLOB_NOMATCH;
  889. }
  890. }
  891. globfree (&dirs);
  892. }
  893. else
  894. {
  895. int old_pathc = pglob->gl_pathc;
  896. status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
  897. if (status != 0)
  898. return status;
  899. if (dirlen > 0)
  900. {
  901. /* Stick the directory on the front of each name. */
  902. if (__prefix_array (dirname,
  903. &pglob->gl_pathv[old_pathc + pglob->gl_offs],
  904. pglob->gl_pathc - old_pathc))
  905. {
  906. globfree (pglob);
  907. pglob->gl_pathc = 0;
  908. return GLOB_NOSPACE;
  909. }
  910. }
  911. }
  912. if (flags & GLOB_MARK)
  913. {
  914. /* Append slashes to directory names. */
  915. size_t i;
  916. struct stat st;
  917. struct_stat64 st64;
  918. for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i)
  919. if (((flags & GLOB_ALTDIRFUNC)
  920. ? ((*pglob->gl_stat) (pglob->gl_pathv[i], &st) == 0
  921. && S_ISDIR (st.st_mode))
  922. : (__stat64 (pglob->gl_pathv[i], &st64) == 0
  923. && S_ISDIR (st64.st_mode))))
  924. {
  925. size_t len = strlen (pglob->gl_pathv[i]) + 2;
  926. char *new = realloc (pglob->gl_pathv[i], len);
  927. if (new == NULL)
  928. {
  929. globfree (pglob);
  930. pglob->gl_pathc = 0;
  931. return GLOB_NOSPACE;
  932. }
  933. strcpy (&new[len - 2], "/");
  934. pglob->gl_pathv[i] = new;
  935. }
  936. }
  937. if (!(flags & GLOB_NOSORT))
  938. {
  939. /* Sort the vector. */
  940. qsort (&pglob->gl_pathv[oldcount],
  941. pglob->gl_pathc + pglob->gl_offs - oldcount,
  942. sizeof (char *), __collated_compare);
  943. }
  944. return 0;
  945. }
  946. #ifdef COMPILE_GLOB64
  947. libc_hidden_def(glob64)
  948. #else
  949. libc_hidden_def(glob)
  950. #endif
  951. /* Free storage allocated in PGLOB by a previous `glob' call. */
  952. void
  953. globfree (pglob)
  954. register glob_t *pglob;
  955. {
  956. if (pglob->gl_pathv != NULL)
  957. {
  958. size_t i;
  959. for (i = 0; i < pglob->gl_pathc; ++i)
  960. if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
  961. free (pglob->gl_pathv[pglob->gl_offs + i]);
  962. free (pglob->gl_pathv);
  963. pglob->gl_pathv = NULL;
  964. }
  965. }
  966. #ifdef COMPILE_GLOB64
  967. libc_hidden_def(globfree64)
  968. #else
  969. libc_hidden_def(globfree)
  970. #endif