Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practicum1 / fgrep.c
diff options
context:
space:
mode:
1 files changed, 38 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practicum1/fgrep.c38
diff --git a/Year 3/System programming/Practicum1/fgrep.c b/Year 3/System programming/Practicum1/fgrep.c
new file mode 100644
index 0000000..7603e73
--- /dev/null
+++ b/Year 3/System programming/Practicum1/fgrep.c
@@ -0,0 +1,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;
+ }
+ }
+
+ }
+}
+