?? open.c
字號:
#pragma data_seg()
/* Mac definitions for ioDenyModes */
#define MAC_PERMRD 0x0001
#define MAC_PERMWR 0x0002
#define MAC_DENYRD 0x0010
#define MAC_DENYWR 0x0020
/***
*int _open(path, flag, pmode) - open or create a file
*
*Purpose:
* Opens the file and prepares for subsequent reading or writing.
* the flag argument specifies how to open the file:
* _O_APPEND - reposition file ptr to end before every write
* _O_BINARY - open in binary mode
* _O_CREAT - create a new file* no effect if file already exists
* _O_EXCL - return error if file exists, only use with O_CREAT
* _O_RDONLY - open for reading only
* _O_RDWR - open for reading and writing
* _O_TEXT - open in text mode
* _O_TRUNC - open and truncate to 0 length (must have write permission)
* _O_WRONLY - open for writing only
* exactly one of _O_RDONLY, _O_WRONLY, _O_RDWR must be given
*
* The pmode argument is only required when _O_CREAT is specified. Its
* flag settings:
* _S_IWRITE - writing permitted
* _S_IREAD - reading permitted
* _S_IREAD | _S_IWRITE - both reading and writing permitted
* The current file-permission masks is applied to pmode before
* setting the permission (see umask).
*
* Note, the _creat() function also uses this function but setting up the
* correct arguments and calling _open().
*
*Entry:
* char *path - file name
* int flag - flags for _open()
* int pmode - permission mode for new files
*
*Exit:
* returns file handle of open file if successful
* returns -1 (and sets errno) if fails
*
*Exceptions:
*
*******************************************************************************/
int __cdecl _open (
const char *path,
int oflag,
...
)
{
va_list ap;
va_start(ap, oflag);
/* default sharing mode is DENY NONE */
return _sopen(path, oflag, _SH_DENYNO, va_arg(ap, int));
}
/***
*void __mopen(stpath, fh, ioPermssn, ioDenyModes) - MAC open a file with sharing
*
*Purpose:
* Worker routine to open a file on the MAC. It only opens the file.
* If local open fails it will try AppleShare oepn.
*
*Entry:
* char *stpath - file to open (Pascal string)
* int fh - file handle to use
* int ioPermssn - persmission modes flags
* int ioDenyModes - Deny mode flags
*
*Exit:
* returns TRUE if successful and sets errno & _osfhnd[fh] &
* _osfile[fh] if successful
*
*Exceptions:
*
*******************************************************************************/
int __cdecl __mopen (
char *stpath,
int fh,
int ioPermssn,
int ioDenyModes
)
{
HParamBlockRec parm;
OSErr osErr;
char sz[256];
char buf[10];
char *pch;
//if starts with full path, test the availability of the volume
memcpy(sz, stpath, (*stpath+1));
_p2cstr(sz);
if (*sz != ':' && (pch = strchr(sz, ':')) != NULL)
{
*(pch+1) = '\0';
_c2pstr(sz);
memset(&parm, 0, sizeof(HParamBlockRec));
memset(buf, 0, 10);
parm.ioParam.ioNamePtr = sz;
parm.ioParam.ioBuffer = buf;
parm.ioParam.ioReqCount = 6;
osErr = PBHGetVolParmsSync(&parm);
if (!osErr)
{
if (!(buf[4]&0x8000))
{
parm.ioParam.ioNamePtr = stpath;
parm.ioParam.ioVRefNum = 0;
goto local;
}
}
else
{
_dosmaperr(osErr);
return osErr;
}
}
/* try to open the file using Appleshare calls*/
parm.ioParam.ioNamePtr = stpath;
parm.ioParam.ioVRefNum = 0;
parm.accessParam.ioDenyModes = (unsigned char)ioDenyModes;
_osperm[fh] = (unsigned char)ioDenyModes;
parm.fileParam.ioDirID = 0;
parm.ioParam.ioMisc = NULL;
osErr = PBHOpenDenySync(&parm);
if (osErr == paramErr)
{
local:
/* Try local open */
parm.ioParam.ioPermssn = ioPermssn;
_osperm[fh] = (unsigned char)ioPermssn;
osErr = PBHOpenDFSync(&parm);
}
if (!osErr)
{
_osfile[fh] |= FOPEN;
_osfhnd[fh] = parm.ioParam.ioRefNum;
}
else
{
_dosmaperr(osErr);
}
return osErr;
}
/***
*int _sopen(path, oflag, shflag, pmode) - open a file with sharing
*
*Purpose:
* Opens the file with possible file sharing.
* shflag defines the sharing flags:
* _SH_DENYRW - deny read and write access to the file
* _SH_DENYNO - permit read and write access
*
* Other flags are the same as _open().
*
* SOPEN is the routine used when file sharing is desired.
*
*Entry:
* char *path - file to open (C string)
* int oflag - open flag
* int shflag - sharing flag
* int pmode - permission mode (needed only when creating file)
*
*Exit:
* returns file handle for the opened file
* returns -1 and sets errno if fails.
*
*Exceptions:
*
*******************************************************************************/
int _cdecl _sopen (
const char *path,
int oflag,
int shflag,
...
)
{
int fh; /* handle of opened file */
OSErr osErr = 0;
unsigned char ioPermssn;
short int ioDenyModes;
ParamBlockRec parm;
int pmode;
va_list ap; /* variable argument (pmode) */
char lpath[256];
if (!*path)
{
errno = ENOENT;
return -1;
}
strcpy(lpath,path);
_c2pstr(lpath);
/* get a file handle*/
for (fh = 0; fh <_nfile; fh++)
{
if (!(_osfile[fh] & FOPEN))
{
break;
}
}
if (fh >= _nfile)
{
errno = EMFILE;
_macerrno = 0;
return -1;
}
_osfile[fh] = 0;
_osfileflags[fh] = 0;
/* figure out binary/text mode */
switch (oflag & (_O_BINARY | _O_TEXT))
{
case _O_BINARY:
break;
case _O_TEXT:
_osfile[fh] = (unsigned char)FTEXT;
break;
case _O_TEXT | _O_BINARY:
errno = EINVAL;
return -1;
default:
if (_fmode != _O_BINARY)
{
_osfile[fh] = (unsigned char)FTEXT;
}
break;
}
/* figure out read/write modes */
switch (oflag & (_O_RDWR | _O_RDONLY | _O_WRONLY))
{
case _O_RDONLY:
ioPermssn = fsRdPerm;
_osfile[fh] |= FRDONLY;
if (oflag & _O_TRUNC)
{
errno = EINVAL;
return -1;
}
ioDenyModes = MAC_PERMRD;
break;
case _O_WRONLY:
ioPermssn = fsRdWrShPerm;
_osfile[fh] |= FWRONLY;
ioDenyModes = MAC_PERMWR;
break;
case _O_RDWR:
ioPermssn = fsRdWrPerm;
ioDenyModes = MAC_PERMRD | MAC_PERMWR;
break;
default:
errno = EINVAL;
return -1;
}
switch (shflag)
{
case _SH_DENYRD:
ioDenyModes |= MAC_DENYRD;
break;
case _SH_DENYWR:
ioDenyModes |= MAC_DENYWR;
break;
case _SH_DENYRW:
ioDenyModes |= MAC_DENYRD | MAC_DENYWR;
break;
case _SH_DENYNO:
if (ioPermssn == fsRdWrPerm)
{
ioPermssn = fsRdWrShPerm;
}
break;
default:
errno = EINVAL;
return -1;
}
if (!(oflag & _O_CREAT && oflag & _O_EXCL))
{
/* try to open the file */
if (!__mopen(lpath, fh, ioPermssn, ioDenyModes))
{
oflag &= ~_O_CREAT; /*file open - no need to create*/
}
}
/* Didn't work try creating the file if requested */
if (oflag & _O_CREAT)
{
/* reset errno from mopen, since we can try create*/
errno = 0;
va_start(ap, shflag);
pmode = va_arg(ap, int);
pmode &= ~_umaskval;
if (!(pmode & (_S_IREAD | _S_IWRITE)))
{
errno = EINVAL;
return -1;
}
parm.fileParam.ioNamePtr = lpath;
parm.fileParam.ioVRefNum = 0;
osErr = PBCreateSync(&parm);
if (!osErr)
{
parm.fileParam.ioFDirIndex = 0;
PBGetFInfoSync(&parm);
parm.fileParam.ioFlFndrInfo.fdType = (_osfile[fh] & FTEXT ? 'TEXT' : ' ');
parm.fileParam.ioFlFndrInfo.fdCreator = ' ';
PBSetFInfoSync(&parm);
}
else
{
if (osErr == dupFNErr && oflag & _O_EXCL)
{
errno = EEXIST; /*special case normally returns EACCES*/
_macerrno = osErr;
}
else
{
_dosmaperr(osErr);
}
return -1;
}
if (osErr = __mopen(lpath, fh, ioPermssn, ioDenyModes))
{
_dosmaperr(osErr);
return -1;
}
else if (!(pmode & _S_IWRITE))
{
PBSetFLockSync(&parm);
}
}
if (!(_osfile[fh] & FOPEN))
{
goto ErrExit;
}
parm.ioParam.ioRefNum = _osfhnd[fh];
/* Truncate file */
if (oflag & _O_TRUNC)
{
parm.ioParam.ioMisc = 0;
osErr = PBSetEOFSync(&parm);
if (osErr)
{
_dosmaperr(osErr);
goto ErrExit;
}
}
/* get vol reference */
parm.volumeParam.ioVolIndex = -1;
parm.ioParam.ioNamePtr = lpath;
parm.ioParam.ioVRefNum = 0;
osErr = PBGetVInfoSync(&parm);
if (osErr)
{
_dosmaperr(osErr);
goto ErrExit;
}
_osVRefNum[fh] = parm.volumeParam.ioVRefNum;
if (oflag & _O_APPEND)
{
_osfile[fh] |= FAPPEND;
}
if (oflag & _O_TEMPORARY)
{
_osfileflags[fh] |= FTEMP;
}
return fh; /* return handle */
ErrExit:
if (_osfile[fh] & FOPEN)
{
PBCloseSync(&parm);
_osfile[fh] = 0;
}
return -1;
}
#endif /* _MAC */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -