home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari FTP
/
ATARI_FTP_0693.zip
/
ATARI_FTP_0693
/
Mint
/
mntlib32.zoo
/
open.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-17
|
3KB
|
151 lines
/* based upon Dale Schumacher's dLibs library */
/* extensively modified by ers */
#include <compiler.h>
#include <osbind.h>
#include <mintbind.h>
#include <limits.h>
#include <fcntl.h>
#include <ioctl.h>
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include "lib.h"
/*
* the MiNT kernel uses 0x08 for O_APPEND. For
* backwards compatibility with old .o files,
* we leave the definition in fcntl.h the same,
* but adjust the file masks here.
*/
static int __umask = -1;
extern int __mint;
static void _get_umask __PROTO((void));
/*
* function to set the initial value of __umask
*/
static void
_get_umask()
{
if (__mint < 9) {
__umask = 0;
} else {
__umask = Pumask(0);
(void) Pumask(__umask);
}
}
/* NOTE: we assume here that every compiler that can handle __PROTO
* is __STDC__ and can handle the >>...<< ellipsis
* (see also unistd.h)
*/
#ifdef __STDC__
int open(const char *_filename, int iomode, ...)
#else
int open(_filename, iomode, pmode)
const char *_filename;
int iomode;
unsigned pmode;
#endif
{
int rv;
int modemask; /* which bits get passed to the OS? */
char filename[PATH_MAX];
#ifdef __STDC__
unsigned pmode;
va_list argp;
va_start(argp, iomode);
pmode = va_arg(argp, unsigned int);
va_end(argp);
#endif
_unx2dos(_filename, filename);
/* use the umask() setting to get the right permissions */
if (__umask == -1)
_get_umask();
pmode &= ~__umask;
/* set the file access modes correctly */
iomode = iomode & ~O_SHMODE;
if (__mint >= 9) {
modemask = O_ACCMODE | O_SHMODE | O_SYNC | O_NDELAY;
iomode |= (iomode & O_EXCL) ? O_DENYRW : O_DENYNONE;
if (__mint >= 96) {
modemask |= _REALO_APPEND;
if (iomode & O_APPEND)
iomode |= _REALO_APPEND;
}
} else {
modemask = O_ACCMODE;
}
if(Fattrib(filename, 0, 0) >= 0) /* file exists */
{
if((iomode & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
errno = EEXIST;
return -1;
}
if(iomode & O_TRUNC)
rv = (int)Fcreate(filename, 0x00);
else
rv = (int)Fopen(filename,iomode & modemask);
}
else /* file doesn't exist */
{
if(iomode & O_CREAT) {
rv = (int)Fcreate(filename, 0x00);
if (rv >= 0 && __mint >= 9)
(void)Fchmod(filename, pmode);
}
else
rv = -ENOENT;
}
if((rv >= 0) && (iomode & O_APPEND))
(void)lseek(rv, 0L, SEEK_END);
if(rv < (__SMALLEST_VALID_HANDLE)) {
errno = -rv;
return __SMALLEST_VALID_HANDLE - 1;
}
/* Important side effect: isatty(rv) sets up flags under TOS */
if (isatty(rv) && (!(iomode & O_NOCTTY)) && (!(isatty(-1)))) {
(void) Fforce(-1, rv); /* new controlling tty */
__open_stat[__OPEN_INDEX(-1)] = __open_stat[__OPEN_INDEX(rv)];
}
return(rv);
}
int
creat(name, mode)
const char *name;
unsigned mode;
{
return open(name, O_WRONLY|O_CREAT|O_TRUNC, mode);
}
/* umask -- change default file creation mask */
int umask(complmode)
int complmode;
{
int old_umask;
if (__umask == -1)
_get_umask();
old_umask = __umask;
__umask = complmode;
if (__mint >= 9)
return Pumask(complmode);
return old_umask;
}