summaryrefslogtreecommitdiffstats
path: root/Mac/fopenRF.c
blob: f18f7d9a6672376fd4b744a323406123d2ab849c (plain)
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

/*
 *  fopenRF.c -- Clone of fopen.c to open Mac resource forks.
 *
 *  Copyright (c) 1989 Symantec Corporation.  All rights reserved.
 *
 */

#include "stdio.h"
#include "errno.h"
#include "string.h"
#include "ansi_private.h"

FILE *fopenRF(char *, char *);
FILE *freopenRF(char *, char *, FILE *);
FILE *__openRF(char *, int, int, FILE *);

#include <Files.h>

#define fcbVPtr(fcb)		(* (VCB **) (fcb + 20))
#define fcbDirID(fcb)		(* (long *) (fcb + 58))
#define fcbCName(fcb)		(fcb + 62)

static void setfiletype(StringPtr, int);
static void stdio_exit(void);
static int fileio(FILE *, int);
static int close(FILE *);
static void replace(unsigned char *, size_t, int, int);


FILE *
fopenRF(filename, mode)
char *filename, *mode;
{
	return(freopenRF(filename, mode, __getfile()));
}


FILE *
freopenRF(filename, mode, fp)
char *filename;
register char *mode;
register FILE *fp;
{
	int omode, oflag;
	
		/*  interpret "rwa"  */
	
	if (mode[0] == 'r') {
		omode = fsRdPerm;
		oflag = 0;
	}
	else if (mode[0] == 'w') {
		omode = fsWrPerm;
		oflag = F_CREAT+F_TRUNC;
	}
	else if (mode[0] == 'a') {
		omode = fsWrPerm;
		oflag = F_CREAT+F_APPEND;
	}
	else {
		errno = EINVAL;
		return(NULL);
	}
		
		/*  interpret "b+"  */
		
	if (mode[1] == 'b') {
		oflag |= F_BINARY;
		if (mode[2] == '+')
			omode = fsRdWrPerm;
	}
	else if (mode[1] == '+') {
		omode = fsRdWrPerm;
		if (mode[2] == 'b')
			oflag |= F_BINARY;
	}
	
		/*  open the file  */
		
	return(__openRF(filename, omode, oflag, fp));
}


FILE *
__openRF(filename, omode, oflag, fp)
char *filename;
int omode, oflag;
register FILE *fp;
{
	ioParam pb;
	char pname[FILENAME_MAX];

	if (fp == NULL)
		return(NULL);
	fclose(fp);
	
		/*  set up pb  */
	
	pb.ioNamePtr = __c2p(filename, pname);
	pb.ioVRefNum = 0;
	pb.ioVersNum = 0;
	pb.ioPermssn = omode;
	pb.ioMisc = 0;

		/*  create file  */

	if (oflag & F_CREAT) {
		asm {
			lea		pb,a0
			_PBCreate
		}
		if (pb.ioResult == noErr)
			oflag &= ~F_TRUNC;
		else if (pb.ioResult == dupFNErr && !(oflag & F_EXCL))
			oflag &= ~F_CREAT;
		else {
			errno = pb.ioResult;
			return(NULL);
		}
	}
	
		/*  open resource file  */
		
	asm {
		lea		pb,a0
		_PBOpenRF
	}
	if (pb.ioResult) {
		errno = pb.ioResult;
		if (oflag & F_CREAT) asm {
			lea		pb,a0
			_PBDelete
		}
		return(NULL);
	}
	fp->refnum = pb.ioRefNum;
	
		/*  get/set file length  */
		
	if (oflag & F_TRUNC) asm {
		lea		pb,a0
		_PBSetEOF
	}
	else if (!(oflag & F_CREAT)) asm {
		lea		pb,a0
		_PBGetEOF
	}
	fp->len = (fpos_t) pb.ioMisc;
		
		/*  initialize rest of FILE structure  */
		
	if (oflag & F_APPEND) {
		fp->append = 1;
		fp->pos = fp->len;
	}
	if (oflag & F_BINARY)
		fp->binary = 1;
	setvbuf(fp, NULL, _IOFBF, BUFSIZ);
	fp->proc = fileio;

		/*  set file type  */

	if (oflag & (F_CREAT|F_TRUNC))
		setfiletype(pb.ioNamePtr, oflag);
		
		/*  done  */
		
	__atexit_stdio(stdio_exit);
	return(fp);
}


/*
 *  setfiletype - set type/creator of new file
 *
 */

static void
setfiletype(name, oflag)
StringPtr name;
int oflag;
{
	fileParam pb;
	
	pb.ioNamePtr = name;
	pb.ioVRefNum = 0;
	pb.ioFVersNum = 0;
	pb.ioFDirIndex = 0;
	asm {
		lea		pb,a0
		_PBGetFInfo
		bmi.s	@1
	}
	pb.ioFlFndrInfo.fdType = pb.ioFlFndrInfo.fdCreator = '????';
	if (!(oflag & F_BINARY))
		pb.ioFlFndrInfo.fdType = 'TEXT';
	asm {
		lea		pb,a0
		_PBSetFInfo
@1	}
}


