about summary refs log tree commit diff
path: root/boot/catboot.c
blob: ae0fc6c11c474ed9e1ac777d921cca6ade861138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>

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;
}