-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.c
200 lines (169 loc) · 3.9 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef __WATCOMC__
#include <io.h>
#endif
#include "adpatch.h"
#define STR(x) #x
#define XSTR(x) STR(x)
#ifdef __WATCOMC__
#define DEFAULT_PORT "LPT1"
#else
#define DEFAULT_PORT "0x378"
#endif
static int patch_port;
static void in_place_mode(const char *orig_filename) {
static const char bak_ext[] = ".bak";
FILE *in = 0, *out = 0;
char *bak_filename;
int i, ext;
if (access(orig_filename, R_OK) != 0) {
fprintf(stderr, "Error: Can't read file: %s\n", orig_filename);
exit(1);
}
bak_filename = malloc(strlen(orig_filename) + strlen(bak_ext) + 1);
if (!bak_filename) { exit(1); }
strcpy(bak_filename, orig_filename);
for (ext = i = strlen(bak_filename); i >= 0; i--) {
if (bak_filename[i] == '/' || bak_filename[i] == '\\') {
break;
} else if (bak_filename[i] == '.') {
ext = i;
}
}
strcpy(bak_filename + ext, bak_ext);
printf("ADPATCH " XSTR(VERSION) "\n");
printf("Patching %s\n", orig_filename);
printf("Writing backup copy to %s\n", bak_filename);
if (access(bak_filename, F_OK) == 0) {
fprintf(stderr, "Error: Backup file already exists, not overwriting.\n");
exit(1);
}
if (rename(orig_filename, bak_filename) != 0) {
perror("Error: Rename failed");
exit(1);
}
in = fopen(bak_filename, "rb");
if (!in) {
perror("Error: Can't open input file for reading");
goto rollback;
}
setvbuf(in, 0, _IONBF, 0);
out = fopen(orig_filename, "wb");
if (!out) {
perror("Error: Can't open output file for writing");
goto rollback;
}
setvbuf(out, 0, _IONBF, 0);
i = apply_patches(in, out, patch_port);
if (i <= 0) {
goto rollback;
}
fclose(out);
fclose(in);
free(bak_filename);
return;
rollback:
if (out) {
fclose(out);
}
if (in) {
fclose(in);
}
remove(orig_filename);
rename(bak_filename, orig_filename);
free(bak_filename);
exit(1);
}
static void copy_mode(const char *src_filename, const char *dst_filename) {
FILE *in, *out;
int r;
in = fopen(src_filename, "rb");
if (!in) {
perror("Error: Can't open input file for reading");
exit(1);
}
setvbuf(in, 0, _IONBF, 0);
out = fopen(dst_filename, "wb");
if (!out) {
perror("Error: Can't open output file for writing");
exit(1);
}
setvbuf(out, 0, _IONBF, 0);
r = apply_patches(in, out, patch_port);
fclose(out);
fclose(in);
if (r < 0) {
unlink(dst_filename);
exit(1);
}
}
static void usage() {
fprintf(stderr,
"ADPATCH " XSTR(VERSION) "\n"
"\n"
"Usage: adpatch [OPTIONS...] FILE\n"
" adpatch [OPTIONS...] -o OUTPUT-FILE INPUT-FILE\n"
"\n"
"Options:\n"
" -p PORT Set OPL2LPT port (default: " DEFAULT_PORT ")\n"
);
exit(1);
}
static short get_lpt_port(int i) {
#ifdef __WATCOMC__
return *(short __far *)(0x40 :> (6 + 2*i));
#else
return 0;
#endif
}
static short parse_port(const char *arg) {
int i, port;
if (strncasecmp(arg, "lpt", 3) == 0 && (i = arg[3] - '0') >= 1 && i <= 3) {
port = get_lpt_port(i);
} else {
port = strtol(arg, 0, 16);
}
if (port == 0) {
fprintf(stderr, "Error: invalid port %s\n", arg);
exit(1);
}
return port;
}
int main(int argc, char *argv[]) {
char *src = NULL;
char *dest = NULL;
int opt;
while ((opt = getopt(argc, argv, "ip:o:")) != -1) {
switch (opt) {
case 'i':
/* Ignored for backward compatibility*/
break;
case 'p':
patch_port = parse_port(optarg);
break;
case 'o':
dest = optarg;
break;
default:
usage();
}
}
if (!patch_port) {
patch_port = parse_port(DEFAULT_PORT);
}
if (optind + 1 != argc) {
usage();
}
src = argv[optind];
if (!dest) {
in_place_mode(src);
} else {
copy_mode(src, dest);
}
return 0;
}