/* match.c: pattern matching routines */
static int rangematch(char *, char);
enum { RANGE_FAIL
= -1, RANGE_ERROR
= -2 };
/* match() matches a single pattern against a single string. */
extern bool match(char *p
, char *m
, char *s
) {
while (p
[i
] == '*' && m
[i
] == 1) /* collapse multiple stars */
if (p
[i
] == '\0') /* star at end of pattern? */
if (match(p
+ i
, m
+ i
, s
++))
switch (j
= rangematch(p
+ i
, *s
)) {
panic("bad metacharacter in match");
return FALSE
; /* hush up gcc -Wall */
} else if (p
[i
++] != *s
++)
From the ed(1) man pages (on ranges):
The `-' is treated as an ordinary character if it occurs first
(or first after an initial ^) or last in the string.
The right square bracket does not terminate the enclosed string
if it is the first character (after an initial `^', if any), in
rangematch() matches a single character against a class, and returns
an integer offset to the end of the range on success, or -1 on
static int rangematch(char *p
, char c
) {
return RANGE_ERROR
; /* bad syntax */
if (p
[1] == '-' && p
[2] != ']') { /* check for [..-..] but ignore [..-] */
return p
- orig
+ 1; /* skip the right-bracket */