glob.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * Copyright (c) 1989, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Guido van Rossum.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. * must display the following acknowledgement:
  18. * This product includes software developed by the University of
  19. * California, Berkeley and its contributors.
  20. * 4. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. /*
  37. * glob(3) -- a superset of the one defined in POSIX 1003.2.
  38. *
  39. * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
  40. *
  41. * Optional extra services, controlled by flags not defined by POSIX:
  42. *
  43. * GLOB_QUOTE:
  44. * Escaping convention: \ inhibits any special meaning the following
  45. * character might have (except \ at end of string is retained).
  46. * GLOB_MAGCHAR:
  47. * Set in gl_flags if pattern contained a globbing character.
  48. * GLOB_NOMAGIC:
  49. * Same as GLOB_NOCHECK, but it will only append pattern if it did
  50. * not contain any magic characters. [Used in csh style globbing]
  51. * GLOB_ALTDIRFUNC:
  52. * Use alternately specified directory access functions.
  53. * GLOB_TILDE:
  54. * expand ~user/foo to the /home/dir/of/user/foo
  55. * GLOB_BRACE:
  56. * expand {1,2}{a,b} to 1a 1b 2a 2b
  57. * gl_matchc:
  58. * Number of matches in the current invocation of glob.
  59. */
  60. #ifdef PHP_WIN32
  61. #if _MSC_VER < 1800
  62. # define _POSIX_
  63. # include <limits.h>
  64. # undef _POSIX_
  65. #else
  66. /* Visual Studio 2013 removed all the _POSIX_ defines, but we depend on some */
  67. # ifndef ARG_MAX
  68. # define ARG_MAX 14500
  69. # endif
  70. #endif
  71. #endif
  72. #include "php.h"
  73. #include <sys/stat.h>
  74. #include <ctype.h>
  75. #ifndef PHP_WIN32
  76. #include <sys/param.h>
  77. #include <dirent.h>
  78. #include <pwd.h>
  79. #include <unistd.h>
  80. #endif
  81. #include <errno.h>
  82. #include "glob.h"
  83. #include <stdio.h>
  84. #include <stdlib.h>
  85. #include <string.h>
  86. #define DOLLAR '$'
  87. #define DOT '.'
  88. #define EOS '\0'
  89. #define LBRACKET '['
  90. #define NOT '!'
  91. #define QUESTION '?'
  92. #define QUOTE '\\'
  93. #define RANGE '-'
  94. #define RBRACKET ']'
  95. #define SEP DEFAULT_SLASH
  96. #define STAR '*'
  97. #define TILDE '~'
  98. #define UNDERSCORE '_'
  99. #define LBRACE '{'
  100. #define RBRACE '}'
  101. #define SLASH '/'
  102. #define COMMA ','
  103. #ifndef DEBUG
  104. #define M_QUOTE 0x8000
  105. #define M_PROTECT 0x4000
  106. #define M_MASK 0xffff
  107. #define M_ASCII 0x00ff
  108. typedef u_short Char;
  109. #else
  110. #define M_QUOTE 0x80
  111. #define M_PROTECT 0x40
  112. #define M_MASK 0xff
  113. #define M_ASCII 0x7f
  114. typedef char Char;
  115. #endif
  116. #define CHAR(c) ((Char)((c)&M_ASCII))
  117. #define META(c) ((Char)((c)|M_QUOTE))
  118. #define M_ALL META('*')
  119. #define M_END META(']')
  120. #define M_NOT META('!')
  121. #define M_ONE META('?')
  122. #define M_RNG META('-')
  123. #define M_SET META('[')
  124. #define ismeta(c) (((c)&M_QUOTE) != 0)
  125. static int compare(const void *, const void *);
  126. static int g_Ctoc(const Char *, char *, u_int);
  127. static int g_lstat(Char *, zend_stat_t *, glob_t *);
  128. static DIR *g_opendir(Char *, glob_t *);
  129. static Char *g_strchr(Char *, int);
  130. static int g_stat(Char *, zend_stat_t *, glob_t *);
  131. static int glob0(const Char *, glob_t *);
  132. static int glob1(Char *, Char *, glob_t *, size_t *);
  133. static int glob2(Char *, Char *, Char *, Char *, Char *, Char *,
  134. glob_t *, size_t *);
  135. static int glob3(Char *, Char *, Char *, Char *, Char *, Char *,
  136. Char *, Char *, glob_t *, size_t *);
  137. static int globextend(const Char *, glob_t *, size_t *);
  138. static const Char *globtilde(const Char *, Char *, size_t, glob_t *);
  139. static int globexp1(const Char *, glob_t *);
  140. static int globexp2(const Char *, const Char *, glob_t *, int *);
  141. static int match(Char *, Char *, Char *);
  142. #ifdef DEBUG
  143. static void qprintf(const char *, Char *);
  144. #endif
  145. PHPAPI int
  146. glob(pattern, flags, errfunc, pglob)
  147. const char *pattern;
  148. int flags, (*errfunc)(const char *, int);
  149. glob_t *pglob;
  150. {
  151. const u_char *patnext;
  152. int c;
  153. Char *bufnext, *bufend, patbuf[MAXPATHLEN];
  154. #ifdef PHP_WIN32
  155. /* Force skipping escape sequences on windows
  156. * due to the ambiguity with path backslashes
  157. */
  158. flags |= GLOB_NOESCAPE;
  159. #endif
  160. patnext = (u_char *) pattern;
  161. if (!(flags & GLOB_APPEND)) {
  162. pglob->gl_pathc = 0;
  163. pglob->gl_pathv = NULL;
  164. if (!(flags & GLOB_DOOFFS))
  165. pglob->gl_offs = 0;
  166. }
  167. pglob->gl_flags = flags & ~GLOB_MAGCHAR;
  168. pglob->gl_errfunc = errfunc;
  169. pglob->gl_matchc = 0;
  170. bufnext = patbuf;
  171. bufend = bufnext + MAXPATHLEN - 1;
  172. if (flags & GLOB_NOESCAPE)
  173. while (bufnext < bufend && (c = *patnext++) != EOS)
  174. *bufnext++ = c;
  175. else {
  176. /* Protect the quoted characters. */
  177. while (bufnext < bufend && (c = *patnext++) != EOS)
  178. if (c == QUOTE) {
  179. if ((c = *patnext++) == EOS) {
  180. c = QUOTE;
  181. --patnext;
  182. }
  183. *bufnext++ = c | M_PROTECT;
  184. } else
  185. *bufnext++ = c;
  186. }
  187. *bufnext = EOS;
  188. if (flags & GLOB_BRACE)
  189. return globexp1(patbuf, pglob);
  190. else
  191. return glob0(patbuf, pglob);
  192. }
  193. /*
  194. * Expand recursively a glob {} pattern. When there is no more expansion
  195. * invoke the standard globbing routine to glob the rest of the magic
  196. * characters
  197. */
  198. static int
  199. globexp1(pattern, pglob)
  200. const Char *pattern;
  201. glob_t *pglob;
  202. {
  203. const Char* ptr = pattern;
  204. int rv;
  205. /* Protect a single {}, for find(1), like csh */
  206. if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
  207. return glob0(pattern, pglob);
  208. while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
  209. if (!globexp2(ptr, pattern, pglob, &rv))
  210. return rv;
  211. return glob0(pattern, pglob);
  212. }
  213. /*
  214. * Recursive brace globbing helper. Tries to expand a single brace.
  215. * If it succeeds then it invokes globexp1 with the new pattern.
  216. * If it fails then it tries to glob the rest of the pattern and returns.
  217. */
  218. static int
  219. globexp2(ptr, pattern, pglob, rv)
  220. const Char *ptr, *pattern;
  221. glob_t *pglob;
  222. int *rv;
  223. {
  224. int i;
  225. Char *lm, *ls;
  226. const Char *pe, *pm, *pl;
  227. Char patbuf[MAXPATHLEN];
  228. /* copy part up to the brace */
  229. for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
  230. ;
  231. *lm = EOS;
  232. ls = lm;
  233. /* Find the balanced brace */
  234. for (i = 0, pe = ++ptr; *pe; pe++)
  235. if (*pe == LBRACKET) {
  236. /* Ignore everything between [] */
  237. for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
  238. ;
  239. if (*pe == EOS) {
  240. /*
  241. * We could not find a matching RBRACKET.
  242. * Ignore and just look for RBRACE
  243. */
  244. pe = pm;
  245. }
  246. } else if (*pe == LBRACE)
  247. i++;
  248. else if (*pe == RBRACE) {
  249. if (i == 0)
  250. break;
  251. i--;
  252. }
  253. /* Non matching braces; just glob the pattern */
  254. if (i != 0 || *pe == EOS) {
  255. *rv = glob0(patbuf, pglob);
  256. return 0;
  257. }
  258. for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
  259. const Char *pb;
  260. switch (*pm) {
  261. case LBRACKET:
  262. /* Ignore everything between [] */
  263. for (pb = pm++; *pm != RBRACKET && *pm != EOS; pm++)
  264. ;
  265. if (*pm == EOS) {
  266. /*
  267. * We could not find a matching RBRACKET.
  268. * Ignore and just look for RBRACE
  269. */
  270. pm = pb;
  271. }
  272. break;
  273. case LBRACE:
  274. i++;
  275. break;
  276. case RBRACE:
  277. if (i) {
  278. i--;
  279. break;
  280. }
  281. /* FALLTHROUGH */
  282. case COMMA:
  283. if (i && *pm == COMMA)
  284. break;
  285. else {
  286. /* Append the current string */
  287. for (lm = ls; (pl < pm); *lm++ = *pl++)
  288. ;
  289. /*
  290. * Append the rest of the pattern after the
  291. * closing brace
  292. */
  293. for (pl = pe + 1; (*lm++ = *pl++) != EOS; )
  294. ;
  295. /* Expand the current pattern */
  296. #ifdef DEBUG
  297. qprintf("globexp2:", patbuf);
  298. #endif
  299. *rv = globexp1(patbuf, pglob);
  300. /* move after the comma, to the next string */
  301. pl = pm + 1;
  302. }
  303. break;
  304. default:
  305. break;
  306. }
  307. }
  308. *rv = 0;
  309. return 0;
  310. }
  311. /*
  312. * expand tilde from the passwd file.
  313. */
  314. static const Char *
  315. globtilde(pattern, patbuf, patbuf_len, pglob)
  316. const Char *pattern;
  317. Char *patbuf;
  318. size_t patbuf_len;
  319. glob_t *pglob;
  320. {
  321. #ifndef PHP_WIN32
  322. struct passwd *pwd;
  323. #endif
  324. char *h;
  325. const Char *p;
  326. Char *b, *eb;
  327. if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
  328. return pattern;
  329. /* Copy up to the end of the string or / */
  330. eb = &patbuf[patbuf_len - 1];
  331. for (p = pattern + 1, h = (char *) patbuf;
  332. h < (char *)eb && *p && *p != SLASH; *h++ = (char) *p++)
  333. ;
  334. *h = EOS;
  335. #if 0
  336. if (h == (char *)eb)
  337. return what;
  338. #endif
  339. if (((char *) patbuf)[0] == EOS) {
  340. /*
  341. * handle a plain ~ or ~/ by expanding $HOME
  342. * first and then trying the password file
  343. */
  344. if ((h = getenv("HOME")) == NULL) {
  345. #ifndef PHP_WIN32
  346. if ((pwd = getpwuid(getuid())) == NULL)
  347. return pattern;
  348. else
  349. h = pwd->pw_dir;
  350. #else
  351. return pattern;
  352. #endif
  353. }
  354. } else {
  355. /*
  356. * Expand a ~user
  357. */
  358. #ifndef PHP_WIN32
  359. if ((pwd = getpwnam((char*) patbuf)) == NULL)
  360. return pattern;
  361. else
  362. h = pwd->pw_dir;
  363. #else
  364. return pattern;
  365. #endif
  366. }
  367. /* Copy the home directory */
  368. for (b = patbuf; b < eb && *h; *b++ = *h++)
  369. ;
  370. /* Append the rest of the pattern */
  371. while (b < eb && (*b++ = *p++) != EOS)
  372. ;
  373. *b = EOS;
  374. return patbuf;
  375. }
  376. /*
  377. * The main glob() routine: compiles the pattern (optionally processing
  378. * quotes), calls glob1() to do the real pattern matching, and finally
  379. * sorts the list (unless unsorted operation is requested). Returns 0
  380. * if things went well, nonzero if errors occurred. It is not an error
  381. * to find no matches.
  382. */
  383. static int
  384. glob0(pattern, pglob)
  385. const Char *pattern;
  386. glob_t *pglob;
  387. {
  388. const Char *qpatnext;
  389. int c, err, oldpathc;
  390. Char *bufnext, patbuf[MAXPATHLEN];
  391. size_t limit = 0;
  392. qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
  393. oldpathc = pglob->gl_pathc;
  394. bufnext = patbuf;
  395. /* We don't need to check for buffer overflow any more. */
  396. while ((c = *qpatnext++) != EOS) {
  397. switch (c) {
  398. case LBRACKET:
  399. c = *qpatnext;
  400. if (c == NOT)
  401. ++qpatnext;
  402. if (*qpatnext == EOS ||
  403. g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
  404. *bufnext++ = LBRACKET;
  405. if (c == NOT)
  406. --qpatnext;
  407. break;
  408. }
  409. *bufnext++ = M_SET;
  410. if (c == NOT)
  411. *bufnext++ = M_NOT;
  412. c = *qpatnext++;
  413. do {
  414. *bufnext++ = CHAR(c);
  415. if (*qpatnext == RANGE &&
  416. (c = qpatnext[1]) != RBRACKET) {
  417. *bufnext++ = M_RNG;
  418. *bufnext++ = CHAR(c);
  419. qpatnext += 2;
  420. }
  421. } while ((c = *qpatnext++) != RBRACKET);
  422. pglob->gl_flags |= GLOB_MAGCHAR;
  423. *bufnext++ = M_END;
  424. break;
  425. case QUESTION:
  426. pglob->gl_flags |= GLOB_MAGCHAR;
  427. *bufnext++ = M_ONE;
  428. break;
  429. case STAR:
  430. pglob->gl_flags |= GLOB_MAGCHAR;
  431. /* collapse adjacent stars to one,
  432. * to avoid exponential behavior
  433. */
  434. if (bufnext == patbuf || bufnext[-1] != M_ALL)
  435. *bufnext++ = M_ALL;
  436. break;
  437. default:
  438. *bufnext++ = CHAR(c);
  439. break;
  440. }
  441. }
  442. *bufnext = EOS;
  443. #ifdef DEBUG
  444. qprintf("glob0:", patbuf);
  445. #endif
  446. if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, &limit)) != 0)
  447. return(err);
  448. /*
  449. * If there was no match we are going to append the pattern
  450. * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
  451. * and the pattern did not contain any magic characters
  452. * GLOB_NOMAGIC is there just for compatibility with csh.
  453. */
  454. if (pglob->gl_pathc == oldpathc) {
  455. if ((pglob->gl_flags & GLOB_NOCHECK) ||
  456. ((pglob->gl_flags & GLOB_NOMAGIC) &&
  457. !(pglob->gl_flags & GLOB_MAGCHAR)))
  458. return(globextend(pattern, pglob, &limit));
  459. else
  460. return(GLOB_NOMATCH);
  461. }
  462. if (!(pglob->gl_flags & GLOB_NOSORT))
  463. qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
  464. pglob->gl_pathc - oldpathc, sizeof(char *), compare);
  465. return(0);
  466. }
  467. static int
  468. compare(const void *p, const void *q)
  469. {
  470. return(strcmp(*(char **)p, *(char **)q));
  471. }
  472. static int
  473. glob1(pattern, pattern_last, pglob, limitp)
  474. Char *pattern, *pattern_last;
  475. glob_t *pglob;
  476. size_t *limitp;
  477. {
  478. Char pathbuf[MAXPATHLEN];
  479. /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
  480. if (*pattern == EOS)
  481. return(0);
  482. return(glob2(pathbuf, pathbuf+MAXPATHLEN-1,
  483. pathbuf, pathbuf+MAXPATHLEN-1,
  484. pattern, pattern_last, pglob, limitp));
  485. }
  486. /*
  487. * The functions glob2 and glob3 are mutually recursive; there is one level
  488. * of recursion for each segment in the pattern that contains one or more
  489. * meta characters.
  490. */
  491. static int
  492. glob2(pathbuf, pathbuf_last, pathend, pathend_last, pattern,
  493. pattern_last, pglob, limitp)
  494. Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
  495. Char *pattern, *pattern_last;
  496. glob_t *pglob;
  497. size_t *limitp;
  498. {
  499. zend_stat_t sb;
  500. Char *p, *q;
  501. int anymeta;
  502. /*
  503. * Loop over pattern segments until end of pattern or until
  504. * segment with meta character found.
  505. */
  506. for (anymeta = 0;;) {
  507. if (*pattern == EOS) { /* End of pattern? */
  508. *pathend = EOS;
  509. if (g_lstat(pathbuf, &sb, pglob))
  510. return(0);
  511. if (((pglob->gl_flags & GLOB_MARK) &&
  512. !IS_SLASH(pathend[-1])) && (S_ISDIR(sb.st_mode) ||
  513. (S_ISLNK(sb.st_mode) &&
  514. (g_stat(pathbuf, &sb, pglob) == 0) &&
  515. S_ISDIR(sb.st_mode)))) {
  516. if (pathend+1 > pathend_last)
  517. return (1);
  518. *pathend++ = SEP;
  519. *pathend = EOS;
  520. }
  521. ++pglob->gl_matchc;
  522. return(globextend(pathbuf, pglob, limitp));
  523. }
  524. /* Find end of next segment, copy tentatively to pathend. */
  525. q = pathend;
  526. p = pattern;
  527. while (*p != EOS && !IS_SLASH(*p)) {
  528. if (ismeta(*p))
  529. anymeta = 1;
  530. if (q+1 > pathend_last)
  531. return (1);
  532. *q++ = *p++;
  533. }
  534. if (!anymeta) { /* No expansion, do next segment. */
  535. pathend = q;
  536. pattern = p;
  537. while (IS_SLASH(*pattern)) {
  538. if (pathend+1 > pathend_last)
  539. return (1);
  540. *pathend++ = *pattern++;
  541. }
  542. } else
  543. /* Need expansion, recurse. */
  544. return(glob3(pathbuf, pathbuf_last, pathend,
  545. pathend_last, pattern, pattern_last,
  546. p, pattern_last, pglob, limitp));
  547. }
  548. /* NOTREACHED */
  549. }
  550. static int
  551. glob3(pathbuf, pathbuf_last, pathend, pathend_last, pattern, pattern_last,
  552. restpattern, restpattern_last, pglob, limitp)
  553. Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
  554. Char *pattern, *pattern_last, *restpattern, *restpattern_last;
  555. glob_t *pglob;
  556. size_t *limitp;
  557. {
  558. register struct dirent *dp;
  559. DIR *dirp;
  560. int err;
  561. /*
  562. * The readdirfunc declaration can't be prototyped, because it is
  563. * assigned, below, to two functions which are prototyped in glob.h
  564. * and dirent.h as taking pointers to differently typed opaque
  565. * structures.
  566. */
  567. struct dirent *(*readdirfunc)();
  568. if (pathend > pathend_last)
  569. return (1);
  570. *pathend = EOS;
  571. errno = 0;
  572. if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
  573. /* TODO: don't call for ENOENT or ENOTDIR? */
  574. if (pglob->gl_errfunc) {
  575. char buf[MAXPATHLEN];
  576. if (g_Ctoc(pathbuf, buf, sizeof(buf)))
  577. return(GLOB_ABORTED);
  578. if (pglob->gl_errfunc(buf, errno) ||
  579. pglob->gl_flags & GLOB_ERR)
  580. return(GLOB_ABORTED);
  581. }
  582. return(0);
  583. }
  584. err = 0;
  585. /* Search directory for matching names. */
  586. if (pglob->gl_flags & GLOB_ALTDIRFUNC)
  587. readdirfunc = pglob->gl_readdir;
  588. else
  589. readdirfunc = readdir;
  590. while ((dp = (*readdirfunc)(dirp))) {
  591. register u_char *sc;
  592. register Char *dc;
  593. /* Initial DOT must be matched literally. */
  594. if (dp->d_name[0] == DOT && *pattern != DOT)
  595. continue;
  596. dc = pathend;
  597. sc = (u_char *) dp->d_name;
  598. while (dc < pathend_last && (*dc++ = *sc++) != EOS)
  599. ;
  600. if (dc >= pathend_last) {
  601. *dc = EOS;
  602. err = 1;
  603. break;
  604. }
  605. if (!match(pathend, pattern, restpattern)) {
  606. *pathend = EOS;
  607. continue;
  608. }
  609. err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
  610. restpattern, restpattern_last, pglob, limitp);
  611. if (err)
  612. break;
  613. }
  614. if (pglob->gl_flags & GLOB_ALTDIRFUNC)
  615. (*pglob->gl_closedir)(dirp);
  616. else
  617. closedir(dirp);
  618. return(err);
  619. }
  620. /*
  621. * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
  622. * add the new item, and update gl_pathc.
  623. *
  624. * This assumes the BSD realloc, which only copies the block when its size
  625. * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
  626. * behavior.
  627. *
  628. * Return 0 if new item added, error code if memory couldn't be allocated.
  629. *
  630. * Invariant of the glob_t structure:
  631. * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
  632. * gl_pathv points to (gl_offs + gl_pathc + 1) items.
  633. */
  634. static int
  635. globextend(path, pglob, limitp)
  636. const Char *path;
  637. glob_t *pglob;
  638. size_t *limitp;
  639. {
  640. register char **pathv;
  641. u_int newsize, len;
  642. char *copy;
  643. const Char *p;
  644. newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
  645. pathv = pglob->gl_pathv ? realloc((char *)pglob->gl_pathv, newsize) :
  646. malloc(newsize);
  647. if (pathv == NULL) {
  648. if (pglob->gl_pathv) {
  649. free(pglob->gl_pathv);
  650. pglob->gl_pathv = NULL;
  651. }
  652. return(GLOB_NOSPACE);
  653. }
  654. if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
  655. register int i;
  656. /* first time around -- clear initial gl_offs items */
  657. pathv += pglob->gl_offs;
  658. for (i = pglob->gl_offs; --i >= 0; )
  659. *--pathv = NULL;
  660. }
  661. pglob->gl_pathv = pathv;
  662. for (p = path; *p++;)
  663. ;
  664. len = (u_int)(p - path);
  665. *limitp += len;
  666. if ((copy = malloc(len)) != NULL) {
  667. if (g_Ctoc(path, copy, len)) {
  668. free(copy);
  669. return(GLOB_NOSPACE);
  670. }
  671. pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
  672. }
  673. pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
  674. if ((pglob->gl_flags & GLOB_LIMIT) &&
  675. newsize + *limitp >= ARG_MAX) {
  676. errno = 0;
  677. return(GLOB_NOSPACE);
  678. }
  679. return(copy == NULL ? GLOB_NOSPACE : 0);
  680. }
  681. /*
  682. * pattern matching function for filenames. Each occurrence of the *
  683. * pattern causes a recursion level.
  684. */
  685. static int
  686. match(name, pat, patend)
  687. register Char *name, *pat, *patend;
  688. {
  689. int ok, negate_range;
  690. Char c, k;
  691. while (pat < patend) {
  692. c = *pat++;
  693. switch (c & M_MASK) {
  694. case M_ALL:
  695. if (pat == patend)
  696. return(1);
  697. do
  698. if (match(name, pat, patend))
  699. return(1);
  700. while (*name++ != EOS)
  701. ;
  702. return(0);
  703. case M_ONE:
  704. if (*name++ == EOS)
  705. return(0);
  706. break;
  707. case M_SET:
  708. ok = 0;
  709. if ((k = *name++) == EOS)
  710. return(0);
  711. if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
  712. ++pat;
  713. while (((c = *pat++) & M_MASK) != M_END)
  714. if ((*pat & M_MASK) == M_RNG) {
  715. if (c <= k && k <= pat[1])
  716. ok = 1;
  717. pat += 2;
  718. } else if (c == k)
  719. ok = 1;
  720. if (ok == negate_range)
  721. return(0);
  722. break;
  723. default:
  724. if (*name++ != c)
  725. return(0);
  726. break;
  727. }
  728. }
  729. return(*name == EOS);
  730. }
  731. /* Free allocated data belonging to a glob_t structure. */
  732. PHPAPI void
  733. globfree(pglob)
  734. glob_t *pglob;
  735. {
  736. register int i;
  737. register char **pp;
  738. if (pglob->gl_pathv != NULL) {
  739. pp = pglob->gl_pathv + pglob->gl_offs;
  740. for (i = pglob->gl_pathc; i--; ++pp)
  741. if (*pp)
  742. free(*pp);
  743. free(pglob->gl_pathv);
  744. pglob->gl_pathv = NULL;
  745. }
  746. }
  747. static DIR *
  748. g_opendir(str, pglob)
  749. register Char *str;
  750. glob_t *pglob;
  751. {
  752. char buf[MAXPATHLEN];
  753. if (!*str)
  754. strlcpy(buf, ".", sizeof(buf));
  755. else {
  756. if (g_Ctoc(str, buf, sizeof(buf)))
  757. return(NULL);
  758. }
  759. if (pglob->gl_flags & GLOB_ALTDIRFUNC)
  760. return((*pglob->gl_opendir)(buf));
  761. return(opendir(buf));
  762. }
  763. static int
  764. g_lstat(fn, sb, pglob)
  765. register Char *fn;
  766. zend_stat_t *sb;
  767. glob_t *pglob;
  768. {
  769. char buf[MAXPATHLEN];
  770. if (g_Ctoc(fn, buf, sizeof(buf)))
  771. return(-1);
  772. if (pglob->gl_flags & GLOB_ALTDIRFUNC)
  773. return((*pglob->gl_lstat)(buf, sb));
  774. return(php_sys_lstat(buf, sb));
  775. }
  776. static int
  777. g_stat(fn, sb, pglob)
  778. register Char *fn;
  779. zend_stat_t *sb;
  780. glob_t *pglob;
  781. {
  782. char buf[MAXPATHLEN];
  783. if (g_Ctoc(fn, buf, sizeof(buf)))
  784. return(-1);
  785. if (pglob->gl_flags & GLOB_ALTDIRFUNC)
  786. return((*pglob->gl_stat)(buf, sb));
  787. return(php_sys_stat(buf, sb));
  788. }
  789. static Char *
  790. g_strchr(str, ch)
  791. Char *str;
  792. int ch;
  793. {
  794. do {
  795. if (*str == ch)
  796. return (str);
  797. } while (*str++);
  798. return (NULL);
  799. }
  800. static int
  801. g_Ctoc(str, buf, len)
  802. register const Char *str;
  803. char *buf;
  804. u_int len;
  805. {
  806. while (len--) {
  807. if ((*buf++ = (char) *str++) == EOS)
  808. return (0);
  809. }
  810. return (1);
  811. }
  812. #ifdef DEBUG
  813. static void
  814. qprintf(str, s)
  815. const char *str;
  816. register Char *s;
  817. {
  818. register Char *p;
  819. (void)printf("%s:\n", str);
  820. for (p = s; *p; p++)
  821. (void)printf("%c", CHAR(*p));
  822. (void)printf("\n");
  823. for (p = s; *p; p++)
  824. (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
  825. (void)printf("\n");
  826. for (p = s; *p; p++)
  827. (void)printf("%c", ismeta(*p) ? '_' : ' ');
  828. (void)printf("\n");
  829. }
  830. #endif
  831. /*
  832. * Local variables:
  833. * tab-width: 4
  834. * c-basic-offset: 4
  835. * End:
  836. * vim600: sw=4 ts=4 fdm=marker
  837. * vim<600: sw=4 ts=4
  838. */