mac_io.c 26.5 KB
Newer Older
clean's avatar
clean committed
1

2
3
4
5
6
7
#include "compiledefines.h"

#ifdef KARBON
# define TARGET_API_MAC_CARBON 1
#endif

clean's avatar
clean committed
8
9
#define for_l(v,l,n) for(v=(l);v!=NULL;v=v->n)

John van Groningen's avatar
John van Groningen committed
10
#if defined (applec) || defined (__MWERKS__) || defined (__MRC__) || defined (GNU_C)
clean's avatar
clean committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#	define mpwc
#endif

#ifdef MAKE_MPW_TOOL
# define NO_CLEAN_SYSTEM_FILES_FOLDERS
# define NEWBRIDGE
#endif

#if defined (mpwc) /* && ! (defined (MAKE_MPW_TOOL) && !defined (MAIN_CLM)) */
#	define USE_PATH_CACHE 1
#else
#	define USE_PATH_CACHE 0
#endif

#ifndef _SYSTEM_
26
#	include "types.t"
clean's avatar
clean committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#	include "system.h"
#endif

#if defined (POWER)
#	define USE_SYSTEM_ALLOC 1
#else
#	define USE_SYSTEM_ALLOC 0
#endif

#include <stdio.h>
#ifndef mpwc
#	include <pascal.h>
#endif
#include <Files.h>
#include <Memory.h>
John van Groningen's avatar
John van Groningen committed
42
#ifndef mpwc
clean's avatar
clean committed
43
44
45
46
#	include <strings.h>
#endif
#include <Devices.h>
#include <Events.h>
John van Groningen's avatar
John van Groningen committed
47
48
49
#ifdef KARBON
# include <Script.h>
#endif
clean's avatar
clean committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef mpwc
#	include <unix.h>
#endif
#if USE_PATH_CACHE
#	include "path_cache.h"
#endif

#undef FOLDER_DOES_NOT_EXIST_ERRORS

static unsigned char *copy_c_to_p_string (char *c_string,char *p_string)
{
	char *s,*d,c;
	
	d=p_string+1;
	s=c_string;
	while (c=*s++, c!='\0')
		*d++=c;
	
	*p_string=s-1-c_string;
	
	return (unsigned char*) p_string;
}

73
74
75
76
77
78
79
#ifdef KARBON
	static int FindFileUTCDateTime0 (char *fname,UTCDateTime *file_time_p)
	{
		int err;
		FSCatalogInfo catalog_info;
		FSRef fs_ref;
		FSSpec fs_spec;
clean's avatar
clean committed
80

81
82
83
		copy_c_to_p_string (fname,(char*)&fs_spec.name);
		fs_spec.parID=0;
		fs_spec.vRefNum=0;
clean's avatar
clean committed
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
		err = FSpMakeFSRef (&fs_spec,&fs_ref);
		if (err)
			return 0;

		err = FSGetCatalogInfo (&fs_ref,kFSCatInfoContentMod,&catalog_info,NULL,NULL,NULL);
		if (err)
			return 0;
		else {
			*file_time_p=catalog_info.contentModDate;

			return 1;	
		}
	}

	static int FindFileUTCDateTime (char *fname,struct vd_id vd_id,UTCDateTime *file_time_p)
	{
		int err;
		FSCatalogInfo catalog_info;
		FSRef fs_ref;
		FSSpec fs_spec;

		copy_c_to_p_string (fname,(char*)&fs_spec.name);
		fs_spec.parID=vd_id.directory_id;
		fs_spec.vRefNum=vd_id.volume_id;

		err = FSpMakeFSRef (&fs_spec,&fs_ref);
		if (err)
			return 0;

		err = FSGetCatalogInfo (&fs_ref,kFSCatInfoContentMod,&catalog_info,NULL,NULL,NULL);
		if (err!=0)
			return 0;
		else {
			*file_time_p=catalog_info.contentModDate;

			return 1;	
		}
	}
#else
	static FileTime FindFileTime (char *fname,int wd_ref_num)
	{
		int err;
		FileParam fpb;
		char p_string [256];

		fpb.ioNamePtr=copy_c_to_p_string (fname,p_string);
		fpb.ioFDirIndex=0;
		fpb.ioFVersNum=0;
		fpb.ioVRefNum=wd_ref_num;

#ifdef KARBON
		err = PBHGetFInfo ((HParmBlkPtr)&fpb,0);
clean's avatar
clean committed
137
#else
138
139
140
141
142
# ifdef mpwc
		err = PBGetFInfoSync ((ParmBlkPtr)&fpb);
# else
		err = PBGetFInfo (&fpb, 0);
# endif
clean's avatar
clean committed
143
144
#endif

145
146
147
148
149
150
		if (err)
			return NoFile;
		else
			return fpb.ioFlMdDat;
	}
