glob.c 27 KB

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