]> sourceware.org Git - newlib-cygwin.git/blame - winsup/cygwin/glob.cc
* Merge in cygwin-64bit-branch.
[newlib-cygwin.git] / winsup / cygwin / glob.cc
CommitLineData
1fd5e000
CF
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.
1fd5e000
CF
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
48f81ac6
CV
33#ifdef __CYGWIN__
34#include "winsup.h"
35#endif
36
733c9ed3
CV
37#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
39#endif /* LIBC_SCCS and not lint */
d7bcd2a1
CV
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: src/lib/libc/gen/glob.c,v 1.28 2010/05/12 17:44:00 gordon Exp $");
1fd5e000
CF
42
43/*
44 * glob(3) -- a superset of the one defined in POSIX 1003.2.
45 *
46 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
47 *
48 * Optional extra services, controlled by flags not defined by POSIX:
49 *
50 * GLOB_QUOTE:
51 * Escaping convention: \ inhibits any special meaning the following
52 * character might have (except \ at end of string is retained).
53 * GLOB_MAGCHAR:
54 * Set in gl_flags if pattern contained a globbing character.
55 * GLOB_NOMAGIC:
56 * Same as GLOB_NOCHECK, but it will only append pattern if it did
57 * not contain any magic characters. [Used in csh style globbing]
58 * GLOB_ALTDIRFUNC:
59 * Use alternately specified directory access functions.
60 * GLOB_TILDE:
61 * expand ~user/foo to the /home/dir/of/user/foo
62 * GLOB_BRACE:
75a57bf0 63 * expand {1,2}{a,b} to 1a 1b 2a 2b
1fd5e000
CF
64 * gl_matchc:
65 * Number of matches in the current invocation of glob.
66 */
67
733c9ed3
CV
68/*
69 * Some notes on multibyte character support:
70 * 1. Patterns with illegal byte sequences match nothing - even if
71 * GLOB_NOCHECK is specified.
72 * 2. Illegal byte sequences in filenames are handled by treating them as
73 * single-byte characters with a value of the first byte of the sequence
74 * cast to wchar_t.
75 * 3. State-dependent encodings are not currently supported.
76 */
77
d7bcd2a1
CV
78#include <sys/param.h>
79#include <sys/stat.h>
1fd5e000 80
6ccb6bcf 81#include <ctype.h>
1fd5e000 82#include <dirent.h>
d7bcd2a1 83#include <errno.h>
1fd5e000 84#include <glob.h>
d7bcd2a1 85#include <limits.h>
1fd5e000 86#include <pwd.h>
d7bcd2a1
CV
87#include <stdint.h>
88#include <stdio.h>
1fd5e000 89#include <stdlib.h>
d7bcd2a1 90#include <string.h>
1fd5e000 91#include <unistd.h>
733c9ed3
CV
92#include <wchar.h>
93
d7bcd2a1 94#include "collate.h"
733c9ed3 95
d7bcd2a1
CV
96#ifdef __CYGWIN__
97#include <wctype.h>
733c9ed3
CV
98#include "path.h"
99#include "fhandler.h"
100#include "dtable.h"
733c9ed3 101#include "cygheap.h"
c16d0946 102#include "cygwin/version.h"
b1aae492 103
d7bcd2a1
CV
104#define getpwuid(uid) getpwuid32 (uid)
105#define getuid() getuid32 ()
106#define issetugid() (cygheap->user.issetuid ())
107
d7bcd2a1
CV
108#define CCHAR(c) (ignore_case_with_glob ? towlower (CHAR (c)) : CHAR (c))
109#define Cchar(c) (ignore_case_with_glob ? towlower (c) : (c))
110#endif
733c9ed3 111
1d928241
CV
112#undef MAXPATHLEN
113#define MAXPATHLEN 8192
114
1fd5e000
CF
115#define DOLLAR '$'
116#define DOT '.'
117#define EOS '\0'
118#define LBRACKET '['
119#define NOT '!'
120#define QUESTION '?'
121#define QUOTE '\\'
122#define RANGE '-'
123#define RBRACKET ']'
124#define SEP '/'
125#define STAR '*'
126#define TILDE '~'
127#define UNDERSCORE '_'
128#define LBRACE '{'
129#define RBRACE '}'
130#define SLASH '/'
131#define COMMA ','
132
133#ifndef DEBUG
134
733c9ed3
CV
135#define M_QUOTE 0x8000000000ULL
136#define M_PROTECT 0x4000000000ULL
137#define M_MASK 0xffffffffffULL
138#define M_CHAR 0x00ffffffffULL
1fd5e000 139
733c9ed3 140typedef uint_fast64_t Char;
1fd5e000
CF
141
142#else
143
144#define M_QUOTE 0x80
145#define M_PROTECT 0x40
146#define M_MASK 0xff
733c9ed3 147#define M_CHAR 0x7f
1fd5e000
CF
148
149typedef char Char;
150
151#endif
152
153
733c9ed3 154#define CHAR(c) ((Char)((c)&M_CHAR))
1fd5e000
CF
155#define META(c) ((Char)((c)|M_QUOTE))
156#define M_ALL META('*')
157#define M_END META(']')
158#define M_NOT META('!')
159#define M_ONE META('?')
160#define M_RNG META('-')
161#define M_SET META('[')
162#define ismeta(c) (((c)&M_QUOTE) != 0)
163
164
733c9ed3
CV
165static int compare(const void *, const void *);
166static int g_Ctoc(const Char *, char *, size_t);
d7bcd2a1 167static int g_lstat(Char *, struct stat *, glob_t *);
733c9ed3 168static DIR *g_opendir(Char *, glob_t *);
d7bcd2a1 169static const Char *g_strchr(const Char *, wchar_t);
1fd5e000 170#ifdef notdef
733c9ed3 171static Char *g_strcat(Char *, const Char *);
1fd5e000 172#endif
d7bcd2a1 173static int g_stat(Char *, struct stat *, glob_t *);
733c9ed3
CV
174static int glob0(const Char *, glob_t *, size_t *);
175static int glob1(Char *, glob_t *, size_t *);
176static int glob2(Char *, Char *, Char *, Char *, glob_t *, size_t *);
177static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, size_t *);
178static int globextend(const Char *, glob_t *, size_t *);
46f5dd59 179static const Char *
733c9ed3
CV
180 globtilde(const Char *, Char *, size_t, glob_t *);
181static int globexp1(const Char *, glob_t *, size_t *);
182static int globexp2(const Char *, const Char *, glob_t *, int *, size_t *);
183static int match(Char *, Char *, Char *);
1fd5e000 184#ifdef DEBUG
733c9ed3 185static void qprintf(const char *, Char *);
1fd5e000
CF
186#endif
187
1fd5e000 188int
733c9ed3 189glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob)
1fd5e000 190{
733c9ed3
CV
191 const char *patnext;
192 size_t limit;
193 Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
194 mbstate_t mbs;
195 wchar_t wc;
196 size_t clen;
197
198 patnext = pattern;
1fd5e000
CF
199 if (!(flags & GLOB_APPEND)) {
200 pglob->gl_pathc = 0;
201 pglob->gl_pathv = NULL;
202 if (!(flags & GLOB_DOOFFS))
203 pglob->gl_offs = 0;
204 }
733c9ed3
CV
205 if (flags & GLOB_LIMIT) {
206 limit = pglob->gl_matchc;
207 if (limit == 0)
208 limit = ARG_MAX;
209 } else
210 limit = 0;
1fd5e000
CF
211 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
212 pglob->gl_errfunc = errfunc;
213 pglob->gl_matchc = 0;
214
215 bufnext = patbuf;
733c9ed3
CV
216 bufend = bufnext + MAXPATHLEN - 1;
217 if (flags & GLOB_NOESCAPE) {
218 memset(&mbs, 0, sizeof(mbs));
219 while (bufend - bufnext >= MB_CUR_MAX) {
220 clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
221 if (clen == (size_t)-1 || clen == (size_t)-2)
222 return (GLOB_NOMATCH);
223 else if (clen == 0)
224 break;
225 *bufnext++ = wc;
226 patnext += clen;
227 }
228 } else {
1fd5e000 229 /* Protect the quoted characters. */
733c9ed3
CV
230 memset(&mbs, 0, sizeof(mbs));
231 while (bufend - bufnext >= MB_CUR_MAX) {
232 if (*patnext == QUOTE) {
233 if (*++patnext == EOS) {
234 *bufnext++ = QUOTE | M_PROTECT;
235 continue;
1fd5e000 236 }
733c9ed3
CV
237 prot = M_PROTECT;
238 } else
239 prot = 0;
240 clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
241 if (clen == (size_t)-1 || clen == (size_t)-2)
242 return (GLOB_NOMATCH);
243 else if (clen == 0)
244 break;
245 *bufnext++ = wc | prot;
246 patnext += clen;
247 }
1fd5e000 248 }
1fd5e000
CF
249 *bufnext = EOS;
250
251 if (flags & GLOB_BRACE)
733c9ed3 252 return globexp1(patbuf, pglob, &limit);
1fd5e000 253 else
733c9ed3 254 return glob0(patbuf, pglob, &limit);
1fd5e000
CF
255}
256
257/*
258 * Expand recursively a glob {} pattern. When there is no more expansion
259 * invoke the standard globbing routine to glob the rest of the magic
260 * characters
261 */
733c9ed3
CV
262static int
263globexp1(const Char *pattern, glob_t *pglob, size_t *limit)
1fd5e000
CF
264{
265 const Char* ptr = pattern;
266 int rv;
267
268 /* Protect a single {}, for find(1), like csh */
269 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
733c9ed3 270 return glob0(pattern, pglob, limit);
1fd5e000 271
d7bcd2a1 272 while ((ptr = g_strchr(ptr, LBRACE)) != NULL)
733c9ed3 273 if (!globexp2(ptr, pattern, pglob, &rv, limit))
1fd5e000
CF
274 return rv;
275
733c9ed3 276 return glob0(pattern, pglob, limit);
1fd5e000
CF
277}
278
279
280/*
281 * Recursive brace globbing helper. Tries to expand a single brace.
282 * If it succeeds then it invokes globexp1 with the new pattern.
283 * If it fails then it tries to glob the rest of the pattern and returns.
284 */
733c9ed3
CV
285static int
286globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, size_t *limit)
1fd5e000
CF
287{
288 int i;
289 Char *lm, *ls;
733c9ed3
CV
290 const Char *pe, *pm, *pm1, *pl;
291 Char patbuf[MAXPATHLEN];
1fd5e000
CF
292
293 /* copy part up to the brace */
294 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
295 continue;
733c9ed3 296 *lm = EOS;
1fd5e000
CF
297 ls = lm;
298
299 /* Find the balanced brace */
300 for (i = 0, pe = ++ptr; *pe; pe++)
301 if (*pe == LBRACKET) {
302 /* Ignore everything between [] */
303 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
304 continue;
305 if (*pe == EOS) {
75a57bf0 306 /*
1fd5e000
CF
307 * We could not find a matching RBRACKET.
308 * Ignore and just look for RBRACE
309 */
310 pe = pm;
311 }
312 }
313 else if (*pe == LBRACE)
314 i++;
315 else if (*pe == RBRACE) {
316 if (i == 0)
317 break;
318 i--;
319 }
320
321 /* Non matching braces; just glob the pattern */
322 if (i != 0 || *pe == EOS) {
733c9ed3 323 *rv = glob0(patbuf, pglob, limit);
1fd5e000
CF
324 return 0;
325 }
326
327 for (i = 0, pl = pm = ptr; pm <= pe; pm++)
328 switch (*pm) {
329 case LBRACKET:
330 /* Ignore everything between [] */
733c9ed3 331 for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
1fd5e000
CF
332 continue;
333 if (*pm == EOS) {
75a57bf0 334 /*
1fd5e000
CF
335 * We could not find a matching RBRACKET.
336 * Ignore and just look for RBRACE
337 */
733c9ed3 338 pm = pm1;
1fd5e000
CF
339 }
340 break;
341
342 case LBRACE:
343 i++;
344 break;
345
346 case RBRACE:
347 if (i) {
348 i--;
349 break;
350 }
351 /* FALLTHROUGH */
352 case COMMA:
353 if (i && *pm == COMMA)
354 break;
355 else {
356 /* Append the current string */
357 for (lm = ls; (pl < pm); *lm++ = *pl++)
358 continue;
75a57bf0 359 /*
1fd5e000
CF
360 * Append the rest of the pattern after the
361 * closing brace
362 */
363 for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
364 continue;
365
366 /* Expand the current pattern */
367#ifdef DEBUG
368 qprintf("globexp2:", patbuf);
369#endif
733c9ed3 370 *rv = globexp1(patbuf, pglob, limit);
1fd5e000
CF
371
372 /* move after the comma, to the next string */
373 pl = pm + 1;
374 }
375 break;
376
377 default:
378 break;
379 }
380 *rv = 0;
381 return 0;
382}
383
384
385
386/*
387 * expand tilde from the passwd file.
388 */
389static const Char *
733c9ed3 390globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
1fd5e000
CF
391{
392 struct passwd *pwd;
393 char *h;
394 const Char *p;
733c9ed3 395 Char *b, *eb;
1fd5e000
CF
396
397 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
398 return pattern;
399
46f5dd59
CF
400 /*
401 * Copy up to the end of the string or /
733c9ed3
CV
402 */
403 eb = &patbuf[patbuf_len - 1];
404 for (p = pattern + 1, h = (char *) patbuf;
405 h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
1fd5e000
CF
406 continue;
407
408 *h = EOS;
409
410 if (((char *) patbuf)[0] == EOS) {
75a57bf0 411 /*
733c9ed3
CV
412 * handle a plain ~ or ~/ by expanding $HOME first (iff
413 * we're not running setuid or setgid) and then trying
414 * the password file
1fd5e000 415 */
d7bcd2a1 416 if (issetugid() != 0 ||
733c9ed3
CV
417 (h = getenv("HOME")) == NULL) {
418 if (((h = getlogin()) != NULL &&
419 (pwd = getpwnam(h)) != NULL) ||
d7bcd2a1 420 (pwd = getpwuid(getuid())) != NULL)
1fd5e000 421 h = pwd->pw_dir;
733c9ed3
CV
422 else
423 return pattern;
1fd5e000
CF
424 }
425 }
426 else {
427 /*
428 * Expand a ~user
429 */
430 if ((pwd = getpwnam((char*) patbuf)) == NULL)
431 return pattern;
432 else
433 h = pwd->pw_dir;
434 }
435
436 /* Copy the home directory */
733c9ed3 437 for (b = patbuf; b < eb && *h; *b++ = *h++)
1fd5e000 438 continue;
75a57bf0 439
1fd5e000 440 /* Append the rest of the pattern */
733c9ed3 441 while (b < eb && (*b++ = *p++) != EOS)
1fd5e000 442 continue;
733c9ed3 443 *b = EOS;
1fd5e000
CF
444
445 return patbuf;
446}
75a57bf0 447
1fd5e000
CF
448
449/*
450 * The main glob() routine: compiles the pattern (optionally processing
451 * quotes), calls glob1() to do the real pattern matching, and finally
452 * sorts the list (unless unsorted operation is requested). Returns 0
733c9ed3 453 * if things went well, nonzero if errors occurred.
1fd5e000
CF
454 */
455static int
733c9ed3 456glob0(const Char *pattern, glob_t *pglob, size_t *limit)
1fd5e000
CF
457{
458 const Char *qpatnext;
b3f40a5f 459 int err;
733c9ed3 460 size_t oldpathc;
d7bcd2a1 461 Char *bufnext, c, patbuf[MAXPATHLEN];
1fd5e000 462
733c9ed3 463 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
1fd5e000
CF
464 oldpathc = pglob->gl_pathc;
465 bufnext = patbuf;
466
467 /* We don't need to check for buffer overflow any more. */
468 while ((c = *qpatnext++) != EOS) {
469 switch (c) {
470 case LBRACKET:
471 c = *qpatnext;
472 if (c == NOT)
473 ++qpatnext;
474 if (*qpatnext == EOS ||
d7bcd2a1 475 g_strchr(qpatnext+1, RBRACKET) == NULL) {
1fd5e000
CF
476 *bufnext++ = LBRACKET;
477 if (c == NOT)
478 --qpatnext;
479 break;
480 }
481 *bufnext++ = M_SET;
482 if (c == NOT)
483 *bufnext++ = M_NOT;
484 c = *qpatnext++;
485 do {
486 *bufnext++ = CHAR(c);
487 if (*qpatnext == RANGE &&
488 (c = qpatnext[1]) != RBRACKET) {
489 *bufnext++ = M_RNG;
490 *bufnext++ = CHAR(c);
491 qpatnext += 2;
492 }
493 } while ((c = *qpatnext++) != RBRACKET);
494 pglob->gl_flags |= GLOB_MAGCHAR;
495 *bufnext++ = M_END;
496 break;
497 case QUESTION:
498 pglob->gl_flags |= GLOB_MAGCHAR;
499 *bufnext++ = M_ONE;
500 break;
501 case STAR:
502 pglob->gl_flags |= GLOB_MAGCHAR;
75a57bf0 503 /* collapse adjacent stars to one,
1fd5e000
CF
504 * to avoid exponential behavior
505 */
506 if (bufnext == patbuf || bufnext[-1] != M_ALL)
507 *bufnext++ = M_ALL;
508 break;
509 default:
510 *bufnext++ = CHAR(c);
511 break;
512 }
513 }
514 *bufnext = EOS;
515#ifdef DEBUG
516 qprintf("glob0:", patbuf);
517#endif
518
733c9ed3 519 if ((err = glob1(patbuf, pglob, limit)) != 0)
1fd5e000
CF
520 return(err);
521
522 /*
75a57bf0 523 * If there was no match we are going to append the pattern
1fd5e000
CF
524 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
525 * and the pattern did not contain any magic characters
526 * GLOB_NOMAGIC is there just for compatibility with csh.
527 */
733c9ed3
CV
528 if (pglob->gl_pathc == oldpathc) {
529 if (((pglob->gl_flags & GLOB_NOCHECK) ||
530 ((pglob->gl_flags & GLOB_NOMAGIC) &&
531 !(pglob->gl_flags & GLOB_MAGCHAR))))
532 return(globextend(pattern, pglob, limit));
533 else
534 return(GLOB_NOMATCH);
535 }
536 if (!(pglob->gl_flags & GLOB_NOSORT))
1fd5e000
CF
537 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
538 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
539 return(0);
540}
541
542static int
733c9ed3 543compare(const void *p, const void *q)
1fd5e000
CF
544{
545 return(strcmp(*(char **)p, *(char **)q));
546}
547
548static int
733c9ed3 549glob1(Char *pattern, glob_t *pglob, size_t *limit)
1fd5e000 550{
733c9ed3 551 Char pathbuf[MAXPATHLEN];
1fd5e000
CF
552
553 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
554 if (*pattern == EOS)
555 return(0);
733c9ed3
CV
556 return(glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
557 pattern, pglob, limit));
1fd5e000
CF
558}
559
560/*
561 * The functions glob2 and glob3 are mutually recursive; there is one level
562 * of recursion for each segment in the pattern that contains one or more
563 * meta characters.
564 */
565static int
733c9ed3
CV
566glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
567 glob_t *pglob, size_t *limit)
1fd5e000 568{
d7bcd2a1 569 struct stat sb;
1fd5e000
CF
570 Char *p, *q;
571 int anymeta;
572
573 /*
574 * Loop over pattern segments until end of pattern or until
575 * segment with meta character found.
576 */
577 for (anymeta = 0;;) {
578 if (*pattern == EOS) { /* End of pattern? */
579 *pathend = EOS;
580 if (g_lstat(pathbuf, &sb, pglob))
581 return(0);
75a57bf0 582
1fd5e000
CF
583 if (((pglob->gl_flags & GLOB_MARK) &&
584 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
585 || (S_ISLNK(sb.st_mode) &&
586 (g_stat(pathbuf, &sb, pglob) == 0) &&
587 S_ISDIR(sb.st_mode)))) {
733c9ed3
CV
588 if (pathend + 1 > pathend_last)
589 return (GLOB_ABORTED);
1fd5e000
CF
590 *pathend++ = SEP;
591 *pathend = EOS;
592 }
593 ++pglob->gl_matchc;
fa2d9fc5 594 return(globextend(pathbuf, pglob, limit));
1fd5e000
CF
595 }
596
597 /* Find end of next segment, copy tentatively to pathend. */
598 q = pathend;
599 p = pattern;
600 while (*p != EOS && *p != SEP) {
601 if (ismeta(*p))
602 anymeta = 1;
733c9ed3
CV
603 if (q + 1 > pathend_last)
604 return (GLOB_ABORTED);
1fd5e000
CF
605 *q++ = *p++;
606 }
607
608 if (!anymeta) { /* No expansion, do next segment. */
609 pathend = q;
610 pattern = p;
733c9ed3
CV
611 while (*pattern == SEP) {
612 if (pathend + 1 > pathend_last)
613 return (GLOB_ABORTED);
1fd5e000 614 *pathend++ = *pattern++;
733c9ed3 615 }
1fd5e000 616 } else /* Need expansion, recurse. */
733c9ed3
CV
617 return(glob3(pathbuf, pathend, pathend_last, pattern, p,
618 pglob, limit));
1fd5e000
CF
619 }
620 /* NOTREACHED */
621}
622
623static int
733c9ed3
CV
624glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
625 Char *pattern, Char *restpattern,
626 glob_t *pglob, size_t *limit)
1fd5e000 627{
733c9ed3 628 struct dirent *dp;
1fd5e000
CF
629 DIR *dirp;
630 int err;
631 char buf[MAXPATHLEN];
632
633 /*
634 * The readdirfunc declaration can't be prototyped, because it is
635 * assigned, below, to two functions which are prototyped in glob.h
636 * and dirent.h as taking pointers to differently typed opaque
637 * structures.
d7bcd2a1
CV
638 * CYGWIN: Needs prototype and subsequently wild casting to avoid
639 * compiler error.
1fd5e000 640 */
733c9ed3 641 struct dirent *(*readdirfunc)(void *);
1fd5e000 642
733c9ed3
CV
643 if (pathend > pathend_last)
644 return (GLOB_ABORTED);
1fd5e000
CF
645 *pathend = EOS;
646 errno = 0;
75a57bf0 647
1fd5e000
CF
648 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
649 /* TODO: don't call for ENOENT or ENOTDIR? */
650 if (pglob->gl_errfunc) {
733c9ed3
CV
651 if (g_Ctoc(pathbuf, buf, sizeof(buf)))
652 return (GLOB_ABORTED);
1fd5e000
CF
653 if (pglob->gl_errfunc(buf, errno) ||
654 pglob->gl_flags & GLOB_ERR)
733c9ed3 655 return (GLOB_ABORTED);
1fd5e000 656 }
d7bcd2a1 657 return(0);
1fd5e000
CF
658 }
659
660 err = 0;
661
662 /* Search directory for matching names. */
663 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
664 readdirfunc = pglob->gl_readdir;
665 else
733c9ed3 666 readdirfunc = (dirent*(*)(void*)) readdir;
1fd5e000 667 while ((dp = (*readdirfunc)(dirp))) {
733c9ed3
CV
668 char *sc;
669 Char *dc;
670 wchar_t wc;
671 size_t clen;
672 mbstate_t mbs;
1fd5e000
CF
673
674 /* Initial DOT must be matched literally. */
675 if (dp->d_name[0] == DOT && *pattern != DOT)
676 continue;
733c9ed3
CV
677 memset(&mbs, 0, sizeof(mbs));
678 dc = pathend;
679 sc = dp->d_name;
680 while (dc < pathend_last) {
681 clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
682 if (clen == (size_t)-1 || clen == (size_t)-2) {
683 wc = *sc;
684 clen = 1;
685 memset(&mbs, 0, sizeof(mbs));
686 }
687 if ((*dc++ = wc) == EOS)
688 break;
689 sc += clen;
690 }
1fd5e000
CF
691 if (!match(pathend, pattern, restpattern)) {
692 *pathend = EOS;
693 continue;
694 }
733c9ed3
CV
695 err = glob2(pathbuf, --dc, pathend_last, restpattern,
696 pglob, limit);
1fd5e000
CF
697 if (err)
698 break;
699 }
700
701 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
702 (*pglob->gl_closedir)(dirp);
703 else
704 closedir(dirp);
705 return(err);
706}
707
708
709/*
733c9ed3 710 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
1fd5e000
CF
711 * add the new item, and update gl_pathc.
712 *
713 * This assumes the BSD realloc, which only copies the block when its size
714 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
715 * behavior.
716 *
717 * Return 0 if new item added, error code if memory couldn't be allocated.
718 *
719 * Invariant of the glob_t structure:
720 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
721 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
722 */
723static int
733c9ed3 724globextend(const Char *path, glob_t *pglob, size_t *limit)
1fd5e000 725{
733c9ed3
CV
726 char **pathv;
727 size_t i, newsize, len;
1fd5e000
CF
728 char *copy;
729 const Char *p;
730
733c9ed3
CV
731 if (*limit && pglob->gl_pathc > *limit) {
732 errno = 0;
733 return (GLOB_NOSPACE);
734 }
735
1fd5e000 736 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
75a57bf0 737 pathv = pglob->gl_pathv ?
733c9ed3
CV
738 (char **) realloc((char *)pglob->gl_pathv, newsize) :
739 (char **) malloc(newsize);
740 if (pathv == NULL) {
741 if (pglob->gl_pathv) {
742 free(pglob->gl_pathv);
743 pglob->gl_pathv = NULL;
744 }
1fd5e000 745 return(GLOB_NOSPACE);
733c9ed3 746 }
1fd5e000
CF
747
748 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
749 /* first time around -- clear initial gl_offs items */
750 pathv += pglob->gl_offs;
733c9ed3 751 for (i = pglob->gl_offs + 1; --i > 0; )
1fd5e000
CF
752 *--pathv = NULL;
753 }
754 pglob->gl_pathv = pathv;
755
756 for (p = path; *p++;)
757 continue;
733c9ed3
CV
758 len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
759 if ((copy = (char *) malloc(len)) != NULL) {
760 if (g_Ctoc(path, copy, len)) {
761 free(copy);
762 return (GLOB_NOSPACE);
763 }
1fd5e000
CF
764 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
765 }
766 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
767 return(copy == NULL ? GLOB_NOSPACE : 0);
768}
769
1fd5e000
CF
770/*
771 * pattern matching function for filenames. Each occurrence of the *
772 * pattern causes a recursion level.
773 */
774static int
733c9ed3 775match(Char *name, Char *pat, Char *patend)
1fd5e000
CF
776{
777 int ok, negate_range;
778 Char c, k;
779
780 while (pat < patend) {
781 c = *pat++;
782 switch (c & M_MASK) {
783 case M_ALL:
784 if (pat == patend)
785 return(1);
75a57bf0 786 do
1fd5e000
CF
787 if (match(name, pat, patend))
788 return(1);
789 while (*name++ != EOS);
790 return(0);
791 case M_ONE:
792 if (*name++ == EOS)
793 return(0);
794 break;
795 case M_SET:
796 ok = 0;
797 if ((k = *name++) == EOS)
798 return(0);
799 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
800 ++pat;
d7bcd2a1 801 while (((c = *pat++) & M_MASK) != M_END)
733c9ed3 802 if ((*pat & M_MASK) == M_RNG) {
d7bcd2a1
CV
803 if (__collate_load_error ?
804 CCHAR(c) <= CCHAR(k) && CCHAR(k) <= CCHAR(pat[1]) :
805 __collate_range_cmp(CCHAR(c), CCHAR(k)) <= 0
806 && __collate_range_cmp(CCHAR(k), CCHAR(pat[1])) <= 0
807 )
808 ok = 1;
809 pat += 2;
733c9ed3 810 } else if (c == k)
d7bcd2a1 811 ok = 1;
1fd5e000
CF
812 if (ok == negate_range)
813 return(0);
814 break;
815 default:
d7bcd2a1 816 if (Cchar(*name++) != Cchar(c))
733c9ed3 817 return(0);
1fd5e000
CF
818 break;
819 }
820 }
821 return(*name == EOS);
822}
823
824/* Free allocated data belonging to a glob_t structure. */
825void
733c9ed3 826globfree(glob_t *pglob)
1fd5e000 827{
733c9ed3
CV
828 size_t i;
829 char **pp;
1fd5e000
CF
830
831 if (pglob->gl_pathv != NULL) {
832 pp = pglob->gl_pathv + pglob->gl_offs;
833 for (i = pglob->gl_pathc; i--; ++pp)
834 if (*pp)
835 free(*pp);
836 free(pglob->gl_pathv);
733c9ed3 837 pglob->gl_pathv = NULL;
1fd5e000
CF
838 }
839}
840
841static DIR *
733c9ed3 842g_opendir(Char *str, glob_t *pglob)
1fd5e000
CF
843{
844 char buf[MAXPATHLEN];
845
846 if (!*str)
847 strcpy(buf, ".");
733c9ed3
CV
848 else {
849 if (g_Ctoc(str, buf, sizeof(buf)))
850 return (NULL);
851 }
1fd5e000
CF
852
853 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
733c9ed3 854 return (DIR *) ((*pglob->gl_opendir)((const char *) buf));
1fd5e000
CF
855
856 return(opendir(buf));
857}
858
61522196
CV
859#ifdef __x86_64__
860#define CYGWIN_gl_stat(sfptr) ((*pglob->sfptr) (buf, sb))
861#else
2f263187 862static void
61522196 863stat32_to_stat64 (struct __stat32 *src, struct stat *dst)
2f263187
CV
864{
865 dst->st_dev = src->st_dev;
866 dst->st_ino = src->st_ino;
867 dst->st_mode = src->st_mode;
868 dst->st_nlink = src->st_nlink;
869 dst->st_uid = src->st_uid;
870 dst->st_gid = src->st_gid;
871 dst->st_rdev = src->st_rdev;
872 dst->st_size = src->st_size;
c4e6ff48
CV
873 dst->st_atim = src->st_atim;
874 dst->st_mtim = src->st_mtim;
875 dst->st_ctim = src->st_ctim;
7113f5da 876 dst->st_birthtim = src->st_mtim;
2f263187
CV
877 dst->st_blksize = src->st_blksize;
878 dst->st_blocks = src->st_blocks;
879}
880
d7bcd2a1
CV
881#define CYGWIN_gl_stat(sfptr) \
882 ({ int ret; \
883 struct __stat32 lsb; \
884 if (CYGWIN_VERSION_CHECK_FOR_USING_BIG_TYPES) \
885 ret = (*pglob->sfptr) (buf, sb); \
61522196 886 else if (!(ret = (*pglob->sfptr) (buf, (struct stat *) &lsb))) \
d7bcd2a1
CV
887 stat32_to_stat64 (&lsb, sb); \
888 ret; \
889 })
61522196 890#endif
d7bcd2a1 891
1fd5e000 892static int
d7bcd2a1 893g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
1fd5e000
CF
894{
895 char buf[MAXPATHLEN];
896
733c9ed3
CV
897 if (g_Ctoc(fn, buf, sizeof(buf))) {
898 errno = ENAMETOOLONG;
899 return (-1);
900 }
d7bcd2a1
CV
901 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
902 return CYGWIN_gl_stat (gl_lstat);
acb56175 903 return(lstat64(buf, sb));
1fd5e000
CF
904}
905
906static int
d7bcd2a1 907g_stat(Char *fn, struct stat *sb, glob_t *pglob)
1fd5e000
CF
908{
909 char buf[MAXPATHLEN];
910
733c9ed3
CV
911 if (g_Ctoc(fn, buf, sizeof(buf))) {
912 errno = ENAMETOOLONG;
913 return (-1);
914 }
d7bcd2a1
CV
915 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
916 return CYGWIN_gl_stat (gl_stat);
acb56175 917 return(stat64(buf, sb));
1fd5e000
CF
918}
919
d7bcd2a1
CV
920static const Char *
921g_strchr(const Char *str, wchar_t ch)
1fd5e000 922{
733c9ed3 923
1fd5e000
CF
924 do {
925 if (*str == ch)
926 return (str);
927 } while (*str++);
928 return (NULL);
929}
930
733c9ed3
CV
931static int
932g_Ctoc(const Char *str, char *buf, size_t len)
1fd5e000 933{
733c9ed3
CV
934 mbstate_t mbs;
935 size_t clen;
936
937 memset(&mbs, 0, sizeof(mbs));
938 while (len >= (size_t) MB_CUR_MAX) {
939 clen = wcrtomb(buf, *str, &mbs);
940 if (clen == (size_t)-1)
941 return (1);
942 if (*str == L'\0')
943 return (0);
944 str++;
945 buf += clen;
946 len -= clen;
947 }
948 return (1);
1fd5e000
CF
949}
950
951#ifdef DEBUG
75a57bf0 952static void
733c9ed3 953qprintf(const char *str, Char *s)
1fd5e000 954{
733c9ed3 955 Char *p;
1fd5e000
CF
956
957 (void)printf("%s:\n", str);
958 for (p = s; *p; p++)
959 (void)printf("%c", CHAR(*p));
960 (void)printf("\n");
961 for (p = s; *p; p++)
962 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
963 (void)printf("\n");
964 for (p = s; *p; p++)
965 (void)printf("%c", ismeta(*p) ? '_' : ' ');
966 (void)printf("\n");
967}
968#endif
This page took 0.508717 seconds and 5 git commands to generate.