aboutsummaryrefslogtreecommitdiff
path: root/tools/container.c
blob: 254eee3f0ce8459c7b19aded4079378093d79df6 (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const char *filename(const char *const path)
{
    const char *ret = &path[strlen(path)];

    while (ret != path && *(ret - 1) != '\\' && *(ret - 1) != '/')
        ret--;

    return ret;
}

static int dump_file(const char *const path, const char *const outpath,
    FILE *const out)
{
    int ret = -1;
    FILE *const f = fopen(path, "rb");

    if (!f)
    {
        fprintf(stderr, "%s: could not open %s: %s\n",
            __func__, path, strerror(errno));
        goto end;
    }

    const char *const bname = filename(path);

    if (fprintf(out, "%s", bname) < 0)
    {
        fprintf(stderr, "fprintf(f, %s): %s\n", path, strerror(errno));
        goto end;
    }
    else if (fputc('\0', out) == EOF)
    {
        perror("could not write null character");
        goto end;
    }
    else if (fseek(f, 0, SEEK_END))
    {
        perror("fseek(SEEK_END)");
        goto end;
    }

    /* This won't work on files larger than 2 GiB if sizeof sz == 4,
     * but this is acceptable for a PS1 video game. */
    const long sz = ftell(f);

    if (fprintf(out, "%ld", sz) < 0)
    {
        perror("could not write file size");
        goto end;
    }
    else if (fputc('\0', out) == EOF)
    {
        perror("could not write null character");
        goto end;
    }
    else if (fseek(f, 0, SEEK_SET))
    {
        fprintf(stderr, "%s: fseek(SEEK_SET): %s\n", __func__, strerror(errno));
        goto end;
    }

    while (!feof(f))
    {
        char c;

        if (!fread(&c, sizeof c, 1, f))
        {
            if (ferror(f))
            {
                fprintf(stderr, "%s: ferror\n", path);
                goto end;
            }
        }
        else if (fputc(c, out) == EOF)
        {
            fprintf(stderr, "could not dump %s to %s", path, outpath);
            goto end;
        }
    }

    ret = 0;

end:
    if (f)
        fclose(f);

    return ret;
}

int main(const int argc, const char *const *const argv)
{
    int ret = EXIT_FAILURE;
    size_t n_in = 0;
    FILE *out = NULL;

    if (argc < 3)
    {
        fprintf(stderr, "%s <file1> [<file2> ...] <output>\n", *argv);
        goto end;
    }

    const char *const outpath = argv[argc - 1];

    n_in = argc - 2;

    if (!(out = fopen(outpath, "wb")))
    {
        fprintf(stderr, "could not open %s: %s\n", outpath, strerror(errno));
        goto end;
    }

    for (size_t i = 0; i < n_in; i++)
        if (dump_file(argv[i + 1], outpath, out))
            goto end;

    ret = EXIT_SUCCESS;

end:
    if (out)
        fclose(out);

    return ret;
}