forked from aamine/stdlinux2-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grep.c
58 lines (51 loc) · 1.07 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
static void do_grep(regex_t *pat, FILE *f);
int
main(int argc, char *argv[])
{
regex_t pat;
int err;
int i;
if (argc < 2) {
fputs("no pattern\n", stderr);
exit(1);
}
err = regcomp(&pat, argv[1], REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
if (err != 0) {
char buf[1024];
regerror(err, &pat, buf, sizeof buf);
puts(buf);
exit(1);
}
if (argc == 2) {
do_grep(&pat, stdin);
}
else {
for (i = 2; i < argc; i++) {
FILE *f;
f = fopen(argv[i], "r");
if (!f) {
perror(argv[i]);
exit(1);
}
do_grep(&pat, f);
fclose(f);
}
}
regfree(&pat);
exit(0);
}
static void
do_grep(regex_t *pat, FILE *src)
{
char buf[4096];
while (fgets(buf, sizeof buf, src)) {
if (regexec(pat, buf, 0, NULL, 0) == 0) {
fputs(buf, stdout);
}
}
}