]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/glob/sm_loop.c
1 /* Copyright (C) 1991-2023 Free Software Foundation, Inc.
3 This file is part of GNU Bash, the Bourne Again SHell.
5 Bash is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Bash is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19 extern int interrupt_state
, terminating_signal
;
27 int FCT (CHAR
*, CHAR
*, int);
29 static int GMATCH (CHAR
*, CHAR
*, CHAR
*, CHAR
*, struct STRUCT
*, int);
30 static inline CHAR
*PARSE_SUBBRACKET (CHAR
*, int);
31 static CHAR
*BRACKMATCH (CHAR
*, U_CHAR
, int);
32 static int EXTMATCH (INT
, CHAR
*, CHAR
*, CHAR
*, CHAR
*, int);
34 extern void DEQUOTE_PATHNAME (CHAR
*);
36 /*static*/ CHAR
*PATSCAN (CHAR
*, CHAR
*, INT
, int);
39 FCT (CHAR
*pattern
, CHAR
*string
, int flags
)
43 if (string
== 0 || pattern
== 0)
46 se
= string
+ STRLEN ((XCHAR
*)string
);
47 pe
= pattern
+ STRLEN ((XCHAR
*)pattern
);
49 return (GMATCH (string
, se
, pattern
, pe
, (struct STRUCT
*)NULL
, flags
));
52 /* Match STRING against the filename pattern PATTERN, returning zero if
53 it matches, FNM_NOMATCH if not. */
55 GMATCH (CHAR
*string
, CHAR
*se
, CHAR
*pattern
, CHAR
*pe
, struct STRUCT
*ends
, int flags
)
57 CHAR
*p
, *n
; /* pattern, string */
58 INT c
; /* current pattern character - XXX U_CHAR? */
59 INT sc
; /* current string character - XXX U_CHAR? */
64 if (string
== 0 || pattern
== 0)
68 fprintf(stderr
, "gmatch: string = %s; se = %s\n", string
, se
);
69 fprintf(stderr
, "gmatch: pattern = %s; pe = %s\n", pattern
, pe
);
77 sc
= n
< se
? *n
: '\0';
79 if (interrupt_state
|| terminating_signal
)
83 /* EXTMATCH () will handle recursively calling GMATCH, so we can
84 just return what EXTMATCH() returns. */
85 if ((flags
& FNM_EXTMATCH
) && *p
== L('(') &&
86 (c
== L('+') || c
== L('*') || c
== L('?') || c
== L('@') || c
== L('!'))) /* ) */
89 /* If we're not matching the start of the string, we're not
90 concerned about the special cases for matching `.' */
91 lflags
= (n
== string
) ? flags
: (flags
& ~(FNM_PERIOD
|FNM_DOTDOT
));
92 return (EXTMATCH (c
, n
, se
, p
, pe
, lflags
));
94 #endif /* EXTENDED_GLOB */
98 case L('?'): /* Match single character */
101 else if ((flags
& FNM_PATHNAME
) && sc
== L('/'))
102 /* If we are matching a pathname, `?' can never match a `/'. */
104 else if ((flags
& FNM_PERIOD
) && sc
== L('.') &&
105 (n
== string
|| ((flags
& FNM_PATHNAME
) && n
[-1] == L('/'))))
106 /* `?' cannot match a `.' if it is the first character of the
107 string or if it is the first character following a slash and
108 we are matching a pathname. */
111 /* `?' cannot match `.' or `..' if it is the first character of the
112 string or if it is the first character following a slash and
113 we are matching a pathname. */
114 if ((flags
& FNM_DOTDOT
) &&
115 ((n
== string
&& SDOT_OR_DOTDOT(n
)) ||
116 ((flags
& FNM_PATHNAME
) && n
[-1] == L('/') && PDOT_OR_DOTDOT(n
))))
121 case L('\\'): /* backslash escape removes special meaning */
122 if (p
== pe
&& sc
== '\\' && (n
+1 == se
))
128 if ((flags
& FNM_NOESCAPE
) == 0)
131 /* A trailing `\' cannot match. */
136 if (FOLD (sc
) != (U_CHAR
)c
)
140 case L('*'): /* Match zero or more characters */
141 /* See below for the reason for using this. It avoids backtracking
142 back to a previous `*'. Picked up from glibc. */
145 ends
->pattern
= p
- 1;
150 if ((flags
& FNM_PERIOD
) && sc
== L('.') &&
151 (n
== string
|| ((flags
& FNM_PATHNAME
) && n
[-1] == L('/'))))
152 /* `*' cannot match a `.' if it is the first character of the
153 string or if it is the first character following a slash and
154 we are matching a pathname. */
157 /* `*' cannot match `.' or `..' if it is the first character of the
158 string or if it is the first character following a slash and
159 we are matching a pathname. */
160 if ((flags
& FNM_DOTDOT
) &&
161 ((n
== string
&& SDOT_OR_DOTDOT(n
)) ||
162 ((flags
& FNM_PATHNAME
) && n
[-1] == L('/') && PDOT_OR_DOTDOT(n
))))
168 /* Collapse multiple consecutive `*' and `?', but make sure that
169 one character of the string is consumed for each `?'. */
170 for (c
= *p
++; (c
== L('?') || c
== L('*')); c
= *p
++)
172 if ((flags
& FNM_PATHNAME
) && sc
== L('/'))
173 /* A slash does not match a wildcard under FNM_PATHNAME. */
176 else if ((flags
& FNM_EXTMATCH
) && c
== L('?') && *p
== L('(')) /* ) */
180 /* We can match 0 or 1 times. If we match, return success */
181 if (EXTMATCH (c
, n
, se
, p
, pe
, flags
) == 0)
184 /* We didn't match the extended glob pattern, but
185 that's OK, since we can match 0 or 1 occurrences.
186 We need to skip the glob pattern and see if we
187 match the rest of the string. */
188 newn
= PATSCAN (p
+ 1, pe
, 0, flags
);
189 /* If NEWN is 0, we have an ill-formed pattern. */
190 p
= newn
? newn
: pe
;
193 else if (c
== L('?'))
197 /* One character of the string is consumed in matching
198 this ? wildcard, so *??? won't match if there are
199 fewer than three characters. */
201 sc
= n
< se
? *n
: '\0';
205 /* Handle ******(patlist) */
206 if ((flags
& FNM_EXTMATCH
) && c
== L('*') && *p
== L('(')) /*)*/
209 /* We need to check whether or not the extended glob
210 pattern matches the remainder of the string.
211 If it does, we match the entire pattern. */
212 for (newn
= n
; newn
< se
; ++newn
)
214 if (EXTMATCH (c
, newn
, se
, p
, pe
, flags
) == 0)
217 /* We didn't match the extended glob pattern, but
218 that's OK, since we can match 0 or more occurrences.
219 We need to skip the glob pattern and see if we
220 match the rest of the string. */
221 newn
= PATSCAN (p
+ 1, pe
, 0, flags
);
222 /* If NEWN is 0, we have an ill-formed pattern. */
223 p
= newn
? newn
: pe
;
230 /* The wildcards are the last element of the pattern. The name
231 cannot match completely if we are looking for a pathname and
232 it contains another slash, unless FNM_LEADING_DIR is set. */
235 int r
= (flags
& FNM_PATHNAME
) == 0 ? 0 : FNM_NOMATCH
;
236 if (flags
& FNM_PATHNAME
)
238 if (flags
& FNM_LEADING_DIR
)
240 else if (MEMCHR (n
, L('/'), se
- n
) == NULL
)
246 /* If we've hit the end of the pattern and the last character of
247 the pattern was handled by the loop above, we've succeeded.
248 Otherwise, we need to match that last character. */
249 if (p
== pe
&& (c
== L('?') || c
== L('*')))
252 /* If we've hit the end of the string and the rest of the pattern
253 is something that matches the empty string, we can succeed. */
254 #if defined (EXTENDED_GLOB)
255 if (n
== se
&& ((flags
& FNM_EXTMATCH
) && (c
== L('!') || c
== L('?')) && *p
== L('(')))
258 if (EXTMATCH (c
, n
, se
, p
, pe
, flags
) == 0)
259 return (c
== L('!') ? FNM_NOMATCH
: 0);
260 return (c
== L('!') ? 0 : FNM_NOMATCH
);
264 /* If we stop at a slash in the pattern and we are looking for a
265 pathname ([star]/foo), then consume enough of the string to stop
266 at any slash and then try to match the rest of the pattern. If
267 the string doesn't contain a slash, fail */
268 if (c
== L('/') && (flags
& FNM_PATHNAME
))
270 while (n
< se
&& *n
!= L('/'))
272 if (n
< se
&& *n
== L('/') && (GMATCH (n
+1, se
, p
, pe
, NULL
, flags
) == 0))
274 return FNM_NOMATCH
; /* XXX */
277 /* General case, use recursion. */
284 endp
= MEMCHR (n
, (flags
& FNM_PATHNAME
) ? L('/') : L('\0'), se
- n
);
288 c1
= ((flags
& FNM_NOESCAPE
) == 0 && c
== L('\\')) ? *p
: c
;
290 for (--p
; n
< endp
; ++n
)
292 /* Only call strmatch if the first character indicates a
293 possible match. We can check the first character if
294 we're not doing an extended glob match. */
295 if ((flags
& FNM_EXTMATCH
) == 0 && c
!= L('[') && FOLD (*n
) != c1
) /*]*/
298 /* If we're doing an extended glob match and the pattern is not
299 one of the extended glob patterns, we can check the first
301 if ((flags
& FNM_EXTMATCH
) && p
[1] != L('(') && /*)*/
302 STRCHR (L("?*+@!"), *p
) == 0 && c
!= L('[') && FOLD (*n
) != c1
) /*]*/
305 /* Otherwise, we just recurse. */
306 if (GMATCH (n
, se
, p
, pe
, &end
, flags
& ~(FNM_PERIOD
|FNM_DOTDOT
)) == 0)
308 if (end
.pattern
== NULL
)
313 /* This is a clever idea from glibc, used to avoid backtracking
314 to a `*' that appears earlier in the pattern. We get away
315 without saving se and pe because they are always the same,
316 even in the recursive calls to gmatch */
317 if (end
.pattern
!= NULL
)
329 if (sc
== L('\0') || n
== se
)
332 /* A character class cannot match a `.' if it is the first
333 character of the string or if it is the first character
334 following a slash and we are matching a pathname. */
335 if ((flags
& FNM_PERIOD
) && sc
== L('.') &&
336 (n
== string
|| ((flags
& FNM_PATHNAME
) && n
[-1] == L('/'))))
337 return (FNM_NOMATCH
);
339 /* If we are matching pathnames, we can't match a slash with a
340 bracket expression. */
341 if (sc
== L('/') && (flags
& FNM_PATHNAME
))
342 return (FNM_NOMATCH
);
344 /* `?' cannot match `.' or `..' if it is the first character of the
345 string or if it is the first character following a slash and
346 we are matching a pathname. */
347 if ((flags
& FNM_DOTDOT
) &&
348 ((n
== string
&& SDOT_OR_DOTDOT(n
)) ||
349 ((flags
& FNM_PATHNAME
) && n
[-1] == L('/') && PDOT_OR_DOTDOT(n
))))
352 p
= BRACKMATCH (p
, sc
, flags
);
359 if ((U_CHAR
)c
!= FOLD (sc
))
360 return (FNM_NOMATCH
);
369 if ((flags
& FNM_LEADING_DIR
) && *n
== L('/'))
370 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
373 return (FNM_NOMATCH
);
376 #define SLASH_PATHNAME(c) (c == L('/') && (flags & FNM_PATHNAME))
378 /* Parse a special bracket expression symbol ([.sym.], [=char=], [:cclass:]),
379 starting at P, and return the position of the terminating .], =], or :].
380 P points to the character after the opening bracket. Returns NULL if the
381 symbol isn't correctly terminated. */
383 PARSE_SUBBRACKET (CHAR
*p
, int flags
)
385 CHAR type
; /* the type of special bracket expression symbol */
389 /* POSIX allows a right bracket to appear in a collating symbol. */
390 while (*++p
!= L('\0') && SLASH_PATHNAME (*p
) == 0 && (type
!= L('.') && *p
== L(']')) == 0)
391 if (*p
== type
&& p
[1] == L(']'))
396 /* Use prototype definition here because of type promotion. */
398 BRACKMATCH (CHAR
*p
, U_CHAR test
, int flags
)
400 register CHAR cstart
, cend
, c
;
401 register int not; /* Nonzero if the sense of the character class is inverted. */
402 int forcecoll
, isrange
;
409 test
= FOLD (orig_test
);
413 /* POSIX.2 3.13.1 says that an exclamation mark (`!') shall replace the
414 circumflex (`^') in its role in a `nonmatching list'. A bracket
415 expression starting with an unquoted circumflex character produces
416 unspecified results. This implementation treats the two identically. */
417 if (not = (*p
== L('!') || *p
== L('^')))
423 /* Initialize cstart and cend in case `-' is the last
424 character of the pattern. */
428 /* POSIX.2 equivalence class: [=c=]. See POSIX.2 2.8.3.2. Find
429 the end of the equivalence class, move the pattern pointer past
430 it, and check for equivalence. */
431 if (c
== L('[') && *p
== L('=') && (close
= PARSE_SUBBRACKET (p
, flags
)) != NULL
)
434 pc
= COLLSYM (p
, close
- p
);
438 if (COLLEQUIV (test
, pc
))
440 /*[*/ /* Move past the closing `]', since the first thing we do at
441 the `matched:' label is back p up one. */
449 return ((test
== L('[')) ? savep
: (CHAR
*)0); /*]*/
450 else if (c
== L('/') && (flags
& FNM_PATHNAME
))
451 return ((test
== L('[')) ? savep
: (CHAR
*)0); /*]*/
452 else if (c
== L(']'))
459 /* POSIX.2 character class expression. See POSIX.2 2.8.3.2. */
460 if (c
== L('[') && *p
== L(':') && (close
= PARSE_SUBBRACKET (p
, flags
)) != NULL
)
464 pc
= 0; /* make sure invalid char classes don't match. */
466 ccname
= (CHAR
*)malloc ((close
- p
) * sizeof (CHAR
));
469 bcopy (p
+ 1, ccname
, (close
- p
- 1) * sizeof (CHAR
));
470 *(ccname
+ (close
- p
- 1)) = L('\0');
471 /* As a result of a POSIX discussion, char class names are
472 allowed to be quoted (?) */
473 DEQUOTE_PATHNAME (ccname
);
474 pc
= IS_CCLASS (orig_test
, (XCHAR
*)ccname
);
477 /* CCNAME is not a valid character class in the current
478 locale. In addition to noting no match (pc = 0), we have
479 a choice about what to do with the invalid charclass.
480 Posix leaves the behavior unspecified, but we're going
481 to skip over the charclass and keep going instead of
482 testing ORIG_TEST against each character in the class
483 string. If we don't want to do that, take out the update
494 /*[*/ /* Move past the closing `]', since the first thing we do at
495 the `matched:' label is back p up one. */
501 /* continue the loop here, since this expression can't be
502 the first part of a range expression. */
505 return ((test
== L('[')) ? savep
: (CHAR
*)0);
506 else if (c
== L('/') && (flags
& FNM_PATHNAME
))
507 return ((test
== L('[')) ? savep
: (CHAR
*)0); /*]*/
508 else if (c
== L(']'))
515 /* POSIX.2 collating symbols. See POSIX.2 2.8.3.2. Find the end of
516 the symbol name, make sure it is terminated by `.]', translate
517 the name to a character using the external table, and do the
519 if (c
== L('[') && *p
== L('.') && (close
= PARSE_SUBBRACKET (p
, flags
)) != NULL
)
522 cstart
= COLLSYM (p
, close
- p
);
527 if (!(flags
& FNM_NOESCAPE
) && c
== L('\\'))
530 return ((test
== L('[')) ? savep
: (CHAR
*)0);
531 else if (*p
== L('/') && (flags
& FNM_PATHNAME
))
532 return ((test
== L('[')) ? savep
: (CHAR
*)0);
533 cstart
= cend
= *p
++;
536 cstart
= cend
= FOLD (cstart
);
539 /* POSIX.2 2.8.3.1.2 says: `An expression containing a `[' that
540 is not preceded by a backslash and is not part of a bracket
541 expression produces undefined results.' This implementation
542 treats the `[' as just a character to be matched if there is
543 not a closing `]'. */
545 return ((test
== L('[')) ? savep
: (CHAR
*)0);
547 /* POSIX.2 2.13.3 says: `If a <slash> character is found following an
548 unescaped <left-square-bracket> character before a corresponding
549 <right-square-bracket> is found, the open bracket shall be treated
550 as an ordinary character.' If we find a slash in a bracket
551 expression and the flags indicate we're supposed to be treating the
552 string like a pathname, we have to treat the `[' as just a character
554 if (c
== L('/') && (flags
& FNM_PATHNAME
))
555 return ((test
== L('[')) ? savep
: (CHAR
*)0);
561 return ((test
== L('[')) ? savep
: (CHAR
*)0);
562 else if (c
== L('/') && (flags
& FNM_PATHNAME
))
563 return ((test
== L('[')) ? savep
: (CHAR
*)0);
565 /* This introduces a range, unless the `-' is the last
566 character of the class. Find the end of the range
568 if (c
== L('-') && *p
!= L(']'))
571 if (!(flags
& FNM_NOESCAPE
) && cend
== L('\\'))
574 return ((test
== L('[')) ? savep
: (CHAR
*)0);
575 else if (cend
== L('/') && (flags
& FNM_PATHNAME
))
576 return ((test
== L('[')) ? savep
: (CHAR
*)0);
577 if (cend
== L('[') && *p
== L('.') && (close
= PARSE_SUBBRACKET (p
, flags
)) != NULL
)
580 cend
= COLLSYM (p
, close
- p
);
588 /* POSIX.2 2.8.3.2: ``The ending range point shall collate
589 equal to or higher than the starting range point; otherwise
590 the expression shall be treated as invalid.'' Note that this
591 applies to only the range expression; the rest of the bracket
592 expression is still checked for matches. */
593 if (RANGECMP (cstart
, cend
, forcecoll
) > 0)
603 if (isrange
== 0 && test
== cstart
)
605 if (isrange
&& RANGECMP (test
, cstart
, forcecoll
) >= 0 && RANGECMP (test
, cend
, forcecoll
) <= 0)
612 return (!not ? (CHAR
*)0 : p
);
615 /* Skip the rest of the [...] that already matched. */
619 /* A `[' without a matching `]' is just another character to match. */
621 return ((test
== L('[')) ? savep
: (CHAR
*)0);
622 else if (c
== L('/') && (flags
& FNM_PATHNAME
))
623 return ((test
== L('[')) ? savep
: (CHAR
*)0);
626 if (c
== L('[') && (*p
== L('=') || *p
== L(':') || *p
== L('.')))
628 if ((close
= PARSE_SUBBRACKET (p
, flags
)) != NULL
)
631 /* Left bracket loses its special meaning inside a bracket expression.
632 It is only valid when followed by a `.', `=', or `:', which we check
633 for above. Technically the right bracket can appear in a collating
634 symbol, so we check for that as well. The right brackets terminating
635 collating symbols, equivalence classes, or character classes are
636 processed by PARSE_SUBBRACKET. Otherwise, a right bracket terminates
637 the bracket expression. */
638 else if (c
== L(']'))
640 else if (!(flags
& FNM_NOESCAPE
) && c
== L('\\'))
643 return ((test
== L('[')) ? savep
: (CHAR
*)0);
644 /* We don't allow backslash to quote slash if we're matching pathnames */
645 else if (*p
== L('/') && (flags
& FNM_PATHNAME
))
646 return ((test
== L('[')) ? savep
: (CHAR
*)0);
647 /* Posix issue 8 leaves this unspecified for the shell. */
651 return (not ? (CHAR
*)0 : p
);
654 #if defined (EXTENDED_GLOB)
655 /* ksh-like extended pattern matching:
659 where pat-list is a list of one or patterns separated by `|'. Operation
662 ?(patlist) match zero or one of the given patterns
663 *(patlist) match zero or more of the given patterns
664 +(patlist) match one or more of the given patterns
665 @(patlist) match exactly one of the given patterns
666 !(patlist) match anything except one of the given patterns
669 /* Scan a pattern starting at STRING and ending at END, keeping track of
670 embedded () and []. If DELIM is 0, we scan until a matching `)'
671 because we're scanning a `patlist'. Otherwise, we scan until we see
672 DELIM. In all cases, we never scan past END. The return value is the
673 first character after the matching DELIM or NULL if the pattern is
676 PATSCAN (CHAR
*string
, CHAR
*end
, INT delim
, int flags
)
678 int pnest
, bnest
, skip
;
679 CHAR
*s
, c
, *bfirst
, *t
;
681 pnest
= bnest
= skip
= 0;
687 for (s
= string
; c
= *s
; s
++)
699 if ((flags
& FNM_NOESCAPE
) == 0)
704 return ((CHAR
*)NULL
);
706 /* `[' is not special inside a bracket expression, but it may
707 introduce one of the special POSIX bracket expressions
708 ([.SYM.], [=c=], [: ... :]) that needs special handling. */
713 if (*bfirst
== L('!') || *bfirst
== L('^'))
717 else if (s
[1] == L(':') || s
[1] == L('.') || s
[1] == L('='))
719 t
= PARSE_SUBBRACKET (s
+ 1, flags
);
721 s
= t
+ 2 - 1; /* -1 to cancel s++ in loop above */
725 /* `]' is not special if it's the first char (after a leading `!'
726 or `^') in a bracket expression or if it's part of one of the
727 special POSIX bracket expressions ([.SYM.], [=c=], [: ... :]) */
745 if (bnest
== 0 && pnest
-- <= 0)
750 if (bnest
== 0 && pnest
== 0 && delim
== L('|'))
759 /* Return 0 if dequoted pattern matches S in the current locale. */
761 STRCOMPARE (CHAR
*p
, CHAR
*pe
, CHAR
*s
, CHAR
*se
)
771 return (FNM_NOMATCH
); /* unequal lengths, can't be identical */
781 #if HAVE_MULTIBYTE || defined (HAVE_STRCOLL)
782 ret
= STRCOLL ((XCHAR
*)p
, (XCHAR
*)s
);
784 ret
= STRCMP ((XCHAR
*)p
, (XCHAR
*)s
);
792 return (ret
== 0 ? ret
: FNM_NOMATCH
);
795 /* Match a ksh extended pattern specifier. Return FNM_NOMATCH on failure or
796 0 on success. This is handed the entire rest of the pattern and string
797 the first time an extended pattern specifier is encountered, so it calls
798 gmatch recursively. */
800 EXTMATCH (INT xc
, CHAR
*s
, CHAR
*se
, CHAR
*p
, CHAR
*pe
, int flags
)
802 CHAR
*prest
; /* pointer to rest of pattern */
803 CHAR
*psub
; /* pointer to sub-pattern */
804 CHAR
*pnext
; /* pointer to next sub-pattern */
805 CHAR
*srest
; /* pointer to rest of string */
806 int m1
, m2
, xflags
; /* xflags = flags passed to recursive matches */
809 fprintf(stderr
, "extmatch: xc = %c\n", xc
);
810 fprintf(stderr
, "extmatch: s = %s; se = %s\n", s
, se
);
811 fprintf(stderr
, "extmatch: p = %s; pe = %s\n", p
, pe
);
812 fprintf(stderr
, "extmatch: flags = %d\n", flags
);
815 prest
= PATSCAN (p
+ (*p
== L('(')), pe
, 0, flags
); /* ) */
817 /* If PREST is 0, we failed to scan a valid pattern. In this
818 case, we just want to compare the two as strings. */
819 return (STRCOMPARE (p
- 1, pe
, s
, se
));
821 glob_recursion_depth
++;
825 case L('+'): /* match one or more occurrences */
826 case L('*'): /* match zero or more occurrences */
827 /* If we can get away with no matches, don't even bother. Just
828 call GMATCH on the rest of the pattern and return success if
830 if (xc
== L('*') && (GMATCH (s
, se
, prest
, pe
, NULL
, flags
) == 0))
833 /* OK, we have to do this the hard way. First, we make sure one of
834 the subpatterns matches, then we try to match the rest of the
836 for (psub
= p
+ 1; ; psub
= pnext
)
838 pnext
= PATSCAN (psub
, pe
, L('|'), flags
);
839 for (srest
= s
; srest
<= se
; srest
++)
841 /* Match this substring (S -> SREST) against this
842 subpattern (psub -> pnext - 1) */
843 m1
= GMATCH (s
, srest
, psub
, pnext
- 1, NULL
, flags
) == 0;
844 /* OK, we matched a subpattern, so make sure the rest of the
845 string matches the rest of the pattern. Also handle
846 multiple matches of the pattern. */
849 /* if srest > s, we are not at start of string */
850 xflags
= (srest
> s
) ? (flags
& ~(FNM_PERIOD
|FNM_DOTDOT
)) : flags
;
851 m2
= (GMATCH (srest
, se
, prest
, pe
, NULL
, xflags
) == 0) ||
852 (s
!= srest
&& GMATCH (srest
, se
, p
- 1, pe
, NULL
, xflags
) == 0);
860 return (FNM_NOMATCH
);
862 case L('?'): /* match zero or one of the patterns */
863 case L('@'): /* match one (or more) of the patterns */
864 /* If we can get away with no matches, don't even bother. Just
865 call gmatch on the rest of the pattern and return success if
867 if (xc
== L('?') && (GMATCH (s
, se
, prest
, pe
, NULL
, flags
) == 0))
870 /* OK, we have to do this the hard way. First, we see if one of
871 the subpatterns matches, then, if it does, we try to match the
872 rest of the string. */
873 for (psub
= p
+ 1; ; psub
= pnext
)
875 pnext
= PATSCAN (psub
, pe
, L('|'), flags
);
876 srest
= (prest
== pe
) ? se
: s
;
877 for ( ; srest
<= se
; srest
++)
879 /* if srest > s, we are not at start of string */
880 xflags
= (srest
> s
) ? (flags
& ~(FNM_PERIOD
|FNM_DOTDOT
)) : flags
;
881 if (GMATCH (s
, srest
, psub
, pnext
- 1, NULL
, flags
) == 0 &&
882 GMATCH (srest
, se
, prest
, pe
, NULL
, xflags
) == 0)
888 return (FNM_NOMATCH
);
890 case '!': /* match anything *except* one of the patterns */
891 for (srest
= s
; srest
<= se
; srest
++)
894 for (psub
= p
+ 1; ; psub
= pnext
)
896 pnext
= PATSCAN (psub
, pe
, L('|'), flags
);
897 /* If one of the patterns matches, just bail immediately. */
898 if (m1
= (GMATCH (s
, srest
, psub
, pnext
- 1, NULL
, flags
) == 0))
904 /* If nothing matched, but the string starts with a period and we
905 need to match periods explicitly, don't return this as a match,
906 even for negation. */
907 if (m1
== 0 && (flags
& FNM_PERIOD
) && *s
== '.')
908 return (FNM_NOMATCH
);
910 if (m1
== 0 && (flags
& FNM_DOTDOT
) &&
911 (SDOT_OR_DOTDOT (s
) ||
912 ((flags
& FNM_PATHNAME
) && s
[-1] == L('/') && PDOT_OR_DOTDOT(s
))))
913 return (FNM_NOMATCH
);
915 /* if srest > s, we are not at start of string */
916 xflags
= (srest
> s
) ? (flags
& ~(FNM_PERIOD
|FNM_DOTDOT
)) : flags
;
917 if (m1
== 0 && GMATCH (srest
, se
, prest
, pe
, NULL
, xflags
) == 0)
920 return (FNM_NOMATCH
);
923 return (FNM_NOMATCH
);
925 #endif /* EXTENDED_GLOB */
937 #undef PARSE_SUBBRACKET
941 #undef DEQUOTE_PATHNAME
953 #undef PDOT_OR_DOTDOT
954 #undef SDOT_OR_DOTDOT