-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFILE.C
More file actions
444 lines (389 loc) · 11.5 KB
/
Copy pathFILE.C
File metadata and controls
444 lines (389 loc) · 11.5 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// file.c
// various file-handling routines
// By Abe Pralle 10.25.95
#ifndef _file_c_
#define _file_c_ 1
#include "FILE.H"
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
void Pause(LONG n); //defined in gfx.c
extern char *shapedata;
union wordbytes{
WORD word;
BYTE byte[2];
};
char cryptkey[]={'M'-4,'i'-8,'n'-12,'d'-16,'y'-20,0};
// Debug flag for file operations - shared between functions
static int debug_file_open = 0;
void cleanExit(WORD retval);
// Case-insensitive file opening for DOS compatibility
FILE *fopen_case_insensitive(char *fname, const char *mode)
{
FILE *file;
char temp_fname[256];
char *path_part, *filename_part, *ext_part;
char path_backup[256];
int i, len;
if (debug_file_open) {
printf("DEBUG: fopen_case_insensitive trying to open '%s' in mode '%s'\n", fname, mode);
fflush(stdout);
}
// Ensure we don't overflow the buffer
len = strlen(fname);
if (len >= 255) {
if (debug_file_open) {
printf("DEBUG: Filename too long, aborting\n");
fflush(stdout);
}
return NULL;
}
// First try the filename as-is
if (debug_file_open) {
printf("DEBUG: Trying original filename: '%s'\n", fname);
fflush(stdout);
}
file = fopen(fname, mode);
if (file != NULL) {
if (debug_file_open) {
printf("DEBUG: Success with original filename\n");
fflush(stdout);
}
return file;
}
// Make a backup of the original path for manipulation
strcpy(path_backup, fname);
// Try all lowercase
strcpy(temp_fname, fname);
for (i = 0; temp_fname[i]; i++) {
if (temp_fname[i] >= 'A' && temp_fname[i] <= 'Z') {
temp_fname[i] = temp_fname[i] + 32; // Convert to lowercase
}
}
if (debug_file_open) {
printf("DEBUG: Trying all lowercase: '%s'\n", temp_fname);
fflush(stdout);
}
file = fopen(temp_fname, mode);
if (file != NULL) {
if (debug_file_open) {
printf("DEBUG: Success with all lowercase\n");
fflush(stdout);
}
strcpy(fname, temp_fname); // Update fname with the working version
return file;
}
// Try all uppercase
strcpy(temp_fname, fname);
for (i = 0; temp_fname[i]; i++) {
if (temp_fname[i] >= 'a' && temp_fname[i] <= 'z') {
temp_fname[i] = temp_fname[i] - 32; // Convert to uppercase
}
}
if (debug_file_open) {
printf("DEBUG: Trying all uppercase: '%s'\n", temp_fname);
fflush(stdout);
}
file = fopen(temp_fname, mode);
if (file != NULL) {
if (debug_file_open) {
printf("DEBUG: Success with all uppercase\n");
fflush(stdout);
}
strcpy(fname, temp_fname); // Update fname with the working version
return file;
}
// Find the last directory separator to split path and filename
path_part = strrchr(path_backup, '/');
if (!path_part) {
path_part = strrchr(path_backup, '\\');
}
if (path_part) {
// We have a path component
*path_part = '\0'; // Temporarily terminate the path
filename_part = path_part + 1;
// Try different case variations of just the filename part
// while keeping the path unchanged
// Try lowercase filename with original path
strcpy(temp_fname, path_backup);
strcat(temp_fname, "/");
len = strlen(temp_fname);
strcpy(&temp_fname[len], filename_part);
for (i = len; temp_fname[i]; i++) {
if (temp_fname[i] >= 'A' && temp_fname[i] <= 'Z') {
temp_fname[i] = temp_fname[i] + 32;
}
}
file = fopen(temp_fname, mode);
if (file != NULL) {
strcpy(fname, temp_fname);
return file;
}
// Try uppercase filename with original path
strcpy(temp_fname, path_backup);
strcat(temp_fname, "/");
len = strlen(temp_fname);
strcpy(&temp_fname[len], filename_part);
for (i = len; temp_fname[i]; i++) {
if (temp_fname[i] >= 'a' && temp_fname[i] <= 'z') {
temp_fname[i] = temp_fname[i] - 32;
}
}
file = fopen(temp_fname, mode);
if (file != NULL) {
strcpy(fname, temp_fname);
return file;
}
// Try title case (first letter uppercase, rest lowercase)
strcpy(temp_fname, path_backup);
strcat(temp_fname, "/");
len = strlen(temp_fname);
strcpy(&temp_fname[len], filename_part);
for (i = len; temp_fname[i]; i++) {
if (i == len && temp_fname[i] >= 'a' && temp_fname[i] <= 'z') {
temp_fname[i] = temp_fname[i] - 32; // First char uppercase
} else if (temp_fname[i] >= 'A' && temp_fname[i] <= 'Z') {
temp_fname[i] = temp_fname[i] + 32; // Rest lowercase
}
}
file = fopen(temp_fname, mode);
if (file != NULL) {
strcpy(fname, temp_fname);
return file;
}
// Find extension and try different cases for base name and extension
ext_part = strrchr(filename_part, '.');
if (ext_part) {
// Try lowercase base, uppercase extension
strcpy(temp_fname, path_backup);
strcat(temp_fname, "/");
len = strlen(temp_fname);
// Copy and convert base name to lowercase
for (i = 0; filename_part + i < ext_part; i++) {
if (filename_part[i] >= 'A' && filename_part[i] <= 'Z') {
temp_fname[len + i] = filename_part[i] + 32;
} else {
temp_fname[len + i] = filename_part[i];
}
}
temp_fname[len + i] = '\0';
// Append dot and convert extension to uppercase
strcat(temp_fname, ".");
len = strlen(temp_fname);
for (i = 1; ext_part[i]; i++) { // Skip the dot
if (ext_part[i] >= 'a' && ext_part[i] <= 'z') {
temp_fname[len + i - 1] = ext_part[i] - 32;
} else {
temp_fname[len + i - 1] = ext_part[i];
}
}
temp_fname[len + i - 1] = '\0';
file = fopen(temp_fname, mode);
if (file != NULL) {
strcpy(fname, temp_fname);
return file;
}
// Try uppercase base, lowercase extension
strcpy(temp_fname, path_backup);
strcat(temp_fname, "/");
len = strlen(temp_fname);
// Copy and convert base name to uppercase
for (i = 0; filename_part + i < ext_part; i++) {
if (filename_part[i] >= 'a' && filename_part[i] <= 'z') {
temp_fname[len + i] = filename_part[i] - 32;
} else {
temp_fname[len + i] = filename_part[i];
}
}
temp_fname[len + i] = '\0';
// Append dot and convert extension to lowercase
strcat(temp_fname, ".");
len = strlen(temp_fname);
for (i = 1; ext_part[i]; i++) { // Skip the dot
if (ext_part[i] >= 'A' && ext_part[i] <= 'Z') {
temp_fname[len + i - 1] = ext_part[i] + 32;
} else {
temp_fname[len + i - 1] = ext_part[i];
}
}
temp_fname[len + i - 1] = '\0';
file = fopen(temp_fname, mode);
if (file != NULL) {
strcpy(fname, temp_fname);
return file;
}
}
} else {
// No path component, just filename - try additional variations
// Try title case (first letter uppercase, rest lowercase)
strcpy(temp_fname, fname);
for (i = 0; temp_fname[i]; i++) {
if (i == 0 && temp_fname[i] >= 'a' && temp_fname[i] <= 'z') {
temp_fname[i] = temp_fname[i] - 32; // First char uppercase
} else if (temp_fname[i] >= 'A' && temp_fname[i] <= 'Z') {
temp_fname[i] = temp_fname[i] + 32; // Rest lowercase
}
}
file = fopen(temp_fname, mode);
if (file != NULL) {
strcpy(fname, temp_fname);
return file;
}
// Find extension and try different cases for base name and extension
ext_part = strrchr(fname, '.');
if (ext_part) {
// Try lowercase base, uppercase extension
strcpy(temp_fname, fname);
// Convert base name to lowercase
char *temp_ext = strrchr(temp_fname, '.');
int base_len = temp_ext ? (temp_ext - temp_fname) : strlen(temp_fname);
for (i = 0; i < base_len; i++) {
if (temp_fname[i] >= 'A' && temp_fname[i] <= 'Z') {
temp_fname[i] = temp_fname[i] + 32;
}
}
// Convert extension to uppercase
temp_ext = strrchr(temp_fname, '.');
if (temp_ext) {
for (i = 1; temp_ext[i]; i++) { // Skip the dot
if (temp_ext[i] >= 'a' && temp_ext[i] <= 'z') {
temp_ext[i] = temp_ext[i] - 32;
}
}
}
file = fopen(temp_fname, mode);
if (file != NULL) {
strcpy(fname, temp_fname);
return file;
}
// Try uppercase base, lowercase extension
strcpy(temp_fname, fname);
// Convert base name to uppercase
temp_ext = strrchr(temp_fname, '.');
base_len = temp_ext ? (temp_ext - temp_fname) : strlen(temp_fname);
for (i = 0; i < base_len; i++) {
if (temp_fname[i] >= 'a' && temp_fname[i] <= 'z') {
temp_fname[i] = temp_fname[i] - 32;
}
}
// Convert extension to lowercase
temp_ext = strrchr(temp_fname, '.');
if (temp_ext) {
for (i = 1; temp_ext[i]; i++) { // Skip the dot
if (temp_ext[i] >= 'A' && temp_ext[i] <= 'Z') {
temp_ext[i] = temp_ext[i] + 32;
}
}
}
file = fopen(temp_fname, mode);
if (file != NULL) {
strcpy(fname, temp_fname);
return file;
}
}
}
// If none of the variants work, return NULL
if (debug_file_open) {
printf("DEBUG: All filename variations failed for '%s'\n", fname);
fflush(stdout);
}
return NULL;
}
void set_file_debug(int enable)
{
debug_file_open = enable;
}
LONG Encrypt(char *data,char *key)
{
LONG i=0,keypos=0;
while(data[i]!=0){
data[i++]^=key[keypos++];
if(key[keypos]==0) keypos=0;
}
return i;
}
void Decrypt(char *data,char *key,LONG slen)
{
LONG i=0,keypos=0;
while(slen>0){
data[i++]^=key[keypos++];
if(key[keypos]==0) keypos=0;
slen--;
}
}
void Error(const char *mesg)
{
fprintf(stderr,"%s",mesg);
Pause(240);
cleanExit(0);
exit(0);
}
FILE *ReadFile(char *fname)
{
FILE *file;
char mesg[120];
file=fopen_case_insensitive(fname,"rb");
if(file==0){
sprintf(mesg,"Could not open \"%s\" for reading",fname);
Error(mesg);
}
return file;
}
ULONG FileSize(FILE *fp)
{
long pos, endpos;
pos = ftell(fp);
fseek(fp, 0, SEEK_END);
endpos = ftell(fp);
fseek(fp, pos, SEEK_SET);
return ((ULONG) endpos);
}
WORD Exists(char *fname)
{
FILE *file;
file=fopen_case_insensitive(fname,"r");
if(file==0){
return 0;
}else{
fclose(file);
return 1;
}
}
void CloseFile(FILE *fp)
{
fclose(fp);
}
void ReadMem(FILE *fp, char *addr, ULONG size)
{
static char buffer[4096];
while(size>=4096){
fread(buffer,4096,1,fp);
_fmemcpy(addr,buffer,4096);
size-=4096;
addr+=4096;
}
fread(buffer,size,1,fp);
_fmemcpy(addr,buffer,size);
addr+=size;
}
UBYTE ReadUByte(FILE *fp)
{
UBYTE data;
fread(&data,1,1,fp);
return data;
}
WORD ReadWord(FILE *fp)
{
union wordbytes data;
fscanf(fp,"%c%c",&data.byte[1],&data.byte[0]);
return data.word;
}
void ReadString(FILE *fp,char *str)
{
WORD size;
size=ReadWord(fp)^22;
ReadMem(fp,str,size);
Decrypt(str,cryptkey,size);
str[size]=0;
}
#endif