xref: /netbsd-src/sys/lib/libkern/pmatch.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: pmatch.c,v 1.5 2005/12/11 12:24:37 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <lib/libkern/libkern.h>
34 /*
35  * pmatch():
36  *	Return 2 on exact match.
37  *	Return 1 on substring match.
38  *	Return 0 on no match.
39  *	Return -1 on error.
40  * *estr will point to the end of thelongest exact or substring match.
41  */
42 int
43 pmatch(string, pattern, estr)
44 	const char *string, *pattern, **estr;
45 {
46 	u_char stringc, patternc, rangec;
47 	int match, negate_range;
48 	const char *oestr, *pestr, *testr;
49 
50 	if (estr == NULL)
51 		estr = &testr;
52 
53 	for (;; ++string) {
54 		stringc = *string;
55 		switch (patternc = *pattern++) {
56 		case 0:
57 			*estr = string;
58 			return stringc == '\0' ? 2 : 1;
59 		case '?':
60 			if (stringc == '\0')
61 				return 0;
62 			*estr = string;
63 			break;
64 		case '*':
65 			if (!*pattern) {
66 				while (*string)
67 					string++;
68 				*estr = string;
69 				return 2;
70 			}
71 			oestr = *estr;
72 			pestr = NULL;
73 
74 			do {
75 				switch (pmatch(string, pattern, estr)) {
76 				case -1:
77 					return -1;
78 				case 0:
79 					break;
80 				case 1:
81 					pestr = *estr;
82 					break;
83 				case 2:
84 					return 2;
85 				default:
86 					return -1;
87 				}
88 				*estr = string;
89 			}
90 			while (*string++);
91 
92 			if (pestr) {
93 				*estr = pestr;
94 				return 1;
95 			} else {
96 				*estr = oestr;
97 				return 0;
98 			}
99 
100 		case '[':
101 			match = 0;
102 			if ((negate_range = (*pattern == '^')) != 0)
103 				pattern++;
104 			while ((rangec = *pattern++) != '\0') {
105 				if (rangec == ']')
106 					break;
107 				if (match)
108 					continue;
109 				if (rangec == '-' && *(pattern - 2) != '[' &&
110 				    *pattern != ']') {
111 					match =
112 					    stringc <= (u_char)*pattern &&
113 					    (u_char)*(pattern - 2) <= stringc;
114 					pattern++;
115 				} else
116 					match = (stringc == rangec);
117 			}
118 			if (rangec == 0)
119 				return -1;
120 			if (match == negate_range)
121 				return 0;
122 			*estr = string;
123 			break;
124 		default:
125 			if (patternc != stringc)
126 				return 0;
127 			*estr = string;
128 			break;
129 		}
130 	}
131 }
132