aboutsummaryrefslogtreecommitdiff
path: root/backendC/CleanCompilerSources/windows_io.c
blob: 986d95e811a1bca8cd34860271bd9f9871bc1ee3 (plain) (blame)
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
#ifdef __MWERKS__
#	define _WINDOWS_
#endif

#include "compiledefines.h"
#include "system.h"
#include <stdio.h>

#ifdef __MWERKS__
#	include <x86_prefix.h>
#else
#	define _X86_
#endif
#include <windef.h>
#include <winbase.h>

char *GetFileExtension (FileKind kind)
{
	switch (kind){
		case abcFile:
			return ".abc";
		case obj00File:
		case obj20File:
		case obj81File:
			return ".obj";
		case iclFile:
			return ".icl";
		case dclFile:
			return ".dcl";
		case dumpFile:
			return ".dmp";
		case statFile:
			return ".stt";
		case stasFile:
			return ".str";
		case assFile:
			return ".a";
		case sunAssFile:
			return ".s";
		case helpFile:
		case applFile:
		case otherFile:
		default:
			return "";
	}	
}

char clean_lib_directory[129] = ".";

#if WRITE_DCL_MODIFICATION_TIME
static int file_exists_with_time (char *file_name,FileTime *file_time_p)
{
	HANDLE h;
	WIN32_FIND_DATA find_data;
	
	h=FindFirstFile (file_name,&find_data);

	if (h!=INVALID_HANDLE_VALUE){
		FindClose (h);
		
		*file_time_p=find_data.ftLastWriteTime;
		return True;
	} else
		return False;
}
#endif

static int file_exists (char *file_name)
{
	HANDLE h;
	WIN32_FIND_DATA find_data;
	
	h=FindFirstFile (file_name,&find_data);

	if (h!=INVALID_HANDLE_VALUE){
		FindClose (h);
		return True;
	} else
		return False;
}

static int use_clean_system_files_folder=1;

extern char *path_parameter;

#if WRITE_DCL_MODIFICATION_TIME
static Bool find_filepath_and_time (char *fname,FileKind kind,char *path,FileTime *file_time_p)
{
    char *s,*path_elem,c,*pathlist,*ext;

	if (path_parameter==NULL)
	    pathlist=getenv ("CLEANPATH");
	else
		pathlist=path_parameter;

    if (pathlist==NULL)
		pathlist=".";

	ext = GetFileExtension (kind);

	if (! (fname[0]=='\\' || (fname[0]!=0 && fname[1]==':'))){
		path_elem = pathlist;

		s=path_elem;		
		for (;;){
			c = *s;
			if (c == ';' || c == '\0'){
				char *from_p,*dest_p;
			
				from_p=path_elem;
				dest_p=path;
				while (from_p<s)
					*dest_p++ = *from_p++;
				*dest_p = '\0';

			    strcat (path,"\\");
			    strcat (path,fname);
			    strcat (path,ext);
				if (file_exists_with_time (path,file_time_p))
					return True;
		    
			    if (c == '\0')
			    	break;
	
				path_elem = ++s;
			} else
			    ++s;
		}
	}

	strcpy (path,fname);
	strcat (path,ext);

 	return file_exists_with_time (path,file_time_p);
}
#endif

static Bool findfilepath (char *fname,FileKind kind,char *path)
{
    char *s,*path_elem,c,*pathlist,*ext;
	int in_clean_system_files_folder;

	if (path_parameter==NULL)
	    pathlist=getenv ("CLEANPATH");
	else
		pathlist=path_parameter;

    if (pathlist==NULL)
		pathlist=".";

	ext = GetFileExtension (kind);

	in_clean_system_files_folder=0;

	if (use_clean_system_files_folder)
		switch (kind){
			case abcFile:
			case obj00File:
			case obj20File:
			case obj81File:
				in_clean_system_files_folder=1;
		}


	if (! (fname[0]=='\\' || (fname[0]!=0 && fname[1]==':'))){
		path_elem = pathlist;

		s=path_elem;		
		for (;;){
			c = *s;
			if (c == ';' || c == '\0'){
				char *from_p,*dest_p;
			
				from_p=path_elem;
				dest_p=path;
				while (from_p<s)
					*dest_p++ = *from_p++;
				*dest_p = '\0';

				if (in_clean_system_files_folder)
					strcat (path,"\\Clean System Files\\");
				else
				    strcat (path,"\\");
			    strcat (path,fname);
			    strcat (path,ext);
				if (file_exists (path))
					return True;
		    
			    if (c == '\0')
			    	break;
	
				path_elem = ++s;
			} else
			    ++s;
		}
	}

	if (in_clean_system_files_folder){
		strcpy (path,"Clean System Files\\");
		strcat (path,fname);
	} else
		strcpy (path,fname);
	strcat (path,ext);

 	return file_exists (path);
}

/*
#include <share.h>

		file=(File) _fsopen (path,mode,_SH_DENYNO);
*/

