-
Notifications
You must be signed in to change notification settings - Fork 36
/
merge16bit.c
112 lines (104 loc) · 2.92 KB
/
merge16bit.c
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
/*
* merge16bit.c: Merge a 16-bit ROM binary from two 8-bit images.
*
* The command-line options `-h` and `-l` specify the high and low
* input files, respectively. The inputs should be raw 8-bit binary
* ROM image halves (raw data, split into two files). The option
* `-o` specifies the output file, which will be a 16-bit ROM image.
* Output is to `stdout` by default.
*
* Copyright (c) 2019 Kimmo Kulovesi, https://arkku.com
* Provided with absolutely no warranty, use at your own risk only.
* Distribute freely, mark modified copies as such.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int
main (int argc, char *argv[]) {
FILE *outfile = stdout;
FILE *inhigh = NULL;
FILE *inlow = NULL;
char *arg = NULL;
while (--argc) {
arg = *(++argv);
if (arg[0] == '-' && arg[1] && arg[2] == '\0') {
switch (arg[1]) {
case 'o':
if (--argc == 0) {
goto invalid_argument;
}
++argv;
if (!(outfile = fopen(*argv, "wb"))) {
goto argument_error;
}
break;
case 'h':
if (--argc == 0) {
goto invalid_argument;
}
++argv;
if (!(inhigh = fopen(*argv, "rb"))) {
goto argument_error;
}
break;
case 'l':
if (--argc == 0) {
goto invalid_argument;
}
++argv;
if (!(inlow = fopen(*argv, "rb"))) {
goto argument_error;
}
break;
case '?':
arg = NULL;
goto usage;
default:
goto invalid_argument;
}
continue;
}
invalid_argument:
(void) fprintf(stderr, "Invalid argument: %s\n", arg);
usage:
(void) fprintf(stderr, "merge16bit - Copyright (c) 2019 Kimmo Kulovesi\n");
(void) fprintf(stderr, "Usage: merge16bit [-o <out.bin>] <-h highfile> <-l lowfile>\n");
return arg ? EXIT_FAILURE : EXIT_SUCCESS;
argument_error:
perror(*argv);
return EXIT_FAILURE;
}
if (!(inhigh && inlow)) {
arg = "";
goto usage;
}
errno = 0;
for (;;) {
int byte = fgetc(inlow);
if (byte == EOF) {
break;
}
if (fputc(byte, outfile) == EOF) {
break;
}
byte = fgetc(inhigh);
if (byte == EOF) {
break;
}
if (fputc(byte, outfile) == EOF) {
break;
}
}
if (errno) {
perror("Error");
}
(void) fclose(inlow);
(void) fclose(inhigh);
if (outfile != stdout) {
(void) fclose(outfile);
}
return errno ? EXIT_FAILURE : EXIT_SUCCESS;
}