glob.c 28 KB

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