2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: wildmat.c,v 1.2 94/04/13 18:40:59 rws Exp $ */
26 ** Do shell-style pattern matching for ?, \, [], and * characters.
27 ** Might not be robust in face of malformed patterns; e.g., "foo[a-"
28 ** could cause a segmentation violation. It is 8bit clean.
30 ** Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
31 ** Rich $alz is now <rsalz@bbn.com>.
32 ** April, 1991: Replaced mutually-recursive calls with in-line code
33 ** for the star character.
35 ** Special thanks to Lars Mathiesen <thorinn@diku.dk> for the ABORT code.
36 ** This can greatly speed up failing wildcard patterns. For example:
37 ** pattern: -*-*-*-*-*-*-12-*-*-*-m-*-*-*
38 ** text 1: -adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1
39 ** text 2: -adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1
40 ** Text 1 matches with 51 calls, while text 2 fails with 54 calls. Without
41 ** the ABORT, then it takes 22310 calls to fail. Ugh. The following
42 ** explanation is from Lars:
43 ** The precondition that must be fulfilled is that DoMatch will consume
44 ** at least one character in text. This is true if *p is neither '*' nor
45 ** '\0'.) The last return has ABORT instead of FALSE to avoid quadratic
46 ** behaviour in cases like pattern "*a*b*c*d" with text "abcxxxxx". With
47 ** FALSE, each star-loop has to run to the end of the text; with ABORT
48 ** only the last one does.
50 ** Once the control of one instance of DoMatch enters the star-loop, that
51 ** instance will return either TRUE or ABORT, and any calling instance
52 ** will therefore return immediately after (without calling recursively
53 ** again). In effect, only one star-loop is ever active. It would be
54 ** possible to modify the code to maintain this context explicitly,
55 ** eliminating all recursive calls at the cost of some complication and
56 ** loss of clarity (and the ABORT stuff seems to be unclear enough by
57 ** itself). I think it would be unwise to try to get this into a
58 ** released version unless you have a good test data base to try it out
67 /* What character marks an inverted character class? */
68 #define NEGATE_CLASS '^'
69 /* Is "*" a common pattern? */
70 #define OPTIMIZE_JUST_STAR
71 /* Do tar(1) matching rules, which ignore a trailing slash? */
72 #undef MATCH_TAR_PATTERN
76 ** Match text and p, return TRUE, FALSE, or ABORT.
87 for ( ; *p; text++, p++) {
88 if (*text == '\0' && *p != '*')
92 /* Literal match with following character. */
100 /* Match anything. */
104 /* Consecutive stars act just like one. */
107 /* Trailing star matches everything. */
110 if ((matched = DoMatch(text++, p)) != FALSE)
114 reverse = p[1] == NEGATE_CLASS ? TRUE : FALSE;
116 /* Inverted character class. */
118 for (last = 0400, matched = FALSE; *++p && *p != ']'; last = *p)
119 /* This next line requires a good C compiler. */
120 if (*p == '-' ? *text <= *++p && *text >= last : *text == *p)
122 if (matched == reverse)
128 #ifdef MATCH_TAR_PATTERN
131 #endif /* MATCH_TAR_ATTERN */
132 return *text == '\0';
137 ** User-level routine. Returns TRUE or FALSE.
144 #ifdef OPTIMIZE_JUST_STAR
145 if (p[0] == '*' && p[1] == '\0')
147 #endif /* OPTIMIZE_JUST_STAR */
148 return DoMatch(text, p) == TRUE;
156 /* Yes, we use gets not fgets. Sue me. */
165 printf("Wildmat tester. Enter pattern, then strings to test.\n");
166 printf("A blank line gets prompts for a new pattern; a blank pattern\n");
167 printf("exits the program.\n");
170 printf("\nEnter pattern: ");
171 (void)fflush(stdout);
172 if (gets(p) == NULL || p[0] == '\0')
175 printf("Enter text: ");
176 (void)fflush(stdout);
177 if (gets(text) == NULL)
180 /* Blank line; go back and get a new pattern. */
182 printf(" %s\n", wildmat(text, p) ? "YES" : "NO");