Starting file reading
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 1 May 2020 19:07:41 +0000 (14:07 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 1 May 2020 19:07:41 +0000 (14:07 -0500)
Makefile
onyx
onyx.c

index 86761a43b16d16ad968a2d68e24724259e27607d..1df1e52ecaf423f657bc68b0374dea8b131f2a9b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ OBJ_FILES=\
 CC=gcc
 INCLUDES=
 LIBS=
-FLGAS=-g
+FLAGS=-g
 
 %.o: %.c
        $(CC) $(FLAGS) -c $< -o $@ $(INCLUDES)
diff --git a/onyx b/onyx
index e33fe9cd51db82c640b9b58720afeae0d569172d..e8adb76455dd27e120c61eb3bf1f51764899959d 100755 (executable)
Binary files a/onyx and b/onyx differ
diff --git a/onyx.c b/onyx.c
index b028e3b15072cbd886ec46c0782be5935c5850c8..59a7d7038596f1910171d75f3e65e7f7b4f7235d 100644 (file)
--- a/onyx.c
+++ b/onyx.c
@@ -1,9 +1,60 @@
-
 #include <stdio.h> // TODO: Replace with custom lib
 #include <stdlib.h> // TODO: Replace with custom lib
 
-int main() {
-       puts("Hello World!");
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+typedef struct File {
+       int fd;
+       int open : 1;
+       const char* path;
+} File;
+
+int file_open(File *f, const char* path, int flags) {
+       f->fd = open(path, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
+
+       if (f->fd < 0)
+               return 1;
+
+       f->open = 1;
+       f->path = path;
+       return 0;
+}
+
+int file_close(File* f) {
+       if (close(f->fd) < 0) {
+               return errno;
+       }
+
+       f->open = 0;
+       return 0;
+}
+
+unsigned char file_read_byte(File* f) {
+       if (!f->open) return 0;
+
+       unsigned char byte;
+       read(f->fd, &byte, 1);
+       return byte;
+}
+
+int main(int argc, char *argv[]) {
+
+       if (argc < 2) {
+               fprintf(stderr, "Expected file to compile\n");
+               return -1;
+       }
+
+       File file;
+
+       if (!file_open(&file, argv[1], O_RDONLY)) {
+               fprintf(stderr, "Failed to open file: %s\n", argv[1]);
+               return -1;
+       }
+
+       file_close(&file);
 
        return 0;
 }