-
Notifications
You must be signed in to change notification settings - Fork 13
/
grep.app.c
62 lines (56 loc) · 1.02 KB
/
grep.app.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
#include "usyscall.h"
char *strstr(const char *where, const char *what) {
int a = 0;
int b;
do {
b = 0;
while (what[b] && what[b] == where[a + b]) {
++b;
}
if (!what[b]) {
return (char*)(where + a);
}
} while (where[a++ + b]);
return (char*)0;
}
void *memchr(const void *str, int c, long unsigned n) {
const char *s = str;
const char *e = str + n;
while (s < e) {
if (*s == c) {
return (void*)s;
}
++s;
}
return (void*)0;
}
void *memmove(void *dst, const void *src, long unsigned n) {
const char *f = src;
char *t = dst;
while (n--) {
*t++ = *f++;
}
return dst;
}
int main(int argc, char* argv[]) {
char buf[5];
int len;
int o = 0;
while (0 < (len = os_read(0, buf + o, sizeof(buf) - o))) {
len += o;
char *p = buf;
char *p2 = memchr(p, '\n', len);
while (p2) {
*p2 = '\0';
if (strstr(p, argv[1])) {
os_write(1, p, p2 - p);
os_write(1, "\n", 1);
}
p = p2 + 1;
p2 = memchr(p, '\n', len - (p - buf));
}
o = len - (p - buf);
memmove(buf, p, o);
}
os_exit(0);
}