5 * Copyright (c) 1991 Symantec Corporation. All rights reserved.
14 #include "ansi_private.h"
16 extern long _ftype
, _fcreator
;
18 #define fcbVPtr(fcb) (* (VCB **) (fcb + 20))
19 #define fcbDirID(fcb) (* (long *) (fcb + 58))
20 #define fcbCName(fcb) (fcb + 62)
22 static void setfiletype(StringPtr
, int);
23 static void stdio_exit(void);
24 static int fileio(FILE *, int);
25 static int close(FILE *);
26 static void replace(unsigned char *, size_t, int, int);
32 fopenRF(const char *filename
, const char *mode
)
34 return(freopenRF(filename
, mode
, __getfile()));
39 freopenRF(const char *filename
, const char *mode
, FILE *fp
)
49 else if (mode
[0] == 'w') {
51 oflag
= F_CREAT
+F_TRUNC
;
53 else if (mode
[0] == 'a') {
55 oflag
= F_CREAT
+F_APPEND
;
69 else if (mode
[1] == '+') {
77 return(__openRF(filename
, omode
, oflag
, fp
));
82 __openRF(const char *filename
, int omode
, int oflag
, FILE *fp
)
85 char pname
[FILENAME_MAX
];
93 pb
.ioNamePtr
= __c2p(filename
, pname
);
101 if (oflag
& F_CREAT
) {
102 PBCreateSync((ParmBlkPtr
)&pb
);
103 if (pb
.ioResult
== noErr
)
105 else if (pb
.ioResult
== dupFNErr
&& !(oflag
& F_EXCL
))
115 PBOpenRFSync((ParmBlkPtr
)&pb
);
119 PBDeleteSync((ParmBlkPtr
)&pb
);
122 fp
->refnum
= pb
.ioRefNum
;
124 /* get/set file length */
127 PBSetEOFSync((ParmBlkPtr
)&pb
);
128 else if (!(oflag
& F_CREAT
))
129 PBGetEOFSync((ParmBlkPtr
)&pb
);
130 fp
->len
= (fpos_t) pb
.ioMisc
;
132 /* initialize rest of FILE structure */
134 if (oflag
& F_APPEND
) {
138 if (oflag
& F_BINARY
)
140 setvbuf(fp
, NULL
, _IOFBF
, BUFSIZ
);
145 if (oflag
& (F_CREAT
|F_TRUNC
))
146 setfiletype(pb
.ioNamePtr
, oflag
);
150 __atexit_stdio(stdio_exit
);
156 * setfiletype - set type/creator of new file
161 setfiletype(StringPtr name
, int oflag
)
169 if (PBGetFInfoSync((ParmBlkPtr
)&pb
) == noErr
) {
170 if (oflag
& F_BINARY
)
171 pb
.ioFlFndrInfo
.fdType
= _ftype
;
173 pb
.ioFlFndrInfo
.fdType
= 'TEXT';
174 pb
.ioFlFndrInfo
.fdCreator
= _fcreator
;
175 PBSetFInfoSync((ParmBlkPtr
)&pb
);
181 * stdio_exit - stdio shutdown routine
191 for (fp
= &__file
[0], n
= FOPEN_MAX
; n
--; fp
++)
197 * fileio - I/O handler proc for files and devices
202 fileio(FILE *fp
, int i
)
206 pb
.ioRefNum
= fp
->refnum
;
212 pb
.ioBuffer
= (Ptr
) fp
->ptr
;
213 pb
.ioReqCount
= fp
->cnt
;
214 pb
.ioPosMode
= fp
->refnum
> 0 ? fsFromStart
: fsAtMark
;
215 pb
.ioPosOffset
= fp
->pos
- fp
->cnt
;
216 PBReadSync((ParmBlkPtr
)&pb
);
217 if (pb
.ioResult
== eofErr
) {
218 fp
->pos
= pb
.ioPosOffset
;
219 if (fp
->cnt
= pb
.ioActCount
)
226 if (!pb
.ioResult
&& !fp
->binary
)
227 replace(fp
->ptr
, fp
->cnt
, '\r', '\n');
233 pb
.ioBuffer
= (Ptr
) fp
->ptr
;
234 pb
.ioReqCount
= fp
->cnt
;
235 pb
.ioPosMode
= fp
->refnum
> 0 ? fsFromStart
: fsAtMark
;
236 if ((pb
.ioPosOffset
= fp
->pos
- fp
->cnt
) > fp
->len
) {
237 pb
.ioMisc
= (Ptr
) pb
.ioPosOffset
;
238 if (PBSetEOFSync((ParmBlkPtr
)&pb
) != noErr
)
242 replace(fp
->ptr
, fp
->cnt
, '\n', '\r');
243 PBWriteSync((ParmBlkPtr
)&pb
);
244 if (!pb
.ioResult
&& pb
.ioPosOffset
> fp
->len
)
245 fp
->len
= pb
.ioPosOffset
;
251 pb
.ioResult
= close(fp
);
275 register char *fcb
= FCBSPtr
+ fp
->refnum
;
276 VCB
*vcb
= fcbVPtr(fcb
);
278 enum { none
, MFS
, HFS
} del
= none
;
280 pb
.ioVRefNum
= vcb
->vcbVRefNum
;
285 /* close temporary file - HFS */
287 if (vcb
->vcbSigWord
== 0x4244) {
288 pb
.ioDirID
= fcbDirID(fcb
);
290 memcpy(buf
, s
, Length(s
) + 1);
294 /* close temporary file - MFS */
296 else if (vcb
->vcbSigWord
== 0xD2D7) {
297 for (pb
.ioFDirIndex
= 1; PBGetFInfoSync((ParmBlkPtr
)&pb
) == noErr
; pb
.ioFDirIndex
++) {
298 if (pb
.ioFRefNum
== fp
->refnum
) {
306 /* close file and flush volume buffer */
308 pb
.ioFRefNum
= fp
->refnum
;
309 if (PBCloseSync((ParmBlkPtr
)&pb
) == noErr
) {
311 PBDeleteSync((ParmBlkPtr
)&pb
);
313 PBHDeleteSync((HParmBlkPtr
)&pb
);
315 PBFlushVolSync((ParmBlkPtr
)&pb
);
322 * replace - routine for doing CR/LF conversion
327 replace(register unsigned char *s
, register size_t n
, register int c1
, register int c2
)
329 #pragma options(honor_register)
330 register unsigned char *t
;
332 for (; n
&& (t
= memchr(s
, c1
, n
)); s
= t
) {