Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practicum1 / fgrep.c
blob: 7603e73e0c457cb332261d4cc2e13f47bf963ac0 (plain) (blame)
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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
	int f1 = open(argv[1], O_RDONLY);
	if (f1 == -1) return 1;
	int f2 = open(argv[2], O_RDONLY);
	if (f2 == -1) return 2;
	
	char buf;
	int fromStart = 0, backtrackTo = 0;
	while(read(f2, &buf, 1)) {
		if (buf == '\n') {
			backtrackTo = 0;
			continue;
		}
		backtrackTo--;
		fromStart++;

		int anyMatched = 0;
		char pbuf, mbuf = buf;

		while(read(f1, &pbuf, 1)) {
			if (pbuf != mbuf) {
				while (pbuf != '\n') read(f1, &pbuf, 1);
				lseek(f2, 
			}
			else if (pbuf == '\n') {
				anyMatched = 1;
			}
		}

	}
}