#endif
clean's avatar
clean committed
151
152
153
154
155

char *PATHLIST;

#ifdef mpwc
struct path_list {
156
157
158
159
# ifdef KARBON
	struct vd_id		path_vd_id;
	struct vd_id		path_clean_system_files_vd_id;
# else
clean's avatar
clean committed
160
161
	short				path_wd_ref_num;
	short				path_clean_system_files_wd_ref_num;
162
# endif
clean's avatar
clean committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
	struct path_list *	path_next;
#if defined (__MWERKS__) || defined (__MRC__)
	char				path_name[];
#else
	char				path_name[0];
#endif
};

static struct path_list *path_list=NULL;

static void add_directory_to_path_list (char *path_name,struct path_list **old_path_list_h)
{
	struct path_list *new_path,**last_path_p;
	int path_name_length;
	char p_string [256];
178
179
180
181
182
183
#ifdef KARBON
	struct vd_id vd_id,clean_system_files_vd_id;
	FSSpec fs_spec;
	FSRef fs_ref;
#else
	short wd_ref_num,clean_system_files_wd_ref_num;
clean's avatar
clean committed
184
185
	CInfoPBRec fpb;
	WDPBRec wd_pb;
186
#endif
clean's avatar
clean committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
	int err,root_path;

	root_path=0;

	if (path_name){
		char *p;
		
		for (p=path_name; *p!=':' && *p!='\0'; ++p)
			;
			
		if (*p=='\0'){
			root_path=1;
			p[0]=':';
			p[1]='\0';
		}
	}

204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#ifdef KARBON
	vd_id.volume_id=0;
	vd_id.directory_id=0;
	err = FSMakeFSSpec (0,0,path_name ? copy_c_to_p_string (path_name,p_string) : (unsigned char*)"\001:",&fs_spec);
	if (err==0){
		err = FSpMakeFSRef (&fs_spec,&fs_ref);
		if (err==0){
			FSCatalogInfo catalog_info;
			
			err = FSGetCatalogInfo (&fs_ref,kFSCatInfoVolume|kFSCatInfoNodeID,&catalog_info,NULL,NULL,NULL);
			
			if (err==0){
				vd_id.volume_id=catalog_info.volume;
				vd_id.directory_id=catalog_info.nodeID;
			}
		}
	}
#else
clean's avatar
clean committed
222
223
224
225
226
227
228
229
230
231
	if (path_name)
		fpb.hFileInfo.ioNamePtr=copy_c_to_p_string (path_name,p_string);
	else
		fpb.hFileInfo.ioNamePtr=(unsigned char*)"\001:";

	fpb.hFileInfo.ioVRefNum=0;
	fpb.hFileInfo.ioFDirIndex=0;
	fpb.hFileInfo.ioDirID=0;

	err = PBGetCatInfoSync (&fpb);
232
#endif
clean's avatar
clean committed
233
234
235
236
237
238
239
240
241
242
243
244
245

	if (err!=0){
#ifdef FOLDER_DOES_NOT_EXIST_ERRORS
		if (path_name)
			fprintf (stderr,"folder '%s' does not exist\n",path_name);
# ifdef ADD_NULL_PATH 
		else
			fprintf (stderr,"folder ':' does not exist\n");
# endif
#endif
		return;
	}

246
#ifndef KARBON
clean's avatar
clean committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
	wd_pb.ioNamePtr=fpb.hFileInfo.ioNamePtr;
	wd_pb.ioWDProcID='ClCo';

	wd_pb.ioVRefNum=0;
	wd_pb.ioWDDirID=0;
/*
	wd_pb.ioVRefNum=fpb.hFileInfo.ioVRefNum;
	wd_pb.ioWDDirID=fpb.hFileInfo.ioDirID;
*/
	err = PBOpenWD (&wd_pb,0);
	if (err!=0){
		if (path_name)
			fprintf (stderr,"folder '%s' does not exist\n",path_name);
#ifdef ADD_NULL_PATH 
		else
			fprintf (stderr,"folder ':' does not exist\n");
#endif
		return;
	}

	wd_ref_num=wd_pb.ioVRefNum;
268
#endif
clean's avatar
clean committed
269
270
271
272
273
274
275
276
277

#ifndef NO_CLEAN_SYSTEM_FILES_FOLDERS
	if (path_name){
		if (root_path)
			strcat (path_name,"Clean System Files");
		else
			strcat (path_name,":Clean System Files");
	} else
		path_name="Clean System Files";
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

# ifdef KARBON
	clean_system_files_vd_id.volume_id=0;
	clean_system_files_vd_id.directory_id=0;
	err = FSMakeFSSpec (0,0,copy_c_to_p_string (path_name,p_string),&fs_spec);

	if (err==fnfErr){
		long dir_id;
		
		err=FSpDirCreate (&fs_spec,smSystemScript,&dir_id);
	}

	if (err==0){
		err = FSpMakeFSRef (&fs_spec,&fs_ref);
		if (err==0){
			FSCatalogInfo catalog_info;
			
			err = FSGetCatalogInfo (&fs_ref,kFSCatInfoVolume|kFSCatInfoNodeID,&catalog_info,NULL,NULL,NULL);
			
			if (err==0){
				clean_system_files_vd_id.volume_id=catalog_info.volume;
				clean_system_files_vd_id.directory_id=catalog_info.nodeID;
			}
		}
	}
clean's avatar
clean committed
303
	
304
305
306
307
308
309
	if (err!=0){
		fprintf (stderr,"cannot create folder '%s'\n",path_name);

		return;	
	}
# else
clean's avatar
clean committed
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
	fpb.hFileInfo.ioNamePtr=copy_c_to_p_string (path_name,p_string);
	fpb.hFileInfo.ioVRefNum =0;
	fpb.hFileInfo.ioFDirIndex=0;
	fpb.hFileInfo.ioDirID=0;

	err = PBGetCatInfoSync (&fpb);

	if (err!=0){
		err = PBDirCreateSync ((HParamBlockRec*)&fpb);

		if (err!=0){
			fprintf (stderr,"cannot create folder '%s'\n",path_name);

			return;
		}
	}

	wd_pb.ioNamePtr=fpb.hFileInfo.ioNamePtr;
	wd_pb.ioWDProcID='ClCo';

	wd_pb.ioVRefNum=0;
	wd_pb.ioWDDirID=0;
/*
	wd_pb.ioVRefNum=fpb.hFileInfo.ioVRefNum;
	wd_pb.ioWDDirID=fpb.hFileInfo.ioDirID;
*/
	err = PBOpenWD (&wd_pb,0);
	if (err!=0){
		if (path_name)
			fprintf (stderr,"folder '%s' does not exist\n",path_name);
		return;
	}

	clean_system_files_wd_ref_num=wd_pb.ioVRefNum;
344
# endif
clean's avatar
clean committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

	path_name_length=strlen (path_name)-strlen (":Clean System Files");
	if (path_name_length<0)
		path_name_length=0;
	path_name[path_name_length]='\0';
#else
	clean_system_files_wd_ref_num=0;
		
	if (path_name==NULL)
		path_name="";
		
	path_name_length=strlen (path_name);
#endif

	last_path_p=&path_list;
	while (*last_path_p)
		last_path_p=&(*last_path_p)->path_next;

	/* reuse memory from previous path_list */
	{
		struct path_list *old_path_list_p;

		for (; (old_path_list_p=*old_path_list_h)!=NULL; old_path_list_h=&old_path_list_p->path_next){
368
369
370
371
372
373
374
375
			if (
#ifdef KARBON
				old_path_list_p->path_vd_id.volume_id==vd_id.volume_id &&
				old_path_list_p->path_vd_id.directory_id==vd_id.directory_id &&
				old_path_list_p->path_clean_system_files_vd_id.volume_id==clean_system_files_vd_id.volume_id &&
				old_path_list_p->path_clean_system_files_vd_id.directory_id==clean_system_files_vd_id.directory_id &&
#else
				old_path_list_p->path_wd_ref_num==wd_ref_num &&
clean's avatar
clean committed
376
				old_path_list_p->path_clean_system_files_wd_ref_num==clean_system_files_wd_ref_num &&
377
#endif
clean's avatar
clean committed
378
379
380
381
382
383
384
385
386
387
388
389
				!strcmp (old_path_list_p->path_name,path_name))
			{
				*old_path_list_h=old_path_list_p->path_next;

				old_path_list_p->path_next=NULL;
				*last_path_p=old_path_list_p;
				return;
			}
		}
	}
	
	new_path=(struct path_list*)Alloc (1,sizeof (struct path_list)+1+path_name_length);
390
391
392
393
#ifdef KARBON
	new_path->path_vd_id=vd_id;
	new_path->path_clean_system_files_vd_id=clean_system_files_vd_id;
#else
clean's avatar
clean committed
394
395
	new_path->path_wd_ref_num=wd_ref_num;
	new_path->path_clean_system_files_wd_ref_num=clean_system_files_wd_ref_num;
396
#endif
clean's avatar
clean committed
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
	strcpy (new_path->path_name,path_name);
	new_path->path_next=NULL;

	*last_path_p=new_path;
}
#endif

