glob.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. /* Experimentally off - libc_hidden_proto(memcpy) */
  31. /* Experimentally off - libc_hidden_proto(mempcpy) */
  32. /* libc_hidden_proto(opendir) */
  33. /* libc_hidden_proto(qsort) */
  34. /* libc_hidden_proto(readdir) */
  35. /* Experimentally off - libc_hidden_proto(strchr) */
  36. /* Experimentally off - libc_hidden_proto(strcoll) */
  37. /* Experimentally off - libc_hidden_proto(strcpy) */
  38. /* Experimentally off - libc_hidden_proto(strdup) */
  39. /* Experimentally off - libc_hidden_proto(strlen) */
  40. /* Experimentally off - 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 __UCLIBC_HAS_LFS__ && !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. free (names->name);
  426. names = names->next;
  427. }
  428. return GLOB_NOSPACE;
  429. }
  430. #ifdef COMPILE_GLOB64
  431. /* libc_hidden_proto(glob64) */
  432. /* libc_hidden_proto(globfree64) */
  433. #else
  434. /* libc_hidden_proto(glob) */
  435. /* libc_hidden_proto(globfree) */
  436. #endif
  437. /* Do glob searching for PATTERN, placing results in PGLOB.
  438. The bits defined above may be set in FLAGS.
  439. If a directory cannot be opened or read and ERRFUNC is not nil,
  440. it is called with the pathname that caused the error, and the
  441. `errno' value from the failing call; if it returns non-zero
  442. `glob' returns GLOB_ABEND; if it returns zero, the error is ignored.
  443. If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned.
  444. Otherwise, `glob' returns zero. */
  445. int
  446. glob (
  447. const char *pattern,
  448. int flags,
  449. int (*errfunc) (const char *, int),
  450. glob_t *pglob)
  451. {
  452. const char *filename;
  453. const char *dirname;
  454. size_t dirlen;
  455. int status;
  456. size_t oldcount;
  457. if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0)
  458. {
  459. __set_errno (EINVAL);
  460. return -1;
  461. }
  462. if (!(flags & GLOB_DOOFFS))
  463. /* Have to do this so `globfree' knows where to start freeing. It
  464. also makes all the code that uses gl_offs simpler. */
  465. pglob->gl_offs = 0;
  466. #if defined ENABLE_GLOB_BRACE_EXPANSION
  467. if (flags & GLOB_BRACE)
  468. {
  469. const char *begin;
  470. if (flags & GLOB_NOESCAPE)
  471. begin = strchr (pattern, '{');
  472. else
  473. {
  474. begin = pattern;
  475. while (1)
  476. {
  477. if (*begin == '\0')
  478. {
  479. begin = NULL;
  480. break;
  481. }
  482. if (*begin == '\\' && begin[1] != '\0')
  483. ++begin;
  484. else if (*begin == '{')
  485. break;
  486. ++begin;
  487. }
  488. }
  489. if (begin != NULL)
  490. {
  491. /* Allocate working buffer large enough for our work. Note that
  492. we have at least an opening and closing brace. */
  493. size_t firstc;
  494. char *alt_start;
  495. const char *p;
  496. const char *next;
  497. const char *rest;
  498. size_t rest_len;
  499. char onealt[strlen (pattern) - 1];
  500. /* We know the prefix for all sub-patterns. */
  501. alt_start = mempcpy (onealt, pattern, begin - pattern);
  502. /* Find the first sub-pattern and at the same time find the
  503. rest after the closing brace. */
  504. next = __next_brace_sub (begin + 1, flags);
  505. if (next == NULL)
  506. {
  507. /* It is an illegal expression. */
  508. return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
  509. }
  510. /* Now find the end of the whole brace expression. */
  511. rest = next;
  512. while (*rest != '}')
  513. {
  514. rest = __next_brace_sub (rest + 1, flags);
  515. if (rest == NULL)
  516. {
  517. /* It is an illegal expression. */
  518. return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob);
  519. }
  520. }
  521. /* Please note that we now can be sure the brace expression
  522. is well-formed. */
  523. rest_len = strlen (++rest) + 1;
  524. /* We have a brace expression. BEGIN points to the opening {,
  525. NEXT points past the terminator of the first element, and END
  526. points past the final }. We will accumulate result names from
  527. recursive runs for each brace alternative in the buffer using
  528. GLOB_APPEND. */
  529. if (!(flags & GLOB_APPEND))
  530. {
  531. /* This call is to set a new vector, so clear out the
  532. vector so we can append to it. */
  533. pglob->gl_pathc = 0;
  534. pglob->gl_pathv = NULL;
  535. }
  536. firstc = pglob->gl_pathc;
  537. p = begin + 1;
  538. while (1)
  539. {
  540. int result;
  541. /* Construct the new glob expression. */
  542. mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len);
  543. result = glob (onealt,
  544. ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC))
  545. | GLOB_APPEND), errfunc, pglob);
  546. /* If we got an error, return it. */
  547. if (result && result != GLOB_NOMATCH)
  548. {
  549. if (!(flags & GLOB_APPEND))
  550. {
  551. globfree (pglob);
  552. pglob->gl_pathc = 0;
  553. }
  554. return result;
  555. }
  556. if (*next == '}')
  557. /* We saw the last entry. */
  558. break;
  559. p = next + 1;
  560. next = __next_brace_sub (p, flags);
  561. /* assert (next != NULL); */
  562. }
  563. if (pglob->gl_pathc != firstc)
  564. /* We found some entries. */
  565. return 0;
  566. else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC)))
  567. return GLOB_NOMATCH;
  568. }
  569. }
  570. #endif
  571. /* Find the filename. */
  572. filename = strrchr (pattern, '/');
  573. if (filename == NULL)
  574. {
  575. /* This can mean two things: a simple name or "~name". The latter
  576. case is nothing but a notation for a directory. */
  577. if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~')
  578. {
  579. dirname = pattern;
  580. dirlen = strlen (pattern);
  581. /* Set FILENAME to NULL as a special flag. This is ugly but
  582. other solutions would require much more code. We test for
  583. this special case below. */
  584. filename = NULL;
  585. }
  586. else
  587. {
  588. filename = pattern;
  589. dirname = ".";
  590. dirlen = 0;
  591. }
  592. }
  593. else if (filename == pattern)
  594. {
  595. /* "/pattern". */
  596. dirname = "/";
  597. dirlen = 1;
  598. ++filename;
  599. }
  600. else
  601. {
  602. char *newp;
  603. dirlen = filename - pattern;
  604. newp = (char *) alloca (dirlen + 1);
  605. *((char *) mempcpy (newp, pattern, dirlen)) = '\0';
  606. dirname = newp;
  607. ++filename;
  608. if (filename[0] == '\0'
  609. && dirlen > 1)
  610. /* "pattern/". Expand "pattern", appending slashes. */
  611. {
  612. int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob);
  613. if (val == 0)
  614. pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK)
  615. | (flags & GLOB_MARK));
  616. return val;
  617. }
  618. }
  619. if (!(flags & GLOB_APPEND))
  620. {
  621. pglob->gl_pathc = 0;
  622. if (!(flags & GLOB_DOOFFS))
  623. pglob->gl_pathv = NULL;
  624. else
  625. {
  626. size_t i;
  627. pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1)
  628. * sizeof (char *));
  629. if (pglob->gl_pathv == NULL)
  630. return GLOB_NOSPACE;
  631. for (i = 0; i <= pglob->gl_offs; ++i)
  632. pglob->gl_pathv[i] = NULL;
  633. }
  634. }
  635. oldcount = pglob->gl_pathc + pglob->gl_offs;
  636. #if defined ENABLE_GLOB_TILDE_EXPANSION
  637. if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~')
  638. {
  639. if (dirname[1] == '\0' || dirname[1] == '/')
  640. {
  641. /* Look up home directory. */
  642. const char *home_dir = getenv ("HOME");
  643. if (home_dir == NULL || home_dir[0] == '\0')
  644. {
  645. int success;
  646. char *name;
  647. # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX)
  648. size_t buflen = GET_LOGIN_NAME_MAX () + 1;
  649. if (buflen == 0)
  650. /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try
  651. a moderate value. */
  652. buflen = 20;
  653. name = (char *) alloca (buflen);
  654. success = getlogin_r (name, buflen) == 0;
  655. if (success)
  656. {
  657. struct passwd *p;
  658. # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX)
  659. long int pwbuflen = GETPW_R_SIZE_MAX ();
  660. char *pwtmpbuf;
  661. struct passwd pwbuf;
  662. int save = errno;
  663. pwtmpbuf = (char *) alloca (pwbuflen);
  664. while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p)
  665. != 0)
  666. {
  667. if (errno != ERANGE)
  668. {
  669. p = NULL;
  670. break;
  671. }
  672. pwtmpbuf = extend_alloca (pwtmpbuf, pwbuflen,
  673. 2 * pwbuflen);
  674. __set_errno (save);
  675. }
  676. if (p != NULL)
  677. home_dir = p->pw_dir;
  678. }
  679. }
  680. if (home_dir == NULL || home_dir[0] == '\0')
  681. {
  682. if (flags & GLOB_TILDE_CHECK)
  683. return GLOB_NOMATCH;
  684. else
  685. home_dir = "~"; /* No luck. */
  686. }
  687. /* Now construct the full directory. */
  688. if (dirname[1] == '\0')
  689. dirname = home_dir;
  690. else
  691. {
  692. char *newp;
  693. size_t home_len = strlen (home_dir);
  694. newp = (char *) alloca (home_len + dirlen);
  695. mempcpy (mempcpy (newp, home_dir, home_len),
  696. &dirname[1], dirlen);
  697. dirname = newp;
  698. }
  699. }
  700. else
  701. {
  702. char *end_name = strchr (dirname, '/');
  703. const char *user_name;
  704. const char *home_dir;
  705. if (end_name == NULL)
  706. user_name = dirname + 1;
  707. else
  708. {
  709. char *newp;
  710. newp = (char *) alloca (end_name - dirname);
  711. *((char *) mempcpy (newp, dirname + 1, end_name - dirname))
  712. = '\0';
  713. user_name = newp;
  714. }
  715. /* Look up specific user's home directory. */
  716. {
  717. struct passwd *p;
  718. long int buflen = GETPW_R_SIZE_MAX ();
  719. char *pwtmpbuf;
  720. struct passwd pwbuf;
  721. int save = errno;
  722. pwtmpbuf = (char *) alloca (buflen);
  723. while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0)
  724. {
  725. if (errno != ERANGE)
  726. {
  727. p = NULL;
  728. break;
  729. }
  730. pwtmpbuf = extend_alloca (pwtmpbuf, buflen, 2 * buflen);
  731. __set_errno (save);
  732. }
  733. if (p != NULL)
  734. home_dir = p->pw_dir;
  735. else
  736. home_dir = NULL;
  737. }
  738. /* If we found a home directory use this. */
  739. if (home_dir != NULL)
  740. {
  741. char *newp;
  742. size_t home_len = strlen (home_dir);
  743. size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
  744. newp = (char *) alloca (home_len + rest_len + 1);
  745. *((char *) mempcpy (mempcpy (newp, home_dir, home_len),
  746. end_name, rest_len)) = '\0';
  747. dirname = newp;
  748. }
  749. else
  750. if (flags & GLOB_TILDE_CHECK)
  751. /* We have to regard it as an error if we cannot find the
  752. home directory. */
  753. return GLOB_NOMATCH;
  754. }
  755. }
  756. /* Now test whether we looked for "~" or "~NAME". In this case we
  757. can give the answer now. */
  758. if (filename == NULL)
  759. {
  760. struct stat st;
  761. struct_stat64 st64;
  762. /* Return the directory if we don't check for error or if it exists. */
  763. if ((flags & GLOB_NOCHECK)
  764. || (((flags & GLOB_ALTDIRFUNC)
  765. ? ((*pglob->gl_stat) (dirname, &st) == 0
  766. && S_ISDIR (st.st_mode))
  767. : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode)))))
  768. {
  769. int newcount = pglob->gl_pathc + pglob->gl_offs;
  770. char **new_gl_pathv;
  771. new_gl_pathv
  772. = (char **) realloc (pglob->gl_pathv,
  773. (newcount + 1 + 1) * sizeof (char *));
  774. if (new_gl_pathv == NULL)
  775. {
  776. nospace:
  777. free (pglob->gl_pathv);
  778. pglob->gl_pathv = NULL;
  779. pglob->gl_pathc = 0;
  780. return GLOB_NOSPACE;
  781. }
  782. pglob->gl_pathv = new_gl_pathv;
  783. pglob->gl_pathv[newcount] = strdup (dirname);
  784. if (pglob->gl_pathv[newcount] == NULL)
  785. goto nospace;
  786. pglob->gl_pathv[++newcount] = NULL;
  787. ++pglob->gl_pathc;
  788. pglob->gl_flags = flags;
  789. return 0;
  790. }
  791. /* Not found. */
  792. return GLOB_NOMATCH;
  793. }
  794. #endif
  795. if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE)))
  796. {
  797. /* The directory name contains metacharacters, so we
  798. have to glob for the directory, and then glob for
  799. the pattern in each directory found. */
  800. glob_t dirs;
  801. size_t i;
  802. if ((flags & GLOB_ALTDIRFUNC) != 0)
  803. {
  804. /* Use the alternative access functions also in the recursive
  805. call. */
  806. dirs.gl_opendir = pglob->gl_opendir;
  807. dirs.gl_readdir = pglob->gl_readdir;
  808. dirs.gl_closedir = pglob->gl_closedir;
  809. dirs.gl_stat = pglob->gl_stat;
  810. dirs.gl_lstat = pglob->gl_lstat;
  811. }
  812. status = glob (dirname,
  813. ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE
  814. | GLOB_ALTDIRFUNC))
  815. | GLOB_NOSORT | GLOB_ONLYDIR),
  816. errfunc, &dirs);
  817. if (status != 0)
  818. return status;
  819. /* We have successfully globbed the preceding directory name.
  820. For each name we found, call glob_in_dir on it and FILENAME,
  821. appending the results to PGLOB. */
  822. for (i = 0; i < dirs.gl_pathc; ++i)
  823. {
  824. int old_pathc;
  825. old_pathc = pglob->gl_pathc;
  826. status = glob_in_dir (filename, dirs.gl_pathv[i],
  827. ((flags | GLOB_APPEND)
  828. & ~(GLOB_NOCHECK | GLOB_NOMAGIC)),
  829. errfunc, pglob);
  830. if (status == GLOB_NOMATCH)
  831. /* No matches in this directory. Try the next. */
  832. continue;
  833. if (status != 0)
  834. {
  835. globfree (&dirs);
  836. globfree (pglob);
  837. pglob->gl_pathc = 0;
  838. return status;
  839. }
  840. /* Stick the directory on the front of each name. */
  841. if (__prefix_array (dirs.gl_pathv[i],
  842. &pglob->gl_pathv[old_pathc + pglob->gl_offs],
  843. pglob->gl_pathc - old_pathc))
  844. {
  845. globfree (&dirs);
  846. globfree (pglob);
  847. pglob->gl_pathc = 0;
  848. return GLOB_NOSPACE;
  849. }
  850. }
  851. flags |= GLOB_MAGCHAR;
  852. /* We have ignored the GLOB_NOCHECK flag in the `glob_in_dir' calls.
  853. But if we have not found any matching entry and the GLOB_NOCHECK
  854. flag was set we must return the input pattern itself. */
  855. if (pglob->gl_pathc + pglob->gl_offs == oldcount)
  856. {
  857. /* No matches. */
  858. if (flags & GLOB_NOCHECK)
  859. {
  860. int newcount = pglob->gl_pathc + pglob->gl_offs;
  861. char **new_gl_pathv;
  862. new_gl_pathv = (char **) realloc (pglob->gl_pathv,
  863. (newcount + 2)
  864. * sizeof (char *));
  865. if (new_gl_pathv == NULL)
  866. {
  867. globfree (&dirs);
  868. return GLOB_NOSPACE;
  869. }
  870. pglob->gl_pathv = new_gl_pathv;
  871. pglob->gl_pathv[newcount] = strdup (pattern);
  872. if (pglob->gl_pathv[newcount] == NULL)
  873. {
  874. globfree (&dirs);
  875. globfree (pglob);
  876. pglob->gl_pathc = 0;
  877. return GLOB_NOSPACE;
  878. }
  879. ++pglob->gl_pathc;
  880. ++newcount;
  881. pglob->gl_pathv[newcount] = NULL;
  882. pglob->gl_flags = flags;
  883. }
  884. else
  885. {
  886. globfree (&dirs);
  887. return GLOB_NOMATCH;
  888. }
  889. }
  890. globfree (&dirs);
  891. }
  892. else
  893. {
  894. int old_pathc = pglob->gl_pathc;
  895. status = glob_in_dir (filename, dirname, flags, errfunc, pglob);
  896. if (status != 0)
  897. return status;
  898. if (dirlen > 0)
  899. {
  900. /* Stick the directory on the front of each name. */
  901. if (__prefix_array (dirname,
  902. &pglob->gl_pathv[old_pathc + pglob->gl_offs],
  903. pglob->gl_pathc - old_pathc))
  904. {
  905. globfree (pglob);
  906. pglob->gl_pathc = 0;
  907. return GLOB_NOSPACE;
  908. }
  909. }
  910. }
  911. if (flags & GLOB_MARK)
  912. {
  913. /* Append slashes to directory names. */
  914. size_t i;
  915. struct stat st;
  916. struct_stat64 st64;
  917. for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i)
  918. if (((flags & GLOB_ALTDIRFUNC)
  919. ? ((*pglob->gl_stat) (pglob->gl_pathv[i], &st) == 0
  920. && S_ISDIR (st.st_mode))
  921. : (__stat64 (pglob->gl_pathv[i], &st64) == 0
  922. && S_ISDIR (st64.st_mode))))
  923. {
  924. size_t len = strlen (pglob->gl_pathv[i]) + 2;
  925. char *new = realloc (pglob->gl_pathv[i], len);
  926. if (new == NULL)
  927. {
  928. globfree (pglob);
  929. pglob->gl_pathc = 0;
  930. return GLOB_NOSPACE;
  931. }
  932. strcpy (&new[len - 2], "/");
  933. pglob->gl_pathv[i] = new;
  934. }
  935. }
  936. if (!(flags & GLOB_NOSORT))
  937. {
  938. /* Sort the vector. */
  939. qsort (&pglob->gl_pathv[oldcount],
  940. pglob->gl_pathc + pglob->gl_offs - oldcount,
  941. sizeof (char *), __collated_compare);
  942. }
  943. return 0;
  944. }
  945. #ifdef COMPILE_GLOB64
  946. libc_hidden_def(glob64)
  947. #else
  948. libc_hidden_def(glob)
  949. #endif
  950. /* Free storage allocated in PGLOB by a previous `glob' call. */
  951. void
  952. globfree (register glob_t *pglob)
  953. {
  954. if (pglob->gl_pathv != NULL)
  955. {
  956. size_t i;
  957. for (i = 0; i < pglob->gl_pathc; ++i)
  958. if (pglob->gl_pathv[pglob->gl_offs + i] != NULL)
  959. free (pglob->gl_pathv[pglob->gl_offs + i]);
  960. free (pglob->gl_pathv);
  961. pglob->gl_pathv = NULL;
  962. }
  963. }
  964. #ifdef COMPILE_GLOB64
  965. libc_hidden_def(globfree64)
  966. #else
  967. libc_hidden_def(globfree)
  968. #endif