Repository navigation
Expand file tree
/
Copy pathpattern.c
More file actions
160 lines (138 loc) · 2.56 KB
/
Copy pathpattern.c
File metadata and controls
160 lines (138 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* Copyright (c) 1985 Ceriel J.H. Jacobs */
# ifndef lint
static char rcsid[] = "$Header$";
# endif not lint
# define _PATTERN_
# include "in_all.h"
# include "pattern.h"
# include "getline.h"
# if V8_REGEX
# include <regexp.h>
# endif V8_REGEX
/*
* Interface to regular expression routines.
* Also: simple minded patterns without meta-characters.
*/
# if USG_REGEX
static char *pattern; /* Pointer to compiled pattern */
char *regcmp(), *regex();
# endif USG_REGEX
# if V8_REGEX
static struct regexp *pattern;
static char *rc_error;
struct regexp *regcomp();
# endif V8_REGEX
# if USG_REGEX || V8_REGEX
/*
* Compile a new pattern, but first free previous result.
*/
char *
re_comp(s) char *s; {
if (!*s) {
/*
* user wants previous pattern
*/
return (char *) 0;
}
if (pattern) {
/*
* there was a compiled pattern
*/
free(pattern);
pattern = 0;
}
# if USG_REGEX
return (pattern = regcmp(s, (char *) 0)) ?
(char *) 0 :
"Error in pattern";
# endif USG_REGEX
# if V8_REGEX
pattern = regcomp(s);
if (pattern) return (char *) 0;
if (rc_error) return rc_error;
return "Error in pattern";
# endif V8_REGEX
}
# if V8_REGEX
VOID
regerror(str) char *str; {
rc_error = str;
}
# endif V8_REGEX
/*
* Search for compiled pattern in string "s". Return 0 if not found.
*/
re_exec(s) char *s; {
# if USG_REGEX
return !(regex(pattern,s) == 0);
# endif USG_REGEX
# if V8_REGEX
# if _MINIX
return regexec(pattern,s,1);
# else
return regexec(pattern,s);
# endif
# endif V8_REGEX
}
# else
# ifndef BSD_REGEX
/*
* In this case, simple minded pattern search without meta-characters
*/
char *strcpy();
static char *pattern;
/*
* re_comp : Just remember pattern.
*/
char *
re_comp(s) char *s; {
if (!*s) {
/*
* User wants previous pattern
*/
if (!pattern) {
return "No previous regular expression";
}
return (char *) 0;
}
if (pattern) {
/*
* Free old pattern
*/
free(pattern);
}
pattern = alloc((unsigned) (strlen(s) + 1), 0);
(VOID) strcpy(pattern,s);
return (char *) 0;
}
/*
* re-exec : Simple minded pattern matcher
*/
re_exec(s) register char *s; {
register char *ppat, *pstr;
for (; *s; s++) {
/*
* As long as there are characters ...
*/
ppat = pattern; /* Try the pattern again */
pstr = s;
while (*ppat == *pstr) {
if (*++ppat == '\0') {
/*
* The pattern matched! Report success
*/
return 1;
}
if (*++pstr == '\0') {
/*
* Not enough characters left in the string.
* Report failure
*/
return 0;
}
}
}
return 0; /* Failure */
}
# endif not BSD_REGEX
# endif