extern char *path_parameter;

void GetInitialPathList (void)
{
	char path[MAXPATHLEN];
	struct path_list *old_path_list;
	char *s,*path_elem,*p;
	int c;

	p = path_parameter;

	if (p==NULL){
		PATHLIST="\0";
		return;
	}

	PATHLIST = p;
	
	old_path_list=path_list;

	path_list=NULL;

#ifdef ADD_NULL_PATH
	add_directory_to_path_list (NULL,&old_path_list);
#endif

    path_elem =PATHLIST;

    s=path_elem;
	for (c = *s;;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';
			
			add_directory_to_path_list (path,&old_path_list);
		    		    
		    if (c == '\0')
				break;
		    
			path_elem = ++s;
		} else
		    ++s;
    }
}

void FreePathList (void)
{
	struct path_list *path,*next_path;
	
	path=path_list;
	path_list=NULL;

	while (path!=NULL){
		next_path=path->path_next;
		Free (path);
		path=next_path;
	}
}

char *GetFileExtension (FileKind kind)
{
	switch (kind){
		case abcFile:		return ".abc";
		case obj00File:		return ".obj0";
		case obj20File:		return ".obj1";
		case obj81File:		return ".obj2";
		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 "";
	}	
}

#ifdef NEWBRIDGE
extern char *clean_abc_path; /* imported from clm.c */
#endif

