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