#if WRITE_DCL_MODIFICATION_TIME
File FOpenWithFileTime (char *file_name,FileKind kind, char *mode,FileTime *file_time_p)
{
	char path[MAXPATHLEN];
	Bool res;

	res=find_filepath_and_time (file_name, kind, path,file_time_p);

	if (res || mode[0] != 'r')
		return fopen (path, mode);
	else
		return NULL;
}
#endif

File FOpen (char *fname,FileKind kind,char *mode)
{
	char path[MAXPATHLEN];
	Bool res;

	if (fname[0]=='\\' || (fname[0]!=0 && fname[1]==':')){
		strcpy (path,fname);
		strcat (path,GetFileExtension (kind));
		return fopen (path,mode);
	}

	if (mode[0]=='r'){
		findfilepath (fname,kind,path);
		return fopen (path,mode);
	} else {
		res=findfilepath (fname,dclFile,path);
		if (!res)
			res=findfilepath (fname,iclFile,path);

		if (res){
			char *p,*after_last_slash;

			after_last_slash=NULL;

			p=path;
			while (*p)
				if (*p++=='\\')
					after_last_slash=p;

			if (after_last_slash==NULL)
				after_last_slash=path;

			if (use_clean_system_files_folder){
				strcpy (after_last_slash,"Clean System Files");

				if (!file_exists (path)){
					SECURITY_ATTRIBUTES sa;

					sa.nLength = sizeof(SECURITY_ATTRIBUTES);
					sa.bInheritHandle = TRUE;
					sa.lpSecurityDescriptor = NULL;

					CreateDirectory (path,&sa);
				}

				strcat (after_last_slash,"\\");
				strcat (after_last_slash,fname);
			} else
				strcpy (after_last_slash,fname);
			strcat (after_last_slash,GetFileExtension (kind));
			
			return fopen (path,mode);
		} else
			return NULL;
	}	
}

int FClose (File f)
{
	return fclose ((FILE *) f);
}

int FDelete (char *fname, FileKind kind)
{
	char path[MAXPATHLEN];
	Bool res;
	
	res = findfilepath (fname,kind,path);

	if (res)
		return remove (path);
	else
		return -1;
}

int FPrintF (File f, char *fmt, ...)
{	int n;
	va_list args;
	
	va_start (args, fmt);

	n = vfprintf ((FILE*)f, fmt, args);

	va_end (args);
	return n;
}

size_t FWrite (void *ptr, size_t size, size_t count, File f)
{
	return fwrite (ptr, size, count, (FILE *) f);
}

size_t FRead  (void *ptr, size_t size, size_t count, File f)
{
	return fread (ptr, size, count, (FILE *) f);
}

char *FGetS (char *s, int n, File f)
{
	return fgets (s, n, (FILE *) f);
}

int FPutS (char *s, File f)
{
	return fputs (s, (FILE *) f);
}

int FSeek (File f, long offset, int origin)
{
	return fseek ((FILE *) f, offset, origin);
}

long FTell (File f)
{
	return ftell ((FILE *) f);
}

SysTime GetSysTime (unsigned scale)
{
	return 0;
}

void StopTimer (void)
{
}

void ResetTimer (void)
{
}

void DoError (char *fmt, ...)
{
	va_list args;
	
	va_start (args, fmt);

	(void) vfprintf (stderr, fmt, args);
	
	va_end (args);
}

void DoFatalError (char *fmt, ...)
{
	va_list args;
	
	va_start (args, fmt);

	(void) vfprintf (stderr, fmt, args);
	
	va_end (args);

	exit (0);
}

void CmdError (char *errormsg,...)
{
	va_list args;
	
	va_start (args, errormsg);

	fputs ("Command line error: ", stdout);
	vfprintf (stdout, errormsg, args);
	fputc ('\n', stdout); 
		
	va_end (args);
}

static void DoNothing (void)
{
}

void (*SetSignal (void (*f) (void))) (void)
{	
	return DoNothing;
}

int CheckInterrupt (void)
{	
	return 0;
}
 
void *Alloc (long unsigned count, SizeT size)
{	
	if (size == 1){
		if (count >= MAXUNSIGNED)
			DoFatalError ("Allocate: severe memory allocation problem");
		return (void *) malloc ((size_t) count);
	}
	else if (count >= (MAXUNSIGNED / size))
		DoFatalError ("Allocate: severe memory allocation problem");
	return (void *) malloc ((size_t) (count * size));
}

void Free (void *p)
{
	(void) free (p);
}

#ifdef WRITE_DCL_MODIFICATION_TIME
void FWriteFileTime (FileTime file_time,File f)
{
	SYSTEMTIME date_and_time;
	FILETIME local_file_time;
	
	FileTimeToLocalFileTime (&file_time,&local_file_time);
	
	FileTimeToSystemTime (&local_file_time,&date_and_time);
	
	fprintf (f,"%04d%02d%02d%02d%02d%02d",
				date_and_time.wYear,date_and_time.wMonth,date_and_time.wDay,
				date_and_time.wHour,date_and_time.wMinute,date_and_time.wSecond);
}
#endif