#if defined (mpwc) && WRITE_DCL_MODIFICATION_TIME
	static Bool find_filepath_and_time (char *file_name,FileKind kind,char *path,FileTime *file_time_p)
	{
		char *file_extension;
		struct path_list *path_elem;
	
		file_extension=GetFileExtension (kind);

		if (file_name[0]!=':'){
			strcpy (path,file_name);
			strcat (path,file_extension);

#if USE_PATH_CACHE
			if (kind==dclFile){
				struct search_dcl_path_in_cache_result r;
				
				if (search_dcl_path_in_cache (file_name,&r)){
					strcpy (path,r.path);

#ifndef NO_CLEAN_SYSTEM_FILES_FOLDERS
					if (path[0]=='\0'){
					} else
						strcat (path,":");
#else
					if (path[0]!='\0' && path[strlen (path)-1]!=':')
						strcat (path,":");
#endif

					strcat (path,file_name);
					strcat (path,file_extension);
					
					*file_time_p=r.file_time;

					return True;
				}
			}
#endif

			for_l (path_elem,path_list,path_next){
532
533
534
535
536
#ifdef KARBON
				UTCDateTime file_time;

				if (FindFileUTCDateTime (path,path_elem->path_vd_id,&file_time)){
#else
clean's avatar
clean committed
537
				unsigned long file_time;
538
				short wd_ref_num;
clean's avatar
clean committed
539
540
541
542
543
544

				wd_ref_num=path_elem->path_wd_ref_num;
	
				file_time=FindFileTime (path,wd_ref_num);

				if (file_time!=NoFile){
545
#endif
clean's avatar
clean committed
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
					strcpy (path,path_elem->path_name);

#ifndef NO_CLEAN_SYSTEM_FILES_FOLDERS
					if (path[0]=='\0'){
					} else
						strcat (path,":");
#else
					if (path[0]!='\0' && path[strlen (path)-1]!=':')
						strcat (path,":");
#endif

					strcat (path,file_name);
					strcat (path,file_extension);

#if USE_PATH_CACHE
					if (kind==dclFile)
562
563
564
565
566
567
568
						cache_dcl_path (file_name,
# ifdef KARBON
							path_elem->path_vd_id,path_elem->path_clean_system_files_vd_id,
# else
							path_elem->path_wd_ref_num,path_elem->path_clean_system_files_wd_ref_num,
# endif
							file_time,path_elem->path_name);
clean's avatar
clean committed
569
570
571
572
573
574
575
576
577
578
579
#endif				
					*file_time_p=file_time;
							
					return True;
				}
			}
		}

		strcpy (path,file_name);	
		strcat (path,file_extension);

580
581
582
#ifdef KARBON
		return FindFileUTCDateTime0 (path,file_time_p);
#else
clean's avatar
clean committed
583
584
585
586
587
588
589
590
591
592
		{	
			unsigned long file_time;
	
			file_time=FindFileTime (path,0);
			if (file_time==NoFile)
				return False;
			else {
				*file_time_p=file_time;
				return True;
			}
593
594
		}
#endif
clean's avatar
clean committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
	}
#endif

#ifdef mpwc
	static Bool findfilepath (char *file_name,FileKind kind,char *path)
	{
		char *file_extension;
		int in_clean_system_files_folder;
		struct path_list *path_elem;
	
		switch (kind){
			case abcFile:
			case obj00File:
			case obj20File:
			case obj81File:
				in_clean_system_files_folder=1;
				break;
			default:
				in_clean_system_files_folder=0;
		}

		file_extension=GetFileExtension (kind);

		if (file_name[0]!=':'){
			strcpy (path,file_name);
			strcat (path,file_extension);

#if USE_PATH_CACHE
			if (kind==dclFile){
				struct search_dcl_path_in_cache_result r;
				
				if (search_dcl_path_in_cache (file_name,&r)){
					strcpy (path,r.path);

#ifndef NO_CLEAN_SYSTEM_FILES_FOLDERS
					if (path[0]=='\0'){
						if (in_clean_system_files_folder)
							strcpy (path,"Clean System Files:");
					} else
						if (in_clean_system_files_folder)
							strcat (path,":Clean System Files:");
						else
							strcat (path,":");
#else
					if (path[0]!='\0' && path[strlen (path)-1]!=':')
						strcat (path,":");
#endif

					strcat (path,file_name);
					strcat (path,file_extension);
										
					return True;
				}
			}
#endif

#ifdef NEWBRIDGE
			for (path_elem=(clean_abc_path!=NULL && !in_clean_system_files_folder && path_list!=NULL)
							? path_list->path_next 
							: path_list;
				 path_elem!=NULL;
				 path_elem=(clean_abc_path!=NULL && in_clean_system_files_folder)
				 			? NULL
				 			: path_elem->path_next)
			{
#else
			for_l (path_elem,path_list,path_next){
#endif
663
664
665
666
667
668
669
670
671
672
673
674
675
#ifdef KARBON
				struct vd_id vd_id;
				FileTime file_time;

# ifndef NO_CLEAN_SYSTEM_FILES_FOLDERS
				if (in_clean_system_files_folder)
					vd_id=path_elem->path_clean_system_files_vd_id;
				else
# endif
					vd_id=path_elem->path_vd_id;
	
				if (FindFileUTCDateTime (path,vd_id,&file_time)){
#else
clean's avatar
clean committed
676
677
678
				short wd_ref_num;
				unsigned long file_time;

679
# ifndef NO_CLEAN_SYSTEM_FILES_FOLDERS
clean's avatar
clean committed
680
681
682
				if (in_clean_system_files_folder)
					wd_ref_num=path_elem->path_clean_system_files_wd_ref_num;
				else
683
# endif
clean's avatar
clean committed
684
685
686
687
688
					wd_ref_num=path_elem->path_wd_ref_num;
	
				file_time=FindFileTime (path,wd_ref_num);

				if (file_time!=NoFile){
689
#endif
clean's avatar
clean committed
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
					strcpy (path,path_elem->path_name);

#ifndef NO_CLEAN_SYSTEM_FILES_FOLDERS
					if (path[0]=='\0'){
						if (in_clean_system_files_folder)
							strcpy (path,"Clean System Files:");
					} else
						if (in_clean_system_files_folder)
							strcat (path,":Clean System Files:");
						else
							strcat (path,":");
#else
					if (path[0]!='\0' && path[strlen (path)-1]!=':')
						strcat (path,":");
#endif

					strcat (path,file_name);
					strcat (path,file_extension);

#if USE_PATH_CACHE
					if (kind==dclFile && !in_clean_system_files_folder)
711
712
713
714
715
716
717
						cache_dcl_path (file_name,
# ifdef KARBON
							path_elem->path_vd_id,path_elem->path_clean_system_files_vd_id,
# else
							path_elem->path_wd_ref_num,path_elem->path_clean_system_files_wd_ref_num,
# endif
							file_time,path_elem->path_name);
clean's avatar
clean committed
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
#endif						
					return True;
				}
			}
#ifdef NEWBRIDGE
			return False;
#endif
		}


#ifndef NO_CLEAN_SYSTEM_FILES_FOLDERS
		if (in_clean_system_files_folder && file_name[0]!=':'){
			strcpy (path,":Clean System Files:");
			strcat (path, file_name);
		} else
#endif
		strcpy (path,file_name);
	
		strcat (path,file_extension);
	
738
739
740
741
742
743
744
745
# ifdef KARBON
		{
			FileTime file_time;
			return FindFileUTCDateTime0 (path,&file_time);
		}
# else
		return FindFileTime (path,0)!=NoFile;
# endif
clean's avatar
clean committed
746
747
748
749
750
751
752
753
754
755
756
757
758
	}
#else
	static Bool findfilepath (char *wname, FileKind kind, char *path)
	{
		char *s,*pathelem,c,*file_extension;
		FILE *f;
		
		file_extension=GetFileExtension (kind);
		
		/* first try current directory */
		strcpy (path,wname);
		strcat (path,file_extension);
	
759
760
761
# ifdef KARBON
		if (FindFileUTCDateTime0 (path) != NoFile)
# else
clean's avatar
clean committed
762
		if (FindFileTime (path,0) != NoFile)
763
# endif
clean's avatar
clean committed
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
			return True;
		
		pathelem = PATHLIST;

		s = pathelem;	
		for (c = *s;;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, wname);
				strcat (path,file_extension);
				
783
784
785
#ifdef KARBON
				if (FindFileUTCDateTime0 (path) != NoFile)
#else
clean's avatar
clean committed
786
				if (FindFileTime (path,0) != NoFile)
787
#endif
clean's avatar
clean committed
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
					return True;
							
				/* if all else fails, exit the loop */
				if (c == '\0')
					break;
				
				pathelem = ++s;
			} else
				++s;
		}
	
		/* if all else fails, return False, and the current name */
		strcpy (path,wname);
		strcat (path,file_extension);
	
		return False;
	}
#endif

807
#if defined (GNU_C)
John van Groningen's avatar
John van Groningen committed
808
char *convert_file_name (char *file_name,char *buffer)
809
810
811
812
813
814
815
816
817
818
819
820
821
{
	CFURLRef hfs_url;
	CFStringRef	hfs_string, posix_string;
	
	hfs_string = CFStringCreateWithCString (NULL/*kCFAllocatorDefault*/,file_name,kCFStringEncodingMacRoman);
	hfs_url = CFURLCreateWithFileSystemPath (NULL/*kCFAllocatorDefault*/,hfs_string,kCFURLHFSPathStyle,/*isDirectory*/false);
	CFRelease (hfs_string);
	posix_string = CFURLCopyFileSystemPath (hfs_url,kCFURLPOSIXPathStyle);
	CFRelease (hfs_url);
	if (! CFStringGetCString (posix_string,buffer,512,kCFStringEncodingMacRoman)){
		CFRelease (posix_string);
		return NULL;
	}
John van Groningen's avatar
John van Groningen committed
822
823
824
825
826
827
828
829
830
831
	return buffer;
}

static FILE *fopen_with_file_name_conversion (char *file_name,char *mode)
{
	char buffer[512+1];

	file_name=convert_file_name (file_name,buffer);
	if (file_name==NULL)
		return NULL;
832
833
834
835
836
837
838

	return fopen (file_name,mode);
}

# define fopen fopen_with_file_name_conversion
#endif

clean's avatar
clean committed
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
#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 (File) fopen (path, mode);
	else
		return NULL;
}
#endif

