-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathece391grep.c
97 lines (88 loc) · 2.34 KB
/
ece391grep.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
#include <stdint.h>
#include "ece391support.h"
#include "ece391syscall.h"
#define BUFSIZE 1024
#define SBUFSIZE 33
int32_t
do_one_file (const char* s, const char* fname)
{
int32_t fd, cnt, last, line_start, line_end, check, s_len;
uint8_t data[BUFSIZE+1];
s_len = ece391_strlen ((uint8_t*)s);
if (-1 == (fd = ece391_open ((uint8_t*)fname))) {
ece391_fdputs (1, (uint8_t*)"file open failed\n");
return -1;
}
last = 0;
while (1) {
cnt = ece391_read (fd, data + last, BUFSIZE - last);
if (-1 == cnt) {
ece391_fdputs (1, (uint8_t*)"file read failed\n");
return -1;
}
last += cnt;
line_start = 0;
while (1) {
line_end = line_start;
while (line_end < last && '\n' != data[line_end])
line_end++;
if ('\n' != data[line_end] && 0 != cnt && line_start != 0) {
/* copy from line_start to last down to 0 and fix last */
data[line_end] = '\0';
ece391_strcpy (data, data + line_start);
last -= line_start;
break;
}
/* search the line */
data[line_end] = '\0';
for (check = line_start; check < line_end; check++) {
if (s[0] == data[check] &&
0 == ece391_strncmp ((uint8_t*)(data + check), (uint8_t*)s, s_len)) {
ece391_fdputs (1, (uint8_t*)fname);
ece391_fdputs (1, (uint8_t*)":");
ece391_fdputs (1, data + line_start);
ece391_fdputs (1, (uint8_t*)"\n");
break;
}
}
line_start = line_end + 1;
if (line_start >= last) {
last = 0;
break;
}
}
if (0 == cnt)
break;
}
if (-1 == ece391_close (fd)) {
ece391_fdputs (1, (uint8_t*)"file close failed\n");
return -1;
}
return 0;
}
int main ()
{
int32_t fd, cnt;
uint8_t buf[SBUFSIZE];
uint8_t search[BUFSIZE];
if (0 != ece391_getargs (search, BUFSIZE)) {
ece391_fdputs (1, (uint8_t*)"could not read argument\n");
return 3;
}
if (-1 == (fd = ece391_open ((uint8_t*)"."))) {
ece391_fdputs (1, (uint8_t*)"directory open failed\n");
return 2;
}
while (0 != (cnt = ece391_read (fd, buf, SBUFSIZE-1))) {
if (-1 == cnt) {
ece391_fdputs (1, (uint8_t*)"directory entry read failed\n");
return 3;
}
if ('.' == buf[0]) /* a directory... */
continue;
buf[cnt] = '\0';
if (0 != do_one_file ((char*)search, (char*)buf))
return 3;
}
return 0;
}