Added a comment, improved a comment, tweaked the recursive function's
[rsync/rsync.git] / lib / wildmatch.c
CommitLineData
446ee5b1
WD
1/*
2** Do shell-style pattern matching for ?, \, [], and * characters.
3** It is 8bit clean.
4**
5** Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
6** Rich $alz is now <rsalz@bbn.com>.
7**
7a1f46b6
WD
8** Modified by Wayne Davison to special-case '/' matching, to make '**'
9** work differently than '*', and to fix the character-class code.
446ee5b1
WD
10*/
11
12#include "rsync.h"
13
14/* What character marks an inverted character class? */
e11c4251
WD
15#define NEGATE_CLASS '!'
16#define NEGATE_CLASS2 '^'
446ee5b1 17
710faea9
WD
18#define FALSE 0
19#define TRUE 1
20b2e9ce
WD
20#define ABORT_ALL -1
21#define ABORT_TO_STARSTAR -2
22
5bb92000
WD
23#define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \
24 && *(class) == *(litmatch) \
136c5c5e 25 && strncmp((char*)class, litmatch, len) == 0)
e11c4251 26
f2ac84c3
WD
27#if defined STDC_HEADERS || !defined isascii
28# define ISASCII(c) 1
29#else
30# define ISASCII(c) isascii(c)
31#endif
32
33#ifdef isblank
34# define ISBLANK(c) (ISASCII(c) && isblank(c))
35#else
36# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
37#endif
38
39#ifdef isgraph
40# define ISGRAPH(c) (ISASCII(c) && isgraph(c))
41#else
42# define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c))
43#endif
44
45#define ISPRINT(c) (ISASCII(c) && isprint(c))
46#define ISDIGIT(c) (ISASCII(c) && isdigit(c))
47#define ISALNUM(c) (ISASCII(c) && isalnum(c))
48#define ISALPHA(c) (ISASCII(c) && isalpha(c))
49#define ISCNTRL(c) (ISASCII(c) && iscntrl(c))
50#define ISLOWER(c) (ISASCII(c) && islower(c))
51#define ISPUNCT(c) (ISASCII(c) && ispunct(c))
52#define ISSPACE(c) (ISASCII(c) && isspace(c))
53#define ISUPPER(c) (ISASCII(c) && isupper(c))
54#define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
55
d5c973cc
WD
56#ifdef WILD_TEST_ITERATIONS
57int wildmatch_iteration_count;
20b2e9ce 58#endif
446ee5b1 59
e725abcf
WD
60/* Match pattern "p" against string "text". */
61static int dowild(const uchar *p, const uchar *text)
446ee5b1
WD
62{
63 int matched, special;
5f238db2 64 uchar ch, prev;
446ee5b1 65
d5c973cc
WD
66#ifdef WILD_TEST_ITERATIONS
67 wildmatch_iteration_count++;
20b2e9ce
WD
68#endif
69
446ee5b1
WD
70 for ( ; (ch = *p) != '\0'; text++, p++) {
71 if (*text == '\0' && ch != '*')
710faea9 72 return FALSE;
446ee5b1
WD
73 switch (ch) {
74 case '\\':
75 /* Literal match with following character. Note that the test
76 * in "default" handles the p[1] == '\0' failure case. */
77 ch = *++p;
78 /* FALLTHROUGH */
79 default:
80 if (*text != ch)
710faea9 81 return FALSE;
446ee5b1
WD
82 continue;
83 case '?':
84 /* Match anything but '/'. */
85 if (*text == '/')
710faea9 86 return FALSE;
446ee5b1
WD
87 continue;
88 case '*':
89 if (*++p == '*') {
90 while (*++p == '*') {}
710faea9 91 special = TRUE;
e725abcf 92 } else
710faea9 93 special = FALSE;
446ee5b1 94 if (*p == '\0') {
710faea9
WD
95 /* Trailing "**" matches everything. Trailing "*" matches
96 * only if there are no more slash characters. */
136c5c5e 97 return special? TRUE : strchr((char*)text, '/') == NULL;
446ee5b1
WD
98 }
99 for ( ; *text; text++) {
e725abcf 100 if ((matched = dowild(p, text)) != FALSE) {
20b2e9ce
WD
101 if (!special || matched != ABORT_TO_STARSTAR)
102 return matched;
e725abcf 103 } else if (!special && *text == '/')
20b2e9ce 104 return ABORT_TO_STARSTAR;
446ee5b1 105 }
20b2e9ce 106 return ABORT_ALL;
446ee5b1 107 case '[':
c9a59880 108 ch = *++p;
e11c4251
WD
109#ifdef NEGATE_CLASS2
110 if (ch == NEGATE_CLASS2)
111 ch = NEGATE_CLASS;
112#endif
c9a59880
WD
113 /* Assign literal TRUE/FALSE because of "matched" comparison. */
114 special = ch == NEGATE_CLASS? TRUE : FALSE;
446ee5b1
WD
115 if (special) {
116 /* Inverted character class. */
c9a59880 117 ch = *++p;
446ee5b1
WD
118 }
119 prev = 0;
710faea9 120 matched = FALSE;
c9a59880 121 do {
446ee5b1 122 if (!ch)
5bb92000 123 return ABORT_ALL;
e11c4251
WD
124 if (ch == '\\') {
125 ch = *++p;
126 if (!ch)
5bb92000 127 return ABORT_ALL;
e11c4251
WD
128 if (*text == ch)
129 matched = TRUE;
e725abcf 130 } else if (ch == '-' && prev && p[1] && p[1] != ']') {
e11c4251
WD
131 ch = *++p;
132 if (ch == '\\') {
133 ch = *++p;
134 if (!ch)
5bb92000 135 return ABORT_ALL;
e11c4251
WD
136 }
137 if (*text <= ch && *text >= prev)
138 matched = TRUE;
139 ch = 0; /* This makes "prev" get set to 0. */
e725abcf 140 } else if (ch == '[' && p[1] == ':') {
5f238db2 141 const uchar *s;
e11c4251 142 int i;
5f238db2 143 for (s = p += 2; (ch = *p) && ch != ']'; p++) {}
e11c4251 144 if (!ch)
5bb92000 145 return ABORT_ALL;
fc96552d
WD
146 i = p - s - 1;
147 if (i < 0 || p[-1] != ':') {
148 /* Didn't find ":]", so treat like a normal set. */
149 p = s - 2;
150 ch = '[';
151 if (*text == ch)
152 matched = TRUE;
153 continue;
154 }
5bb92000
WD
155 if (CC_EQ(s,i, "alnum")) {
156 if (ISALNUM(*text))
157 matched = TRUE;
e725abcf 158 } else if (CC_EQ(s,i, "alpha")) {
5bb92000
WD
159 if (ISALPHA(*text))
160 matched = TRUE;
e725abcf 161 } else if (CC_EQ(s,i, "blank")) {
5bb92000
WD
162 if (ISBLANK(*text))
163 matched = TRUE;
e725abcf 164 } else if (CC_EQ(s,i, "cntrl")) {
5bb92000
WD
165 if (ISCNTRL(*text))
166 matched = TRUE;
e725abcf 167 } else if (CC_EQ(s,i, "digit")) {
5bb92000
WD
168 if (ISDIGIT(*text))
169 matched = TRUE;
e725abcf 170 } else if (CC_EQ(s,i, "graph")) {
5bb92000
WD
171 if (ISGRAPH(*text))
172 matched = TRUE;
e725abcf 173 } else if (CC_EQ(s,i, "lower")) {
5bb92000
WD
174 if (ISLOWER(*text))
175 matched = TRUE;
e725abcf 176 } else if (CC_EQ(s,i, "print")) {
5bb92000
WD
177 if (ISPRINT(*text))
178 matched = TRUE;
e725abcf 179 } else if (CC_EQ(s,i, "punct")) {
5bb92000
WD
180 if (ISPUNCT(*text))
181 matched = TRUE;
e725abcf 182 } else if (CC_EQ(s,i, "space")) {
5bb92000
WD
183 if (ISSPACE(*text))
184 matched = TRUE;
e725abcf 185 } else if (CC_EQ(s,i, "upper")) {
5bb92000
WD
186 if (ISUPPER(*text))
187 matched = TRUE;
e725abcf 188 } else if (CC_EQ(s,i, "xdigit")) {
5bb92000
WD
189 if (ISXDIGIT(*text))
190 matched = TRUE;
e725abcf 191 } else /* malformed [:class:] string */
5bb92000 192 return ABORT_ALL;
7a1f46b6 193 ch = 0; /* This makes "prev" get set to 0. */
e725abcf 194 } else if (*text == ch)
710faea9 195 matched = TRUE;
c9a59880 196 } while (prev = ch, (ch = *++p) != ']');
d811b689 197 if (matched == special || *text == '/')
710faea9 198 return FALSE;
446ee5b1
WD
199 continue;
200 }
201 }
202
203 return *text == '\0';
204}
710faea9 205
e725abcf
WD
206/* Match the "pattern" against the "text" string. */
207int wildmatch(const char *pattern, const char *text)
710faea9 208{
d5c973cc
WD
209#ifdef WILD_TEST_ITERATIONS
210 wildmatch_iteration_count = 0;
20b2e9ce 211#endif
e725abcf 212 return dowild((const uchar*)pattern, (const uchar*)text) == TRUE;
710faea9 213}