John van Groningen's avatar
John van Groningen committed
854
855
856
857
#if 0
extern File rules_file;
#endif

clean's avatar
clean committed
858
859
860
861
862
863
864
865
File FOpen (char *file_name,FileKind kind, char *mode)
{
	char path[MAXPATHLEN];
	Bool res;

#ifdef mpwc
	if (mode[0]=='r'){
		findfilepath (file_name,kind,path);
John van Groningen's avatar
John van Groningen committed
866
867
868
869
870

# if 0
		FPrintF (rules_file,"%s\n",path);
# endif

clean's avatar
clean committed
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
		return (File) fopen (path, mode);
	} else {
		char *p;
		int full_path_name;
				
		for (p=file_name; *p!=':' && *p!='\0'; ++p)
			;
		full_path_name = *p==':';

		if (full_path_name){
			strcpy (path,file_name);
			strcat (path,GetFileExtension (kind));
			return (File) fopen (path,mode);			
		} else {
			res = findfilepath (file_name,dclFile, path);
			if (!res)
				res = findfilepath (file_name,iclFile, path);

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

				after_last_colon=NULL;

				p=path;
				while (*p)
					if (*p++==':')
						after_last_colon=p;
				
				if (after_last_colon==NULL){
					after_last_colon=path;
					*after_last_colon++=':';
				}
#ifndef NO_CLEAN_SYSTEM_FILES_FOLDERS
				strcpy (after_last_colon,"Clean System Files:");
#endif
				strcat (after_last_colon,file_name);
			    strcat (after_last_colon,GetFileExtension (kind));
				
				return (File) fopen (path, mode);
			} else
				return (File) Null;
		}
	}
#else
	res=findfilepath (file_name, kind, path);

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

#if USE_PATH_CACHE

#define BUFFER_SIZE 1024

struct file_block {
	int					file_block_size;
	struct file_block *	file_block_next;
	char				file_block_data[BUFFER_SIZE];
};

static int reading_from_cache;
File dcl_file;

static struct file_block **next_file_block_l;

#if WRITE_DCL_MODIFICATION_TIME
int open_dcl_file_for_block_reading_with_file_time (char *file_name,File *file_p,FileTime *file_time_p)
{
	char path[256];
	struct file_block **file_blocks_p;
	
	file_blocks_p=get_file_blocks_p_and_time_of_dcl_file (file_name,file_time_p);
	
	if (file_blocks_p && *file_blocks_p){
		reading_from_cache=1;
		next_file_block_l=file_blocks_p;
		*file_p=NULL;
		return 1;
	} else {
		reading_from_cache=0;

		findfilepath (file_name,dclFile,path);

		file_blocks_p=get_file_blocks_p_and_time_of_dcl_file (file_name,file_time_p);
		if (file_blocks_p==NULL){
			*file_p=NULL;
			return 0;
		}
		next_file_block_l=file_blocks_p;

		dcl_file=(File) fopen (path,"rb");
	
		*file_p=dcl_file;
		if (dcl_file){
			setvbuf ((FILE*) dcl_file,NULL,_IOFBF,8192);

			return 1;
		} else
			return 0;
	}
}
#endif

int open_dcl_file_for_block_reading (char *file_name,File *file_p)
{
	char path[256];
	struct file_block **file_blocks_p;
	
	file_blocks_p=get_file_blocks_p_of_dcl_file (file_name);
	
	if (file_blocks_p && *file_blocks_p){
		reading_from_cache=1;
		next_file_block_l=file_blocks_p;
		*file_p=NULL;
		return 1;
	} else {
		reading_from_cache=0;

		findfilepath (file_name,dclFile,path);

		file_blocks_p=get_file_blocks_p_of_dcl_file (file_name);
		if (file_blocks_p==NULL){
			*file_p=NULL;
			return 0;
		}
		next_file_block_l=file_blocks_p;

		dcl_file=(File) fopen (path,"rb");
	
		*file_p=dcl_file;
		if (dcl_file){
			setvbuf ((FILE*) dcl_file,NULL,_IOFBF,8192);

			return 1;
		} else
			return 0;
	}
}

int read_next_block_from_dcl_file (char *buffer)
{
	if (reading_from_cache){
		struct file_block *file_block_p;
		
		file_block_p=*next_file_block_l;
		
		if (file_block_p!=NULL){
			int n_chars;
			
			n_chars=file_block_p->file_block_size;
			memcpy (buffer,file_block_p->file_block_data,n_chars);
			
			if (n_chars>0)
				next_file_block_l=&file_block_p->file_block_next;

			return n_chars;
		} else
			return 0;
	} else {
		int n_chars_read;
		
		n_chars_read = FRead (buffer,1,1024l,dcl_file);	
		if (n_chars_read>0){
			struct file_block *new_file_block_p;
			
			new_file_block_p=(struct file_block*)Alloc (1,sizeof (struct file_block));
			
			new_file_block_p->file_block_size=n_chars_read;
			memcpy (new_file_block_p->file_block_data,buffer,n_chars_read);
			new_file_block_p->file_block_next=NULL;
			
			*next_file_block_l=new_file_block_p;
			next_file_block_l=&new_file_block_p->file_block_next;
		}
		
		return n_chars_read;
	}
}
#endif

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

extern int FDelete (char *fname, FileKind kind);

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

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

#define OUTSIZE 2048

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

	vsprintf (outbuffer, fmt, args);
	
	n = strlen (outbuffer);
	if (n >= OUTSIZE)
	{	fputs ("FATAL ERROR: out buffer to small\n", stderr);
		exit (1);
	}

	va_end (args);

	return fputs (outbuffer, (FILE *) f);
} /* FPrintF */

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

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

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

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

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

#ifndef KARBON
clean's avatar
clean committed
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
FileTime FGetFileTime (char *fname, FileKind kind)
{
	char path[MAXPATHLEN];
	Bool res;
	
	res = findfilepath (fname, kind, path);
	
/*	FPrintF (StdOut, "timing %s\n", fname); */

	if (res)
1132
1133
1134
#ifdef KARBON
		return FindFileUTCDateTime0 (path);
#else
clean's avatar
clean committed
1135
		return FindFileTime (path,0);
1136
#endif
clean's avatar
clean committed
1137
1138
	else
		return NoFile;
1139
1140
}
#endif
clean's avatar
clean committed
1141
1142
1143
1144
1145

