From 8c3e736012394ce7327606889e8000d7503cf1e6 Mon Sep 17 00:00:00 2001 From: Mel Date: Fri, 28 Feb 2025 22:05:23 +0100 Subject: Map and read source file Signed-off-by: Mel --- boot/catboot.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'boot/catboot.c') 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 #include +#include +#include +#include +#include -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 \n"); + return EXIT_FAILURE; + } + const char* file_path = argv[1]; + const char* source = read_file(file_path); + + printf("%s", source); + + return EXIT_SUCCESS; } -- cgit 1.4.1