1 /* $NetBSD: pmatch.c,v 1.1 1998/06/21 18:43:35 christos 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 <sys/types.h> 38 #include <lib/libkern/libkern.h> 39 /* 40 * pmatch(): 41 * Return 2 on exact match. 42 * Return 1 on substring match. 43 * Return 0 on no match. 44 * Return -1 on error. 45 * *estr will point to the end of thelongest exact or substring match. 46 */ 47 int 48 pmatch(string, pattern, estr) 49 const char *string, *pattern, **estr; 50 { 51 u_char stringc, patternc, rangec; 52 int match, negate_range; 53 const char *oestr, *pestr, *testr; 54 55 if (estr == NULL) 56 estr = &testr; 57 58 for (;; ++string) { 59 stringc = *string; 60 switch (patternc = *pattern++) { 61 case 0: 62 *estr = string; 63 return stringc == '\0' ? 2 : 1; 64 case '?': 65 if (stringc == '\0') 66 return 0; 67 *estr = string; 68 break; 69 case '*': 70 if (!*pattern) { 71 while (*string) 72 string++; 73 *estr = string; 74 return 2; 75 } 76 oestr = *estr; 77 pestr = NULL; 78 79 do { 80 switch (pmatch(string, pattern, estr)) { 81 case -1: 82 return -1; 83 case 0: 84 break; 85 case 1: 86 pestr = *estr; 87 break; 88 case 2: 89 return 2; 90 default: 91 return -1; 92 } 93 *estr = string; 94 } 95 while (*string++); 96 97 if (pestr) { 98 *estr = pestr; 99 return 1; 100 } else { 101 *estr = oestr; 102 return 0; 103 } 104 105 case '[': 106 match = 0; 107 if ((negate_range = (*pattern == '^')) != 0) 108 pattern++; 109 while ((rangec = *pattern++) != '\0') { 110 if (rangec == ']') 111 break; 112 if (match) 113 continue; 114 if (rangec == '-' && *(pattern - 2) != '[' && 115 *pattern != ']') { 116 match = 117 stringc <= (u_char)*pattern && 118 (u_char)*(pattern - 2) <= stringc; 119 pattern++; 120 } else 121 match = (stringc == rangec); 122 } 123 if (rangec == 0) 124 return -1; 125 if (match == negate_range) 126 return 0; 127 *estr = string; 128 break; 129 default: 130 if (patternc != stringc) 131 return 0; 132 *estr = string; 133 break; 134 } 135 } 136 } 137