glob.c 27 KB

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