/*
 *  stdio_exit - stdio shutdown routine
 *
 */

static void
stdio_exit()
{
	register FILE *fp;
	int n;
	
	for (fp = &__file[0], n = FOPEN_MAX; n--; fp++)
		fclose(fp);
}


/*
 *  fileio - I/O handler proc for files and devices
 *
 */

static int
fileio(fp, i)
register FILE *fp;
int i;
{
	ioParam pb;
	
	pb.ioRefNum = fp->refnum;
	switch (i) {
	
				/*  read  */
			
		case 0:
			pb.ioBuffer = (Ptr) fp->ptr;
			pb.ioReqCount = fp->cnt;
			pb.ioPosMode = fp->refnum > 0 ? fsFromStart : fsAtMark;
			pb.ioPosOffset = fp->pos - fp->cnt;
			asm {
				lea		pb,a0
				_PBRead
			}
			if (pb.ioResult == eofErr) {
				fp->pos = pb.ioPosOffset;
				if (fp->cnt = pb.ioActCount)
					pb.ioResult = 0;
				else {
					fp->eof = 1;
					return(EOF);
				}
			}
			if (pb.ioResult) {
				fp->pos -= fp->cnt;
				fp->cnt = 0;
			}
			else if (!fp->binary)
				replace(fp->ptr, fp->cnt, '\r', '\n');
			break;
			
				/*  write  */

		case 1:
			pb.ioBuffer = (Ptr) fp->ptr;
			pb.ioReqCount = fp->cnt;
			pb.ioPosMode = fp->refnum > 0 ? fsFromStart : fsAtMark;
			if ((pb.ioPosOffset = fp->pos - fp->cnt) > fp->len) {
				pb.ioMisc = (Ptr) pb.ioPosOffset;
				asm {
					lea		pb,a0
					_PBSetEOF
					bmi.s	@1
				}
			}
			if (!fp->binary)
				replace(fp->ptr, fp->cnt, '\n', '\r');
			asm {
				lea		pb,a0
				_PBWrite
@1			}
			if (pb.ioResult) {
				fp->pos -= fp->cnt;
				fp->cnt = 0;
			}
			else if (pb.ioPosOffset > fp->len)
				fp->len = pb.ioPosOffset;
			break;
			
				/*  close  */

		case 2:
			pb.ioResult = close(fp);
			break;
	}
	
		/*  done  */
		
	if (pb.ioResult) {
		fp->err = 1;
		errno = pb.ioResult;
		return(EOF);
	}
	return(0);
}


static int
close(fp)
register FILE *fp;
{
	HFileParam pb;
	Str255 buf;
	register char *fcb = FCBSPtr + fp->refnum;
	VCB *vcb = fcbVPtr(fcb);
	register char *s;
	
	pb.ioNamePtr = buf;
	pb.ioFRefNum = fp->refnum;
	pb.ioVRefNum = vcb->vcbVRefNum;
	pb.ioFVersNum = 0;
	
		/*  close temporary file - HFS  */
		
	if (fp->delete && vcb->vcbSigWord == 0x4244) {
		pb.ioDirID = fcbDirID(fcb);
		s = fcbCName(fcb);
		asm {
			lea		buf,a0
			moveq	#0,d0
			move.b	(s),d0
@1			move.b	(s)+,(a0)+
			dbra	d0,@1
			lea		pb,a0
			_PBClose
			bmi.s	@9
			_PBHDelete
		}
	}
	
		/*  close temporary file - MFS  */
		
	else if (fp->delete && vcb->vcbSigWord == 0xD2D7) {
		pb.ioFDirIndex = 1;
		do asm {
			lea		pb,a0
			_PBGetFInfo
			bmi.s	@2
			addq.w	#1,pb.ioFDirIndex
		} while (pb.ioFRefNum != fp->refnum);
		asm {
			lea		pb,a0
			_PBClose
			bmi.s	@9
			_PBDelete
		}
	}
	
		/*  normal case - just close file  */
		
	else {
		asm {
@2			lea		pb,a0
			_PBClose
			bmi.s	@9
		}
	}
	
		/*  flush volume buffer  */
		
	pb.ioNamePtr = 0;
	asm {
		lea		pb,a0
		_PBFlshVol
@9	}
	return(pb.ioResult);
}


/*
 *  replace - routine for doing CR/LF conversion
 *
 */

static void
replace(s, n, c1, c2)
register unsigned char *s;
register size_t n;
register int c1, c2;
{
	register unsigned char *t;
	
	for (; n && (t = memchr(s, c1, n)); s = t) {
		*t++ = c2;
		n -= t - s;
	}
}