From: Brendan Hansen Date: Fri, 1 May 2020 19:07:41 +0000 (-0500) Subject: Starting file reading X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=fd83e9dcea12ce5a98d909694c4609c88a760b8c;p=onyx.git Starting file reading --- diff --git a/Makefile b/Makefile index 86761a43..1df1e52e 100644 --- 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 e33fe9cd..e8adb764 100755 Binary files a/onyx and b/onyx differ diff --git a/onyx.c b/onyx.c index b028e3b1..59a7d703 100644 --- a/onyx.c +++ b/onyx.c @@ -1,9 +1,60 @@ - #include // TODO: Replace with custom lib #include // TODO: Replace with custom lib -int main() { - puts("Hello World!"); +#include +#include +#include +#include + +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; }