forked from TheGLander/tworld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mklynxcc.c
104 lines (94 loc) · 2.19 KB
/
mklynxcc.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
/* mklynxcc.c: Written by Brian Raiter, 2001.
*
* This is a quick hack that can be used to make .dat files that use
* the Lynx ruleset. Run the program, giving it the name of a .dat
* file, and the program will modify the .dat file to mark it as being
* for the Lynx ruleset.
*
* This source code is in the public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* The magic numbers of the .dat file.
*/
#define CHIPS_SIGBYTE_1 0xAC
#define CHIPS_SIGBYTE_2 0xAA
#define CHIPS_SIGBYTE_3 0x02
/* Error return values.
*/
#define ERR_BAD_CMDLINE 127
#define ERR_NOT_DATFILE 126
#define ERR_NOT_MS_DATFILE 125
/* The file to alter.
*/
static FILE *fp = NULL;
static char const *file = NULL;
/* Replace a byte in the file.
*/
static int changebyte(unsigned long pos, unsigned char byte)
{
if (fseek(fp, pos, SEEK_SET) == 0 && fputc(byte, fp) != EOF)
return TRUE;
perror(file);
return FALSE;
}
/* Return FALSE if the file is not a .dat file.
*/
static int sanitycheck(void)
{
unsigned char header[4];
int n;
n = fread(header, 1, 4, fp);
if (n < 0) {
perror(file);
return FALSE;
}
if (n < 4 || header[0] != CHIPS_SIGBYTE_1
|| header[1] != CHIPS_SIGBYTE_2
|| header[2] != CHIPS_SIGBYTE_3) {
fprintf(stderr, "%s is not a valid .dat file\n", file);
errno = ERR_NOT_DATFILE;
return FALSE;
}
if (header[3] != 0) {
if (header[3] == 1)
fprintf(stderr, "%s is already set for the Lynx ruleset\n", file);
else
fprintf(stderr, "%s is not set for the MS ruleset\n", file);
errno = ERR_NOT_MS_DATFILE;
return FALSE;
}
rewind(fp);
return TRUE;
}
int main(int argc, char *argv[])
{
int ret = 0;
int n;
if (argc < 2) {
fprintf(stderr, "Usage: %s DATFILE ...\n", argv[0]);
return ERR_BAD_CMDLINE;
}
for (n = 1 ; n < argc ; ++n) {
file = argv[n];
if ((fp = fopen(file, "r+b")) == NULL) {
perror(file);
ret = errno;
continue;
}
if (sanitycheck() && changebyte(3, 1))
printf("%s was successfully altered\n", file);
else
ret = errno;
fclose(fp);
}
return ret;
}