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
|
#define _XOPEN_SOURCE 700
#include <errno.h>
#include <limits.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
enum {
FLAG_c = 1 << 0,
FLAG_l = 1 << 1,
FLAG_n = 1 << 2,
FLAG_v = 1 << 3,
};
int
match(const char *string, regex_t *re) {
int status = regexec(re, string, 0, NULL, 0);
if (status != 0)
return 0;
return 1;
}
int
grep(FILE *f, regex_t *re, int flags, char *fname) {
size_t len = LINE_MAX;
ssize_t bytes;
char *buf = malloc(len);
if (buf == NULL) {
fprintf(stderr, "grep: %s\n", strerror(errno));
return 1;
}
int num_matches = 0;
int lineno = 0;
while ((bytes = getline(&buf, &len, f)) != -1) {
lineno++;
int matches = match(buf, re);
if ((matches && ! (flags & FLAG_v)) || (! matches && (flags & FLAG_v))) {
num_matches++;
if (! (flags & FLAG_c) && ! (flags & FLAG_l)) {
if (fname)
printf("%s:", fname);
if (flags & FLAG_n)
printf("%d:", lineno);
printf("%s", buf);
}
}
}
if (flags & FLAG_l) {
printf("%s\n", fname);
} else if (flags & FLAG_c) {
if (fname)
printf("%s:", fname);
printf("%d\n", num_matches);
}
if (ferror(f)) {
fprintf(stderr, "grep: %s\n", strerror(errno));
return 1;
}
free(buf);
if (num_matches == 0)
return 1;
return 0;
}
void
print_usage(void) {
fprintf(stderr, "usage: grep [-E|-F] [-cilnv] regexp [file...]\n");
}
int
main(int argc, char **argv) {
int ret = 1;
int c;
int REGFLAGS = REG_NOSUB;
int flags = 0;
while ((c = getopt(argc, argv, "EFcilnv")) != -1) {
switch (c) {
case 'E':
REGFLAGS |= REG_EXTENDED;
break;
case 'F':
REGFLAGS &= ~REG_EXTENDED;
break;
case 'c':
flags |= FLAG_c;
flags &= ~FLAG_l;
break;
case 'i':
REGFLAGS |= REG_ICASE;
break;
case 'l':
flags |= FLAG_l;
flags &= ~FLAG_c;
break;
case 'n':
flags |= FLAG_n;
break;
case 'v':
flags |= FLAG_v;
break;
case '?':
print_usage();
return 1;
}
}
argc -= optind;
argv += optind - 1;
if (argc == 0) {
print_usage();
return 1;
}
/* TODO: allow '--' after pattern_list
* make a list of regex_ts to do for the -e and -f options
*/
char *regexp = *++argv;
regex_t re;
int regerr = regcomp(&re, regexp, REGFLAGS);
if (regerr != 0) {
char errbuf[100] = {'\0'};
regerror(regerr, &re, errbuf, 100);
fprintf(stderr, "grep: bad regex: %s\n", errbuf);
regfree(&re);
return 1;
}
if (argc == 1) {
ret = grep(stdin, &re, flags, "(standardinput)");
} else {
FILE *f;
while (*++argv) {
char *fname = NULL;
if (**argv == '-' && *(*argv + 1) == '\0') {
f = stdin;
fname = "(standardinput)";
} else {
f = fopen(*argv, "r");
if (f == NULL) {
fprintf(stderr, "grep: %s: %s\n", *argv, strerror(errno));
ret = 1;
continue;
}
fname = *argv;
}
if (argc == 2)
fname = NULL;
/* grep only returns 1 or 0. */
ret *= grep(f, &re, flags, fname);
if (f != stdin && fclose(f) == EOF) {
fprintf(stderr, "grep: %s: %s\n", *argv, strerror(errno));
ret = 1;
continue;
}
}
}
regfree(&re);
return ret;
}
|