int ftruncate(short int file,int offset) { ParamBlockRec pb; OSErr error; //JMM Foo FSSetForkSize FSSetForkPosition FSGetForkPosition pb.ioParam.ioRefNum = file; pb.ioParam.ioMisc = (char *) offset; error = PBSetEOFSync(&pb); return error; }
int __open_file(const char * name, __file_modes mode, __file_handle * handle) { FSSpec spec; OSErr ioResult; HParamBlockRec pb; ioResult = __path2fss(name, &spec); if (ioResult) gSqueakFileLastError = ioResult; if (__system7present()) /* mm 980424 */ { /* mm 980424 */ Boolean targetIsFolder, wasAliased; /* mm 980424 */ ResolveAliasFile(&spec, true, &targetIsFolder, &wasAliased); /* mm 980424 */ } /* mm 980424 */ if (ioResult && (ioResult != fnfErr || mode.open_mode == __must_exist)) return(__io_error); #if TARGET_API_MAC_CARBON if (ioResult) { CFStringRef filePath; CFURLRef sillyThing, sillyThing2; FSRef parentFSRef; short int fileRefNum; OSErr err; UniChar buffer[1024]; long tokenLength; filePath = CFStringCreateWithBytes(kCFAllocatorDefault,(UInt8 *)name,strlen(name),gCurrentVMEncoding,false); if (filePath == nil) return __io_error; sillyThing = CFURLCreateWithFileSystemPath (kCFAllocatorDefault,filePath,kCFURLHFSPathStyle,false); CFRelease(filePath); sillyThing2 = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault,sillyThing); err = CFURLGetFSRef(sillyThing2,&parentFSRef); if (err == 0) { CFRelease(sillyThing); CFRelease(sillyThing2); return fnfErr; } filePath = CFURLCopyLastPathComponent(sillyThing); tokenLength = CFStringGetLength(filePath); if (tokenLength > 1024) { CFRelease(filePath); CFRelease(sillyThing); CFRelease(sillyThing2); return(__io_error); } CFStringGetCharacters(filePath,CFRangeMake(0,tokenLength),buffer); CFRelease(filePath); CFRelease(sillyThing); CFRelease(sillyThing2); ioResult = FSCreateFileUnicode(&parentFSRef,tokenLength,buffer,kFSCatInfoNone,NULL,NULL,&spec); if (ioResult) gSqueakFileLastError = ioResult; if (ioResult) return(__io_error); pb.ioParam.ioNamePtr = spec.name; pb.ioParam.ioVRefNum = spec.vRefNum; pb.ioParam.ioPermssn = (mode.io_mode == __read) ? fsRdPerm : fsRdWrPerm; pb.ioParam.ioMisc = 0; pb.fileParam.ioFVersNum = 0; pb.fileParam.ioDirID = spec.parID; set_file_type(&spec, mode.binary_io); ioResult = PBHOpenDFSync(&pb); /* HH 10/25/97 was PBHOpenSync */ if (ioResult) return(__io_error); *handle = pb.ioParam.ioRefNum; return(__no_io_error); } #endif pb.ioParam.ioNamePtr = spec.name; pb.ioParam.ioVRefNum = spec.vRefNum; pb.ioParam.ioPermssn = (mode.io_mode == __read) ? fsRdPerm : fsRdWrPerm; pb.ioParam.ioMisc = 0; pb.fileParam.ioFVersNum = 0; pb.fileParam.ioDirID = spec.parID; if (ioResult) { if (!(ioResult = PBHCreateSync(&pb))) { if (ioResult) gSqueakFileLastError = ioResult; set_file_type(&spec, mode.binary_io); ioResult = PBHOpenDFSync(&pb); /* HH 10/25/97 was PBHOpenSync */ if (ioResult) gSqueakFileLastError = ioResult; } } else { if (!(ioResult = PBHOpenDFSync(&pb)) && mode.open_mode == __create_or_truncate) /* HH 10/25/97 was PBHOpenSync */ { pb.ioParam.ioMisc = 0; ioResult = PBSetEOFSync((ParmBlkPtr) &pb); if (ioResult) gSqueakFileLastError = ioResult; if (ioResult) PBCloseSync((ParmBlkPtr) &pb); } else { if (ioResult) gSqueakFileLastError = ioResult; } } if (ioResult) return(__io_error); *handle = pb.ioParam.ioRefNum; return(__no_io_error); }
/* * int open(const char *path, int oflag) * * Opens a file stream. */ int my_open(char *path, int oflag) { FSSpec spec; char permission; HParamBlockRec hpb; OSErr err, errno; Boolean targetIsFolder, wasAliased; AssertStr(path,path) /* Setup permission */ if ((oflag & 0x03) == O_RDWR) permission = fsRdWrPerm; else permission = (oflag & O_RDONLY) ? fsRdPerm : 0 + (oflag & O_WRONLY) ? fsWrPerm : 0; FSpLocationFromFullPath(strlen(path),path, &spec); if ((oflag & (O_ALIAS | O_NRESOLVE)) == 0) ResolveAliasFile(&spec, true, &targetIsFolder, &wasAliased); hpb.fileParam.ioNamePtr = spec.name; hpb.fileParam.ioVRefNum = spec.vRefNum; hpb.fileParam.ioDirID = spec.parID; hpb.ioParam.ioPermssn = permission; if (oflag & O_RSRC) /* open the resource fork of the file */ err = PBHOpenRFSync(&hpb); else /* open the data fork of the file */ err = PBHOpenDFSync(&hpb); if ((err == fnfErr) && (oflag & O_CREAT)) { hpb.fileParam.ioFlVersNum = 0; err = PBHCreateSync(&hpb); if (err == noErr) { /* Set the finder info */ unsigned long secs; unsigned long isbinary = oflag & O_BINARY; hpb.fileParam.ioFlFndrInfo.fdType = '\?\?\?\?'; hpb.fileParam.ioFlFndrInfo.fdCreator = '\?\?\?\?'; hpb.fileParam.ioFlFndrInfo.fdFlags = 0; if (oflag & O_ALIAS) /* set the alias bit */ hpb.fileParam.ioFlFndrInfo.fdFlags = kIsAlias; else /* clear all flags */ hpb.fileParam.ioFlFndrInfo.fdFlags = 0; GetDateTime(&secs); hpb.fileParam.ioFlCrDat = hpb.fileParam.ioFlMdDat = secs; PBHSetFInfoSync(&hpb); } if (err && (err != dupFNErr)) { errno = err; return -1; } if (oflag & O_RSRC) /* open the resource fork of the file */ err = PBHOpenRFSync(&hpb); else /* open the data fork of the file */ err = PBHOpenDFSync(&hpb); } if (err && (err != dupFNErr) && (err != opWrErr)) { errno = err; return -1; } if (oflag & O_TRUNC) { IOParam pb; pb.ioRefNum = hpb.ioParam.ioRefNum; pb.ioMisc = 0L; err = PBSetEOFSync((ParmBlkPtr)&pb); if (err != noErr) { errno = err; return -1; } } if (oflag & O_APPEND) lseek(hpb.ioParam.ioRefNum,0,SEEK_END); return (hpb.ioParam.ioRefNum); }