about summary refs log tree commit diff
path: root/main.c
blob: b2d96caf100f74294c4cd726f761f268929f49c0 (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
56
57
#include "cccl.h"

#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>

#include <unistd.h>


int verbose = 0;

int main(int argc, char **argv)
{
    const char *name = *argv;

    int ch;
    while ((ch = getopt(argc, argv, "v")) >= 0)
    {
        switch (ch)
        {
            case 'v':
            {
                verbose = 1;
            } break;
            default:
            {
                fprintf(stderr, "usage: %s [-v] file\n", name);
                exit(1);
            } break;
        }
    }
    argc -= optind;
    argv += optind;

    if (!*argv)
    {
        fprintf(stderr, "usage: %s [-v] file\n", name);
        exit(1);
    }

    struct cccl_File file;

    int error = cccl_allocfile(*argv, &file);
    if (error)
        err(1, "cccl_readfile()");

    FILE *f = fopen(*argv, "r");
    if (!f)
        err(1, "fopen()");
    int bytes_read = fread(file.buffer, 1, file.size, f);
    if (ferror(f) || bytes_read != file.size)
        errx(1, "couldn't read %s", *argv);
    fclose(f);

    cccl(file);
}