about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--boot/catboot.c44
1 files changed, 42 insertions, 2 deletions
diff --git a/boot/catboot.c b/boot/catboot.c
index a021a2b..ae0fc6c 100644
--- a/boot/catboot.c
+++ b/boot/catboot.c
@@ -1,5 +1,45 @@
+#include <fcntl.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
 
-int main() {
-    printf("hi meow!!");
+void
+error(const char* msg) {
+    fprintf(stderr, ":( error: %s\n", msg);
+    exit(EXIT_FAILURE);
+}
+
+const char*
+read_file(const char* path) {
+    struct stat stat_info;
+    if (stat(path, &stat_info) == -1)
+        error("i couldn't open that file, sorry :(");
+
+    const int file = open(path, O_RDONLY);
+    if (file == -1)
+        error("i couldn't open that file, sorry :(");
+
+    const int mmap_prot = PROT_READ;
+    const int mmap_flags = MAP_PRIVATE;
+    const void* file_data = mmap(
+        NULL, stat_info.st_size, mmap_prot, mmap_flags, file, 0
+    );
+
+    return (const char*)file_data;
+}
+
+int
+main(const int argc, const char* argv[]) {
+    if (argc != 2) {
+        printf("usage: catboot <filename>\n");
+        return EXIT_FAILURE;
+    }
+    const char* file_path = argv[1];
+    const char* source = read_file(file_path);
+
+    printf("%s", source);
+
+    return EXIT_SUCCESS;
 }