Exemple #1
0
pascal	OSErr	FSpOpenRFCompat(const FSSpec *spec,
                                char permission,
                                short *refNum)
{
#if !__MACOSSEVENORLATER
    if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
    {
        OSErr			result;
        HParamBlockRec	pb;

        pb.ioParam.ioVRefNum = spec->vRefNum;
        pb.fileParam.ioDirID = spec->parID;
        pb.ioParam.ioNamePtr = (StringPtr) &(spec->name);
        pb.ioParam.ioVersNum = 0;
        pb.ioParam.ioPermssn = permission;
        pb.ioParam.ioMisc = NULL;
        result = PBHOpenRFSync(&pb);
        *refNum = pb.ioParam.ioRefNum;
        return ( result );
    }
    else
#endif	/* !__MACOSSEVENORLATER */
    {
        return ( FSpOpenRF(spec, permission, refNum) );
    }
}
/*
 *  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);
}