]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/glob/fnmatch.c
0763609021538f07dc862f60bd42853dd8bc3373
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
22 #if !defined (__GNU_LIBRARY__) && !defined (STDC_HEADERS)
28 /* Match STRING against the filename pattern PATTERN, returning zero if
29 it matches, FNM_NOMATCH if not. */
31 fnmatch (pattern
, string
, flags
)
36 register char *p
= pattern
, *n
= string
;
39 if ((flags
& ~__FNM_FLAGS
) != 0)
45 while ((c
= *p
++) != '\0')
52 else if ((flags
& FNM_PATHNAME
) && *n
== '/')
54 else if ((flags
& FNM_PERIOD
) && *n
== '.' &&
55 (n
== string
|| ((flags
& FNM_PATHNAME
) && n
[-1] == '/')))
60 if (!(flags
& FNM_NOESCAPE
))
67 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
68 (n
== string
|| ((flags
& FNM_PATHNAME
) && n
[-1] == '/')))
71 for (c
= *p
++; c
== '?' || c
== '*'; c
= *p
++)
73 if (((flags
& FNM_PATHNAME
) && *n
== '/') ||
74 (c
== '?' && *n
== '\0'))
84 char c1
= (!(flags
& FNM_NOESCAPE
) && c
== '\\') ? *p
: c
;
85 for (--p
; *n
!= '\0'; ++n
)
86 if ((c
== '[' || *n
== c1
) &&
87 fnmatch (p
, n
, flags
& ~FNM_PERIOD
) == 0)
94 /* Nonzero if the sense of the character class is inverted. */
100 if ((flags
& FNM_PERIOD
) && *n
== '.' &&
101 (n
== string
|| ((flags
& FNM_PATHNAME
) && n
[-1] == '/')))
102 return (FNM_NOMATCH
);
104 /* Make sure there is a closing `]'. If there isn't, the `['
105 is just a character to be matched. */
109 for (np
= p
; np
&& *np
&& *np
!= ']'; np
++);
114 return (FNM_NOMATCH
);
119 not = (*p
== '!' || *p
== '^');
126 register char cstart
= c
, cend
= c
;
128 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
129 cstart
= cend
= *p
++;
132 /* [ (unterminated) loses. */
133 return (FNM_NOMATCH
);
137 if ((flags
& FNM_PATHNAME
) && c
== '/')
138 /* [/] can never match. */
139 return (FNM_NOMATCH
);
141 if (c
== '-' && *p
!= ']')
144 if (!(flags
& FNM_NOESCAPE
) && cend
== '\\')
147 return (FNM_NOMATCH
);
151 if (*n
>= cstart
&& *n
<= cend
)
158 return (FNM_NOMATCH
);
164 /* Skip the rest of the [...] that already matched. */
168 /* [... (unterminated) loses. */
169 return (FNM_NOMATCH
);
172 if (!(flags
& FNM_NOESCAPE
) && c
== '\\')
173 /* 1003.2d11 is unclear if this is right. %%% */
177 return (FNM_NOMATCH
);
183 return (FNM_NOMATCH
);
192 return (FNM_NOMATCH
);