about summary refs log tree commit diff
path: root/boot/catboot.c
blob: b36bb97645dfe7475d95dbc4c01fb858a1a63fbb (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
46
47
48
49
50
51
52
53
54
55
/*
 * # catboot
 *
 * the bootstrap compiler for catskill,
 * implemented as simply as possible,
 * depending only on the C standard library,
 * in this case musl, and built statically
 * in a single union build.
 *
 * should only be used to compile `catskill` itself,
 * as it is not a full-featured compiler, and never will be.
 * once `catskill` can compile itself using a C backend,
 * this compiler version will be permanently retired.
 * (although that's still very far away!! have fun! :3)
 */

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include "common.c"

const ascii*
read_file(const ascii* path)
{
    struct stat stat_info;
    if (stat(path, &stat_info) == -1) failure("i couldn't open that file, sorry :(");

    const int32 file_descriptor = open(path, O_RDONLY);
    check(file_descriptor != -1, "i couldn't open that file, sorry :(");

    const flags mmap_prot = PROT_READ;
    const flags mmap_flags = MAP_PRIVATE;
    const unknown* file_data =
        mmap(nil, stat_info.st_size, mmap_prot, mmap_flags, file_descriptor, 0);

    return file_data;
}

integer
main(const integer argc, const ascii* argv[])
{
    if (argc != 2) {
        printf("usage: catboot <filename>\n");
        return EXIT_FAILURE;
    }
    const ascii* file_path = argv[1];
    const ascii* source = read_file(file_path);

    printf("%s", source);

    return EXIT_SUCCESS;
}