5 * wildmat.c - simple regular expression pattern matching routines
9 * These routines provide simple UNIX style regular expression matching.
10 * They were originally written by Rich Salz, the comp.sources.unix
11 * moderator for inclusion in some of his software. These routines
12 * were released into the public domain and used by John Gilmore in
17 * Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
18 * John Gilmore (gnu@hoptoad)
19 * Rich Salz (rs@uunet.uu.net)
22 * Sponsored by The USENIX Association for public distribution.
24 * Copyright (c) 1989 Mark H. Colburn.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms are permitted
28 * provided that the above copyright notice is duplicated in all such
29 * forms and that any documentation, advertising materials, and other
30 * materials related to such distribution and use acknowledge that the
31 * software was developed * by Mark H. Colburn and sponsored by The
34 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
35 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
36 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
39 * Revision 1.3 1999/06/08 02:01:29 ajc
40 * * Session no longer locks up when an express message is cancelled
41 * * The default Citadel server is now "localhost" instead of uncnsrd
42 * * Added some documentation
44 * Revision 1.2 1999/06/03 03:48:52 ajc
45 * * Ditched the frames mode completely. It wasn't working properly in,
46 * among other places, IE 5. Die, Bill, Die.
48 * Revision 1.1 1999/03/07 03:09:55 ajc
49 * * wildmat.c, braindamage.c: added
51 * Revision 1.1 88/12/23 18:02:41 mark
64 /* Function Prototypes */
67 static int star(char *, char *);
74 * star - handle trailing * in a regular expression
78 * Star is used to match filename expansions containing a trailing
79 * asterisk ('*'). Star call wildmat() to determine if the substring
80 * passed to it is matches the regular expression.
84 * char *source - The source string which is to be compared to the
85 * regular expression pattern.
86 * char *pattern - The regular expression which we are supposed to
91 * Returns non-zero if the entire source string is completely matched by
92 * the regular expression pattern, returns 0 otherwise. This is used to
93 * see if *'s in a pattern matched the entire source string.
99 static int star(char *source, char *pattern)
102 static int star(source, pattern)
103 char *source; /* source operand */
104 char *pattern; /* regular expression to match */
108 while (!wildmat(source, pattern)) {
109 if (*++source == '\0') {
118 * wildmat - match a regular expression
122 * Wildmat attempts to match the string pointed to by source to the
123 * regular expression pointed to by pattern. The subset of regular
124 * expression syntax which is supported is defined by POSIX P1003.2
125 * FILENAME EXPANSION rules.
129 * char *source - The source string which is to be compared to the
130 * regular expression pattern.
131 * char *pattern - The regular expression which we are supposed to
136 * Returns non-zero if the source string matches the regular expression
137 * pattern specified, returns 0 otherwise.
143 int wildmat(char *pattern, char *source)
146 int wildmat(pattern, source)
147 char *pattern; /* regular expression to match */
148 char *source; /* source operand */
152 int last; /* last character matched */
153 int matched; /* !0 if a match occurred */
154 int reverse; /* !0 if sense of match is reversed */
156 for (; *pattern; source++, pattern++) {
159 /* Literal match with following character */
163 if (*source != *pattern) {
168 /* Match anything. */
169 if (*source == '\0') {
174 /* Trailing star matches everything. */
175 return (*++pattern ? star(source, pattern) : 1);
177 /* [^....] means inverse character class. */
178 if ((reverse = pattern[1]) == '^') {
181 for (last = 0400, matched = 0;
182 *++pattern && *pattern != ']'; last = *pattern) {
183 /* This next line requires a good C compiler. */
185 ? *source <= *++pattern && *source >= last
186 : *source == *pattern) {
190 if (matched == reverse) {
198 * For "tar" use, matches that end at a slash also work. --hoptoad!gnu
200 return (*source == '\0' || *source == '/');