1/* 2 * Copyright (c) 1989, 1993, 1994 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. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#if defined(LIBC_SCCS) && !defined(lint) 38static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94"; 39#endif /* LIBC_SCCS and not lint */ 40 41/* 42 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6. 43 * Compares a filename or pathname to a pattern. 44 */ 45
| 1/* 2 * Copyright (c) 1989, 1993, 1994 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. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#if defined(LIBC_SCCS) && !defined(lint) 38static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94"; 39#endif /* LIBC_SCCS and not lint */ 40 41/* 42 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6. 43 * Compares a filename or pathname to a pattern. 44 */ 45
|
| 46#include <ctype.h>
|
46#include <fnmatch.h> 47#include <locale.h> 48#include <string.h>
| 47#include <fnmatch.h> 48#include <locale.h> 49#include <string.h>
|
| 50#include <stdio.h>
|
49 50#define EOS '\0' 51 52static const char *rangematch __P((const char *, int, int)); 53 54int 55fnmatch(pattern, string, flags) 56 const char *pattern, *string; 57 int flags; 58{ 59 const char *stringstart; 60 char c, test; 61 62 for (stringstart = string;;) 63 switch (c = *pattern++) { 64 case EOS: 65 return (*string == EOS ? 0 : FNM_NOMATCH); 66 case '?': 67 if (*string == EOS) 68 return (FNM_NOMATCH); 69 if (*string == '/' && (flags & FNM_PATHNAME)) 70 return (FNM_NOMATCH); 71 if (*string == '.' && (flags & FNM_PERIOD) && 72 (string == stringstart || 73 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 74 return (FNM_NOMATCH); 75 ++string; 76 break; 77 case '*': 78 c = *pattern; 79 /* Collapse multiple stars. */ 80 while (c == '*') 81 c = *++pattern; 82 83 if (*string == '.' && (flags & FNM_PERIOD) && 84 (string == stringstart || 85 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 86 return (FNM_NOMATCH); 87 88 /* Optimize for pattern with * at end or before /. */ 89 if (c == EOS) 90 if (flags & FNM_PATHNAME) 91 return (strchr(string, '/') == NULL ? 92 0 : FNM_NOMATCH); 93 else 94 return (0); 95 else if (c == '/' && flags & FNM_PATHNAME) { 96 if ((string = strchr(string, '/')) == NULL) 97 return (FNM_NOMATCH); 98 break; 99 } 100 101 /* General case, use recursion. */ 102 while ((test = *string) != EOS) { 103 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) 104 return (0); 105 if (test == '/' && flags & FNM_PATHNAME) 106 break; 107 ++string; 108 } 109 return (FNM_NOMATCH); 110 case '[': 111 if (*string == EOS) 112 return (FNM_NOMATCH); 113 if (*string == '/' && flags & FNM_PATHNAME) 114 return (FNM_NOMATCH); 115 if ((pattern = 116 rangematch(pattern, *string, flags)) == NULL) 117 return (FNM_NOMATCH); 118 ++string; 119 break; 120 case '\\': 121 if (!(flags & FNM_NOESCAPE)) { 122 if ((c = *pattern++) == EOS) { 123 c = '\\'; 124 --pattern; 125 } 126 } 127 /* FALLTHROUGH */ 128 default:
| 51 52#define EOS '\0' 53 54static const char *rangematch __P((const char *, int, int)); 55 56int 57fnmatch(pattern, string, flags) 58 const char *pattern, *string; 59 int flags; 60{ 61 const char *stringstart; 62 char c, test; 63 64 for (stringstart = string;;) 65 switch (c = *pattern++) { 66 case EOS: 67 return (*string == EOS ? 0 : FNM_NOMATCH); 68 case '?': 69 if (*string == EOS) 70 return (FNM_NOMATCH); 71 if (*string == '/' && (flags & FNM_PATHNAME)) 72 return (FNM_NOMATCH); 73 if (*string == '.' && (flags & FNM_PERIOD) && 74 (string == stringstart || 75 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 76 return (FNM_NOMATCH); 77 ++string; 78 break; 79 case '*': 80 c = *pattern; 81 /* Collapse multiple stars. */ 82 while (c == '*') 83 c = *++pattern; 84 85 if (*string == '.' && (flags & FNM_PERIOD) && 86 (string == stringstart || 87 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 88 return (FNM_NOMATCH); 89 90 /* Optimize for pattern with * at end or before /. */ 91 if (c == EOS) 92 if (flags & FNM_PATHNAME) 93 return (strchr(string, '/') == NULL ? 94 0 : FNM_NOMATCH); 95 else 96 return (0); 97 else if (c == '/' && flags & FNM_PATHNAME) { 98 if ((string = strchr(string, '/')) == NULL) 99 return (FNM_NOMATCH); 100 break; 101 } 102 103 /* General case, use recursion. */ 104 while ((test = *string) != EOS) { 105 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) 106 return (0); 107 if (test == '/' && flags & FNM_PATHNAME) 108 break; 109 ++string; 110 } 111 return (FNM_NOMATCH); 112 case '[': 113 if (*string == EOS) 114 return (FNM_NOMATCH); 115 if (*string == '/' && flags & FNM_PATHNAME) 116 return (FNM_NOMATCH); 117 if ((pattern = 118 rangematch(pattern, *string, flags)) == NULL) 119 return (FNM_NOMATCH); 120 ++string; 121 break; 122 case '\\': 123 if (!(flags & FNM_NOESCAPE)) { 124 if ((c = *pattern++) == EOS) { 125 c = '\\'; 126 --pattern; 127 } 128 } 129 /* FALLTHROUGH */ 130 default:
|
129 if (c != *string++)
| 131 if (c == *string) 132 ; 133 else if ((flags & FNM_ICASE) && 134 (tolower(c) == tolower(*string))) 135 ; 136 else
|
130 return (FNM_NOMATCH);
| 137 return (FNM_NOMATCH);
|
| 138 string++;
|
131 break; 132 } 133 /* NOTREACHED */ 134} 135 136static const char * 137rangematch(pattern, test, flags) 138 const char *pattern; 139 int test, flags; 140{ 141 int negate, ok; 142 char c, c2; 143 144 /* 145 * A bracket expression starting with an unquoted circumflex 146 * character produces unspecified results (IEEE 1003.2-1992, 147 * 3.13.2). This implementation treats it like '!', for 148 * consistency with the regular expression syntax. 149 * J.T. Conklin (conklin@ngai.kaleida.com) 150 */ 151 if ( (negate = (*pattern == '!' || *pattern == '^')) ) 152 ++pattern; 153
| 139 break; 140 } 141 /* NOTREACHED */ 142} 143 144static const char * 145rangematch(pattern, test, flags) 146 const char *pattern; 147 int test, flags; 148{ 149 int negate, ok; 150 char c, c2; 151 152 /* 153 * A bracket expression starting with an unquoted circumflex 154 * character produces unspecified results (IEEE 1003.2-1992, 155 * 3.13.2). This implementation treats it like '!', for 156 * consistency with the regular expression syntax. 157 * J.T. Conklin (conklin@ngai.kaleida.com) 158 */ 159 if ( (negate = (*pattern == '!' || *pattern == '^')) ) 160 ++pattern; 161
|
| 162 if (flags & FNM_ICASE) 163 test = tolower(test); 164
|
154 for (ok = 0; (c = *pattern++) != ']';) { 155 if (c == '\\' && !(flags & FNM_NOESCAPE)) 156 c = *pattern++; 157 if (c == EOS) 158 return (NULL);
| 165 for (ok = 0; (c = *pattern++) != ']';) { 166 if (c == '\\' && !(flags & FNM_NOESCAPE)) 167 c = *pattern++; 168 if (c == EOS) 169 return (NULL);
|
| 170 171 if (flags & FNM_ICASE) 172 c = tolower(c); 173
|
159 if (*pattern == '-' 160 && (c2 = *(pattern+1)) != EOS && c2 != ']') { 161 pattern += 2; 162 if (c2 == '\\' && !(flags & FNM_NOESCAPE)) 163 c2 = *pattern++; 164 if (c2 == EOS) 165 return (NULL);
| 174 if (*pattern == '-' 175 && (c2 = *(pattern+1)) != EOS && c2 != ']') { 176 pattern += 2; 177 if (c2 == '\\' && !(flags & FNM_NOESCAPE)) 178 c2 = *pattern++; 179 if (c2 == EOS) 180 return (NULL);
|
| 181 182 if (flags & FNM_ICASE) 183 c2 = tolower(c2); 184
|
166 if ( collate_range_cmp(c, test) <= 0 167 && collate_range_cmp(test, c2) <= 0 168 ) 169 ok = 1; 170 } else if (c == test) 171 ok = 1; 172 } 173 return (ok == negate ? NULL : pattern); 174}
| 185 if ( collate_range_cmp(c, test) <= 0 186 && collate_range_cmp(test, c2) <= 0 187 ) 188 ok = 1; 189 } else if (c == test) 190 ok = 1; 191 } 192 return (ok == negate ? NULL : pattern); 193}
|