1 /* $NetBSD: pmatch.c,v 1.2 2001/11/15 09:48:21 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1991 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/param.h> 37 #include <lib/libkern/libkern.h> 38 /* 39 * pmatch(): 40 * Return 2 on exact match. 41 * Return 1 on substring match. 42 * Return 0 on no match. 43 * Return -1 on error. 44 * *estr will point to the end of thelongest exact or substring match. 45 */ 46 int 47 pmatch(string, pattern, estr) 48 const char *string, *pattern, **estr; 49 { 50 u_char stringc, patternc, rangec; 51 int match, negate_range; 52 const char *oestr, *pestr, *testr; 53 54 if (estr == NULL) 55 estr = &testr; 56 57 for (;; ++string) { 58 stringc = *string; 59 switch (patternc = *pattern++) { 60 case 0: 61 *estr = string; 62 return stringc == '\0' ? 2 : 1; 63 case '?': 64 if (stringc == '\0') 65 return 0; 66 *estr = string; 67 break; 68 case '*': 69 if (!*pattern) { 70 while (*string) 71 string++; 72 *estr = string; 73 return 2; 74 } 75 oestr = *estr; 76 pestr = NULL; 77 78 do { 79 switch (pmatch(string, pattern, estr)) { 80 case -1: 81 return -1; 82 case 0: 83 break; 84 case 1: 85 pestr = *estr; 86 break; 87 case 2: 88 return 2; 89 default: 90 return -1; 91 } 92 *estr = string; 93 } 94 while (*string++); 95 96 if (pestr) { 97 *estr = pestr; 98 return 1; 99 } else { 100 *estr = oestr; 101 return 0; 102 } 103 104 case '[': 105 match = 0; 106 if ((negate_range = (*pattern == '^')) != 0) 107 pattern++; 108 while ((rangec = *pattern++) != '\0') { 109 if (rangec == ']') 110 break; 111 if (match) 112 continue; 113 if (rangec == '-' && *(pattern - 2) != '[' && 114 *pattern != ']') { 115 match = 116 stringc <= (u_char)*pattern && 117 (u_char)*(pattern - 2) <= stringc; 118 pattern++; 119 } else 120 match = (stringc == rangec); 121 } 122 if (rangec == 0) 123 return -1; 124 if (match == negate_range) 125 return 0; 126 *estr = string; 127 break; 128 default: 129 if (patternc != stringc) 130 return 0; 131 *estr = string; 132 break; 133 } 134 } 135 } 136