#ifdef WRITE_DCL_MODIFICATION_TIME
void FWriteFileTime (FileTime file_time,File f)
{
	DateTimeRec date_and_time;
1146
1147
1148
1149
1150
# ifdef KARBON
	{
		LocalDateTime local_date_and_time;
		LongDateRec long_date_and_time;
		SInt64 long_file_time;
clean's avatar
clean committed
1151
	
1152
1153
1154
1155
1156
1157
		ConvertUTCToLocalDateTime (&file_time,&local_date_and_time);
		long_file_time=((SInt64)local_date_and_time.highSeconds<<32) | local_date_and_time.lowSeconds;
		LongSecondsToDate (&long_file_time,&long_date_and_time);
		date_and_time=long_date_and_time.od.oldDate;
	}
# else
clean's avatar
clean committed
1158
	SecondsToDate (file_time,&date_and_time);
1159
# endif
clean's avatar
clean committed
1160
1161
1162
1163
1164
1165
1166
	
	fprintf (f,"%04d%02d%02d%02d%02d%02d",
				date_and_time.year,date_and_time.month,date_and_time.day,
				date_and_time.hour,date_and_time.minute,date_and_time.second);
}

#endif
1167
1168
1169
1170
1171

Bool GetOptionsFromIclFile (char *fname, CompilerOptions *opts)
{
	return False;
} /* GetOptionsFromIclFile */
clean's avatar
clean committed
1172
1173
1174
1175
1176
1177
1178
1179
1180
	
void DoError (char *fmt, ...)
{	va_list args;
	
	va_start (args, fmt);

	(void) vfprintf (stderr, fmt, args);
	
	va_end (args);
1181
}
clean's avatar
clean committed
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192

void DoFatalError (char *fmt, ...)
{	va_list args;
	
	va_start (args, fmt);
	
	fputs ("Fatal error: ", stderr);
	(void) vfprintf (stderr, fmt, args);
	va_end (args);

	exit (0);
1193
}
clean's avatar
clean committed
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204

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);
1205
}
clean's avatar
clean committed
1206
1207
1208
1209
1210

extern long GetMainModuleVolume (void);
long GetMainModuleVolume (void)
{
	return 0;
1211
}
clean's avatar
clean committed
1212
1213
1214

static void Nothing (void)
{
1215
}
clean's avatar
clean committed
1216
1217
1218
1219
1220
1221
1222

static void (*interfunct) (void) = Nothing;

void (*SetSignal (void (*f) (void))) (void)
{	void (*oldf) () = interfunct;
	interfunct = f;
	return oldf;
1223
}
clean's avatar
clean committed
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335

int CheckInterrupt ()
{
#ifndef mpwc
	EventRecord the_Event;	/* holds the event record */

	GetNextEvent(everyEvent, &the_Event);

	if (the_Event.what == keyDown && the_Event.modifiers & cmdKey)
	{
		char c;

		c  = (unsigned char) the_Event.message;
		if (c == '.')
		{	(*interfunct) ();
			return True;
		}
	}
#endif
	return False;
} /* CheckInterrupt */

void *Alloc (unsigned long count, SizeT size)
{		
	if (size!=1){
		if (count >= MAXUNSIGNED / size)
			DoFatalError ("Allocate: severe memory allocation problem");
		count *= size;	
	}

#if USE_SYSTEM_ALLOC
	return (void *) NewPtr ((size_t) (count));
#else	
	return (void *) malloc ((size_t) (count));
#endif
}

void Free (void *p)
{		
#if USE_SYSTEM_ALLOC
	DisposePtr ((char*)p);
#else
	free ((char *) p);
#endif
}

typedef struct LaunchStruct {
	char	*pfName;
	int		param;
	char	LC[2];
	long	extBlockLen;
	int		fFlags;
	long	launchFlags;
} *pLanchStruct;

int System (char *file_name)
{
#ifdef mpwc
	return 0;
#else
	struct LaunchStruct	myLaunch;
	HFileInfo	myPb;
	int			error;
	char		*errmsg = "Could not launch %s\n";
		
	CtoPstr (file_name);
	
	myPb.ioNamePtr=(StringPtr) file_name;
	myPb.ioVRefNum=0;
	myPb.ioFDirIndex=0;
	myPb.ioDirID=0;
	
	error=PBGetCatInfo ((CInfoPBPtr) &myPb,0);
	if (error)
	{
		PtoCstr (file_name);
		DoError (errmsg, file_name);
		return error;
	}
	
	error=SetVol (Null,0);
	if (error)
	{
		PtoCstr (file_name);
		DoError (errmsg, file_name);
		return error;
	}
		
	myLaunch.pfName=file_name;
	myLaunch.param=0;
	myLaunch.LC[0]='L';
	myLaunch.LC[1]='C';
	myLaunch.extBlockLen=4;
	myLaunch.fFlags=myPb.ioFlFndrInfo.fdFlags;
	myLaunch.launchFlags=0xc0000000;
	
	asm
	{
		lea		myLaunch,A0
		_Launch
		move	d0,error
	}
	
	PtoCstr (file_name);
	
	if (error>=0)
		return 0;
	else
	{	DoError (errmsg, file_name);
		return error;
	}
#endif
John van Groningen's avatar
John van Groningen committed
1336
}