-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathgrep.c
186 lines (167 loc) · 3.18 KB
/
grep.c
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* grep - search for a pattern Author: Martin C. Atkins */
/*
* Search files for a regular expression
*
*<-xtx-*>cc -o grep grep.c -lregexp
*/
/*
* This program was written by:
* Martin C. Atkins,
* University of York,
* Heslington,
* York. Y01 5DD
* England
* and is released into the public domain, on the condition
* that this comment is always included without alteration.
*
* The program was modified by Andy Tanenbaum.
*/
#include <regexp.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
void match(char *name, regexp *exp);
void pline(char *name, int lineno, char buf[]);
int _getline(char *buf, int size);
void usage();
void regdump(regexp *s);
#define MAXLINE (101)
int status = 1;
char *progname;
int pmflag = 1; /* print lines which match */
int pnmflag = 0; /* print lines which don't match */
int nflag = 0; /* number the lines printed */
int args;
int main(argc, argv) int argc;
char *argv[];
{
regexp *exp;
char **argp = &argv[1];
int fd;
args = argc;
progname = argv[0];
while (*argp != 0 && argp[0][0] == '-')
{
args--; /* flags don't count */
switch (argp[0][1])
{
case 'v':
pmflag = 0;
pnmflag = 1;
break;
case 'n':
nflag++;
break;
case 's':
pmflag = pnmflag = 0;
break;
case 'e':
argp++;
goto out;
default:
usage();
}
argp++;
}
out:
if (*argp == 0)
usage();
// printf("flags pm %d pnm %d n %d \n", pmflag, pnmflag, nflag);
if ((exp = regcomp(*argp++)) == NULL)
{
fprintf(stderr, "grep: regcomp failed\n");
exit(2);
}
if (*argp == 0){
match((char *)0, exp);
}else{
while (*argp)
{
if (strcmp(*argp, "-") == 0)
match("-", exp);
else
{
close(STDIN_FILENO);
if ((fd = open(*argp, O_RDONLY)) == -1)
{
fprintf(stderr, "Can't open %s\n", *argp);
status = 2;
}
else
{
// printf("opened %s in %d\n", *argp, fd);
match(*argp, exp);
}
}
argp++;
}
}
return status;
}
/*
* This routine actually matches the file
*/
void match(char *name, regexp *exp)
{
char buf[MAXLINE];
int lineno = 0, ret, lineret;
// printf("match %s\n", name);
// enable_syscall_tracing();
while ((lineret = _getline(buf, MAXLINE)) != -1)
{
char *cr = index(buf, '\n');
lineno++;
if (cr == 0)
{
fprintf(stderr, "Line too long in %s\n", name ? name : "stdin");
}
else{
*cr = '\0';
}
ret = regexec(exp, buf);
if (ret)
{
if (pmflag)
pline(name, lineno, buf);
if (status != 2)
status = 0;
}
else if (pnmflag){
pline(name, lineno, buf);
}
// printf("regexec %s ret %d\n", buf, ret);
}
// printf("line ret %d\n", lineret);
}
void pline(char *name, int lineno, char buf[])
{
if (name && args > 3)
printf("%s:", name);
if (nflag)
printf("%d:", lineno);
printf("%s\n", buf);
}
void usage()
{
fprintf(stderr, "Usage: grep [-v] [-n] [-s] [-e expr] expression [file ...]\n");
exit(2);
}
int _getline(char *buf, int size)
{
char *initbuf = buf, c;
while (1)
{
c = getc(stdin);
// printf("read %d %c\n", c, c);
*buf++ = c;
if (c < 0)
return (-1);
if (((buf - initbuf) == size - 1) || c == '\n'){
*buf = '\0';
return buf - initbuf;
}
}
return -1;
}