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.1 1999/03/07 03:09:55 ajc
40 * * wildmat.c, braindamage.c: added
42 * Revision 1.1 88/12/23 18:02:41 mark
48 static char *ident = "$Id$";
49 static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
61 /* Function Prototypes */
64 static int star(char *, char *);
71 * star - handle trailing * in a regular expression
75 * Star is used to match filename expansions containing a trailing
76 * asterisk ('*'). Star call wildmat() to determine if the substring
77 * passed to it is matches the regular expression.
81 * char *source - The source string which is to be compared to the
82 * regular expression pattern.
83 * char *pattern - The regular expression which we are supposed to
88 * Returns non-zero if the entire source string is completely matched by
89 * the regular expression pattern, returns 0 otherwise. This is used to
90 * see if *'s in a pattern matched the entire source string.
96 static int star(char *source, char *pattern)
99 static int star(source, pattern)
100 char *source; /* source operand */
101 char *pattern; /* regular expression to match */
105 while (!wildmat(source, pattern)) {
106 if (*++source == '\0') {
115 * wildmat - match a regular expression
119 * Wildmat attempts to match the string pointed to by source to the
120 * regular expression pointed to by pattern. The subset of regular
121 * expression syntax which is supported is defined by POSIX P1003.2
122 * FILENAME EXPANSION rules.
126 * char *source - The source string which is to be compared to the
127 * regular expression pattern.
128 * char *pattern - The regular expression which we are supposed to
133 * Returns non-zero if the source string matches the regular expression
134 * pattern specified, returns 0 otherwise.
140 int wildmat(char *pattern, char *source)
143 int wildmat(pattern, source)
144 char *pattern; /* regular expression to match */
145 char *source; /* source operand */
149 int last; /* last character matched */
150 int matched; /* !0 if a match occurred */
151 int reverse; /* !0 if sense of match is reversed */
153 for (; *pattern; source++, pattern++) {
156 /* Literal match with following character */
160 if (*source != *pattern) {
165 /* Match anything. */
166 if (*source == '\0') {
171 /* Trailing star matches everything. */
172 return (*++pattern ? star(source, pattern) : 1);
174 /* [^....] means inverse character class. */
175 if (reverse = pattern[1] == '^') {
178 for (last = 0400, matched = 0;
179 *++pattern && *pattern != ']'; last = *pattern) {
180 /* This next line requires a good C compiler. */
182 ? *source <= *++pattern && *source >= last
183 : *source == *pattern) {
187 if (matched == reverse) {
195 * For "tar" use, matches that end at a slash also work. --hoptoad!gnu
197 return (*source == '\0' || *source == '/');