Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / crypto / kerberosIV / lib / roken / fnmatch.c
1 /*      $NetBSD: fnmatch.c,v 1.11 1995/02/27 03:43:06 cgd Exp $ */
2
3 /*
4  * Copyright (c) 1989, 1993, 1994
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  * @(#)fnmatch.c        8.2 (Berkeley) 4/16/94
39  * $NetBSD: fnmatch.c,v 1.11 1995/02/27 03:43:06 cgd Exp $
40  */
41
42 /*
43  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
44  * Compares a filename or pathname to a pattern.
45  */
46
47 #include <fnmatch.h>
48 #include <string.h>
49
50 #define EOS     '\0'
51
52 static const char *rangematch (const char *, int, int);
53
54 int
55 fnmatch(const char *pattern, const char *string, int flags)
56 {
57         const char *stringstart;
58         char c, test;
59
60         for (stringstart = string;;)
61                 switch (c = *pattern++) {
62                 case EOS:
63                         return (*string == EOS ? 0 : FNM_NOMATCH);
64                 case '?':
65                         if (*string == EOS)
66                                 return (FNM_NOMATCH);
67                         if (*string == '/' && (flags & FNM_PATHNAME))
68                                 return (FNM_NOMATCH);
69                         if (*string == '.' && (flags & FNM_PERIOD) &&
70                             (string == stringstart ||
71                             ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
72                                 return (FNM_NOMATCH);
73                         ++string;
74                         break;
75                 case '*':
76                         c = *pattern;
77                         /* Collapse multiple stars. */
78                         while (c == '*')
79                                 c = *++pattern;
80
81                         if (*string == '.' && (flags & FNM_PERIOD) &&
82                             (string == stringstart ||
83                             ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
84                                 return (FNM_NOMATCH);
85
86                         /* Optimize for pattern with * at end or before /. */
87                         if (c == EOS)
88                                 if (flags & FNM_PATHNAME)
89                                         return (strchr(string, '/') == NULL ?
90                                             0 : FNM_NOMATCH);
91                                 else
92                                         return (0);
93                         else if (c == '/' && flags & FNM_PATHNAME) {
94                                 if ((string = strchr(string, '/')) == NULL)
95                                         return (FNM_NOMATCH);
96                                 break;
97                         }
98
99                         /* General case, use recursion. */
100                         while ((test = *string) != EOS) {
101                                 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
102                                         return (0);
103                                 if (test == '/' && flags & FNM_PATHNAME)
104                                         break;
105                                 ++string;
106                         }
107                         return (FNM_NOMATCH);
108                 case '[':
109                         if (*string == EOS)
110                                 return (FNM_NOMATCH);
111                         if (*string == '/' && flags & FNM_PATHNAME)
112                                 return (FNM_NOMATCH);
113                         if ((pattern =
114                             rangematch(pattern, *string, flags)) == NULL)
115                                 return (FNM_NOMATCH);
116                         ++string;
117                         break;
118                 case '\\':
119                         if (!(flags & FNM_NOESCAPE)) {
120                                 if ((c = *pattern++) == EOS) {
121                                         c = '\\';
122                                         --pattern;
123                                 }
124                         }
125                         /* FALLTHROUGH */
126                 default:
127                         if (c != *string++)
128                                 return (FNM_NOMATCH);
129                         break;
130                 }
131         /* NOTREACHED */
132 }
133
134 static const char *
135 rangematch(const char *pattern, int test, int flags)
136 {
137         int negate, ok;
138         char c, c2;
139
140         /*
141          * A bracket expression starting with an unquoted circumflex
142          * character produces unspecified results (IEEE 1003.2-1992,
143          * 3.13.2).  This implementation treats it like '!', for
144          * consistency with the regular expression syntax.
145          * J.T. Conklin (conklin@ngai.kaleida.com)
146          */
147         if (negate = (*pattern == '!' || *pattern == '^'))
148                 ++pattern;
149         
150         for (ok = 0; (c = *pattern++) != ']';) {
151                 if (c == '\\' && !(flags & FNM_NOESCAPE))
152                         c = *pattern++;
153                 if (c == EOS)
154                         return (NULL);
155                 if (*pattern == '-' 
156                     && (c2 = *(pattern+1)) != EOS && c2 != ']') {
157                         pattern += 2;
158                         if (c2 == '\\' && !(flags & FNM_NOESCAPE))
159                                 c2 = *pattern++;
160                         if (c2 == EOS)
161                                 return (NULL);
162                         if (c <= test && test <= c2)
163                                 ok = 1;
164                 } else if (c == test)
165                         ok = 1;
166         }
167         return (ok == negate ? NULL : pattern);
168 }