Kaydet (Commit) 437a0e60 authored tarafından Guido van Rossum's avatar Guido van Rossum

Think 6.0 version

üst d7047b39
/* /*
* fopenRF.c -- Clone of fopen.c to open Mac resource forks. * fopen.c
* *
* Copyright (c) 1989 Symantec Corporation. All rights reserved. * Copyright (c) 1991 Symantec Corporation. All rights reserved.
* *
*/ */
#include <MacHeaders>
#include "stdio.h" #include "stdio.h"
#include "errno.h" #include "errno.h"
#include "string.h" #include "string.h"
#include "ansi_private.h" #include "ansi_private.h"
FILE *fopenRF(char *, char *); extern long _ftype, _fcreator;
FILE *freopenRF(char *, char *, FILE *);
FILE *__openRF(char *, int, int, FILE *);
#include <Files.h>
#define fcbVPtr(fcb) (* (VCB **) (fcb + 20)) #define fcbVPtr(fcb) (* (VCB **) (fcb + 20))
#define fcbDirID(fcb) (* (long *) (fcb + 58)) #define fcbDirID(fcb) (* (long *) (fcb + 58))
...@@ -27,20 +25,18 @@ static int fileio(FILE *, int); ...@@ -27,20 +25,18 @@ static int fileio(FILE *, int);
static int close(FILE *); static int close(FILE *);
static void replace(unsigned char *, size_t, int, int); static void replace(unsigned char *, size_t, int, int);
FILE *freopenRF();
FILE *__openRF();
FILE * FILE *
fopenRF(filename, mode) fopenRF(const char *filename, const char *mode)
char *filename, *mode;
{ {
return(freopenRF(filename, mode, __getfile())); return(freopenRF(filename, mode, __getfile()));
} }
FILE * FILE *
freopenRF(filename, mode, fp) freopenRF(const char *filename, const char *mode, FILE *fp)
char *filename;
register char *mode;
register FILE *fp;
{ {
int omode, oflag; int omode, oflag;
...@@ -83,12 +79,9 @@ register FILE *fp; ...@@ -83,12 +79,9 @@ register FILE *fp;
FILE * FILE *
__openRF(filename, omode, oflag, fp) __openRF(const char *filename, int omode, int oflag, FILE *fp)
char *filename;
int omode, oflag;
register FILE *fp;
{ {
ioParam pb; IOParam pb;
char pname[FILENAME_MAX]; char pname[FILENAME_MAX];
if (fp == NULL) if (fp == NULL)
...@@ -106,10 +99,7 @@ register FILE *fp; ...@@ -106,10 +99,7 @@ register FILE *fp;
/* create file */ /* create file */
if (oflag & F_CREAT) { if (oflag & F_CREAT) {
asm { PBCreateSync((ParmBlkPtr)&pb);
lea pb,a0
_PBCreate
}
if (pb.ioResult == noErr) if (pb.ioResult == noErr)
oflag &= ~F_TRUNC; oflag &= ~F_TRUNC;
else if (pb.ioResult == dupFNErr && !(oflag & F_EXCL)) else if (pb.ioResult == dupFNErr && !(oflag & F_EXCL))
...@@ -120,32 +110,23 @@ register FILE *fp; ...@@ -120,32 +110,23 @@ register FILE *fp;
} }
} }
/* open resource file */ /* open file */
asm { PBOpenRFSync((ParmBlkPtr)&pb);
lea pb,a0
_PBOpenRF
}
if (pb.ioResult) { if (pb.ioResult) {
errno = pb.ioResult; errno = pb.ioResult;
if (oflag & F_CREAT) asm { if (oflag & F_CREAT)
lea pb,a0 PBDeleteSync((ParmBlkPtr)&pb);
_PBDelete
}
return(NULL); return(NULL);
} }
fp->refnum = pb.ioRefNum; fp->refnum = pb.ioRefNum;
/* get/set file length */ /* get/set file length */
if (oflag & F_TRUNC) asm { if (oflag & F_TRUNC)
lea pb,a0 PBSetEOFSync((ParmBlkPtr)&pb);
_PBSetEOF else if (!(oflag & F_CREAT))
} PBGetEOFSync((ParmBlkPtr)&pb);
else if (!(oflag & F_CREAT)) asm {
lea pb,a0
_PBGetEOF
}
fp->len = (fpos_t) pb.ioMisc; fp->len = (fpos_t) pb.ioMisc;
/* initialize rest of FILE structure */ /* initialize rest of FILE structure */
...@@ -177,28 +158,22 @@ register FILE *fp; ...@@ -177,28 +158,22 @@ register FILE *fp;
*/ */
static void static void
setfiletype(name, oflag) setfiletype(StringPtr name, int oflag)
StringPtr name;
int oflag;
{ {
fileParam pb; FileParam pb;
pb.ioNamePtr = name; pb.ioNamePtr = name;
pb.ioVRefNum = 0; pb.ioVRefNum = 0;
pb.ioFVersNum = 0; pb.ioFVersNum = 0;
pb.ioFDirIndex = 0; pb.ioFDirIndex = 0;
asm { if (PBGetFInfoSync((ParmBlkPtr)&pb) == noErr) {
lea pb,a0 if (oflag & F_BINARY)
_PBGetFInfo pb.ioFlFndrInfo.fdType = _ftype;
bmi.s @1 else
pb.ioFlFndrInfo.fdType = 'TEXT';
pb.ioFlFndrInfo.fdCreator = _fcreator;
PBSetFInfoSync((ParmBlkPtr)&pb);
} }
pb.ioFlFndrInfo.fdType = pb.ioFlFndrInfo.fdCreator = '????';
if (!(oflag & F_BINARY))
pb.ioFlFndrInfo.fdType = 'TEXT';
asm {
lea pb,a0
_PBSetFInfo
@1 }
} }
...@@ -208,7 +183,7 @@ int oflag; ...@@ -208,7 +183,7 @@ int oflag;
*/ */
static void static void
stdio_exit() stdio_exit(void)
{ {
register FILE *fp; register FILE *fp;
int n; int n;
...@@ -224,11 +199,9 @@ stdio_exit() ...@@ -224,11 +199,9 @@ stdio_exit()
*/ */
static int static int
fileio(fp, i) fileio(FILE *fp, int i)
register FILE *fp;
int i;
{ {
ioParam pb; IOParam pb;
pb.ioRefNum = fp->refnum; pb.ioRefNum = fp->refnum;
switch (i) { switch (i) {
...@@ -240,10 +213,7 @@ int i; ...@@ -240,10 +213,7 @@ int i;
pb.ioReqCount = fp->cnt; pb.ioReqCount = fp->cnt;
pb.ioPosMode = fp->refnum > 0 ? fsFromStart : fsAtMark; pb.ioPosMode = fp->refnum > 0 ? fsFromStart : fsAtMark;
pb.ioPosOffset = fp->pos - fp->cnt; pb.ioPosOffset = fp->pos - fp->cnt;
asm { PBReadSync((ParmBlkPtr)&pb);
lea pb,a0
_PBRead
}
if (pb.ioResult == eofErr) { if (pb.ioResult == eofErr) {
fp->pos = pb.ioPosOffset; fp->pos = pb.ioPosOffset;
if (fp->cnt = pb.ioActCount) if (fp->cnt = pb.ioActCount)
...@@ -253,11 +223,7 @@ int i; ...@@ -253,11 +223,7 @@ int i;
return(EOF); return(EOF);
} }
} }
if (pb.ioResult) { if (!pb.ioResult && !fp->binary)
fp->pos -= fp->cnt;
fp->cnt = 0;
}
else if (!fp->binary)
replace(fp->ptr, fp->cnt, '\r', '\n'); replace(fp->ptr, fp->cnt, '\r', '\n');
break; break;
...@@ -269,23 +235,13 @@ int i; ...@@ -269,23 +235,13 @@ int i;
pb.ioPosMode = fp->refnum > 0 ? fsFromStart : fsAtMark; pb.ioPosMode = fp->refnum > 0 ? fsFromStart : fsAtMark;
if ((pb.ioPosOffset = fp->pos - fp->cnt) > fp->len) { if ((pb.ioPosOffset = fp->pos - fp->cnt) > fp->len) {
pb.ioMisc = (Ptr) pb.ioPosOffset; pb.ioMisc = (Ptr) pb.ioPosOffset;
asm { if (PBSetEOFSync((ParmBlkPtr)&pb) != noErr)
lea pb,a0 break;
_PBSetEOF
bmi.s @1
}
} }
if (!fp->binary) if (!fp->binary)
replace(fp->ptr, fp->cnt, '\n', '\r'); replace(fp->ptr, fp->cnt, '\n', '\r');
asm { PBWriteSync((ParmBlkPtr)&pb);
lea pb,a0 if (!pb.ioResult && pb.ioPosOffset > fp->len)
_PBWrite
@1 }
if (pb.ioResult) {
fp->pos -= fp->cnt;
fp->cnt = 0;
}
else if (pb.ioPosOffset > fp->len)
fp->len = pb.ioPosOffset; fp->len = pb.ioPosOffset;
break; break;
...@@ -299,6 +255,10 @@ int i; ...@@ -299,6 +255,10 @@ int i;
/* done */ /* done */
if (pb.ioResult) { if (pb.ioResult) {
if (i < 2) {
fp->pos -= fp->cnt;
fp->cnt = 0;
}
fp->err = 1; fp->err = 1;
errno = pb.ioResult; errno = pb.ioResult;
return(EOF); return(EOF);
...@@ -308,73 +268,52 @@ int i; ...@@ -308,73 +268,52 @@ int i;
static int static int
close(fp) close(FILE *fp)
register FILE *fp;
{ {
HFileParam pb; HFileParam pb;
Str255 buf; Str255 buf;
register char *fcb = FCBSPtr + fp->refnum; register char *fcb = FCBSPtr + fp->refnum;
VCB *vcb = fcbVPtr(fcb); VCB *vcb = fcbVPtr(fcb);
register char *s; register char *s;
enum { none, MFS, HFS } del = none;
pb.ioNamePtr = buf;
pb.ioFRefNum = fp->refnum;
pb.ioVRefNum = vcb->vcbVRefNum; pb.ioVRefNum = vcb->vcbVRefNum;
pb.ioFVersNum = 0; if (fp->remove) {
pb.ioNamePtr = buf;
pb.ioFVersNum = 0;
/* close temporary file - HFS */ /* close temporary file - HFS */
if (fp->delete && vcb->vcbSigWord == 0x4244) { if (vcb->vcbSigWord == 0x4244) {
pb.ioDirID = fcbDirID(fcb); pb.ioDirID = fcbDirID(fcb);
s = fcbCName(fcb); s = fcbCName(fcb);
asm { memcpy(buf, s, Length(s) + 1);
lea buf,a0 del = HFS;
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) { /* close temporary file - MFS */
pb.ioFDirIndex = 1;
do asm { else if (vcb->vcbSigWord == 0xD2D7) {
lea pb,a0 for (pb.ioFDirIndex = 1; PBGetFInfoSync((ParmBlkPtr)&pb) == noErr; pb.ioFDirIndex++) {
_PBGetFInfo if (pb.ioFRefNum == fp->refnum) {
bmi.s @2 del = MFS;
addq.w #1,pb.ioFDirIndex break;
} while (pb.ioFRefNum != fp->refnum); }
asm { }
lea pb,a0
_PBClose
bmi.s @9
_PBDelete
} }
} }
/* normal case - just close file */ /* close file and flush volume buffer */
else {
asm {
@2 lea pb,a0
_PBClose
bmi.s @9
}
}
/* flush volume buffer */ pb.ioFRefNum = fp->refnum;
if (PBCloseSync((ParmBlkPtr)&pb) == noErr) {
pb.ioNamePtr = 0; if (del == MFS)
asm { PBDeleteSync((ParmBlkPtr)&pb);
lea pb,a0 else if (del == HFS)
_PBFlshVol PBHDeleteSync((HParmBlkPtr)&pb);
@9 } pb.ioNamePtr = 0;
PBFlushVolSync((ParmBlkPtr)&pb);
}
return(pb.ioResult); return(pb.ioResult);
} }
...@@ -385,11 +324,9 @@ register FILE *fp; ...@@ -385,11 +324,9 @@ register FILE *fp;
*/ */
static void static void
replace(s, n, c1, c2) replace(register unsigned char *s, register size_t n, register int c1, register int c2)
register unsigned char *s;
register size_t n;
register int c1, c2;
{ {
#pragma options(honor_register)
register unsigned char *t; register unsigned char *t;
for (; n && (t = memchr(s, c1, n)); s = t) { for (; n && (t = memchr(s, c1, n)); s = t) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment