author | rubidium |
Wed, 17 Dec 2008 23:12:35 +0000 | |
changeset 10435 | 849b45cca2e4 |
parent 10385 | 4f9838649c7f |
permissions | -rw-r--r-- |
10385
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
1 |
/* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
2 |
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
3 |
/* Updated 2008 to use fread/fopen and friends instead of read/open so it compiles with GCC on Unix (Rubidium) */ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
4 |
#include <stdio.h> |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
5 |
#include <stdlib.h> |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
6 |
#include <fcntl.h> |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
7 |
#include <sys/stat.h> |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
8 |
#include <string.h> |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
9 |
#include <unistd.h> |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
10 |
#include <ctype.h> |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
11 |
|
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
12 |
|
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
13 |
static void |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
14 |
exe2aout(char *fname) |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
15 |
{ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
16 |
unsigned short header[3]; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
17 |
FILE *ifile; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
18 |
FILE *ofile; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
19 |
char buf[4096]; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
20 |
int rbytes; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
21 |
char *dot = strrchr(fname, '.'); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
22 |
if (!dot || strlen(dot) != 4 |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
23 |
|| tolower(dot[1]) != 'e' |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
24 |
|| tolower(dot[2]) != 'x' |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
25 |
|| tolower(dot[3]) != 'e') |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
26 |
{ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
27 |
fprintf(stderr, "%s: Arguments MUST end with a .exe extension\n", fname); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
28 |
return; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
29 |
} |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
30 |
|
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
31 |
ifile = fopen(fname, "rb"); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
32 |
if (!ifile) |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
33 |
{ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
34 |
perror(fname); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
35 |
return; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
36 |
} |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
37 |
fread(header, sizeof(header), 1, ifile); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
38 |
if (header[0] == 0x5a4d) |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
39 |
{ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
40 |
long header_offset = (long)header[2]*512L; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
41 |
if (header[1]) |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
42 |
header_offset += (long)header[1] - 512L; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
43 |
fseek(ifile, header_offset, SEEK_SET); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
44 |
header[0] = 0; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
45 |
fread(header, sizeof(header), 1, ifile); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
46 |
if ((header[0] != 0x010b) && (header[0] != 0x014c)) |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
47 |
{ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
48 |
fprintf(stderr, "`%s' does not have a COFF/AOUT program appended to it\n", fname); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
49 |
return; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
50 |
} |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
51 |
fseek(ifile, header_offset, SEEK_SET); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
52 |
} |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
53 |
else |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
54 |
{ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
55 |
fprintf(stderr, "`%s' is not an .EXE file\n", fname); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
56 |
return; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
57 |
} |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
58 |
|
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
59 |
*dot = 0; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
60 |
ofile = fopen(fname, "w+b"); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
61 |
if (!ofile) |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
62 |
{ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
63 |
perror(fname); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
64 |
return; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
65 |
} |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
66 |
|
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
67 |
while ((rbytes=fread(buf, 1, 4096, ifile)) > 0) |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
68 |
{ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
69 |
int wb = fwrite(buf, 1, rbytes, ofile); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
70 |
if (wb < 0) |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
71 |
{ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
72 |
perror(fname); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
73 |
break; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
74 |
} |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
75 |
if (wb < rbytes) |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
76 |
{ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
77 |
fprintf(stderr, "`%s': disk full\n", fname); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
78 |
exit(1); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
79 |
} |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
80 |
} |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
81 |
fclose(ifile); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
82 |
fclose(ofile); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
83 |
} |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
84 |
|
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
85 |
int |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
86 |
main(int argc, char **argv) |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
87 |
{ |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
88 |
int i; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
89 |
if (argc == 1) printf("Usage: %s <exename>", argv[0]); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
90 |
for (i=1; i<argc; i++) |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
91 |
exe2aout(argv[i]); |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
92 |
return 0; |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
93 |
} |
4f9838649c7f
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents:
diff
changeset
|
94 |