1989-05-30 13:34:25 +00:00
|
|
|
/*
|
|
|
|
* fopen.c - open a stream
|
|
|
|
*/
|
|
|
|
/* $Header$ */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "loc_incl.h"
|
|
|
|
|
|
|
|
#define PMODE 0666
|
|
|
|
|
1989-12-18 15:04:14 +00:00
|
|
|
/* The next 3 defines are true in all UNIX systems known to me.
|
|
|
|
*/
|
|
|
|
#define O_RDONLY 0
|
|
|
|
#define O_WRONLY 1
|
|
|
|
#define O_RDWR 2
|
|
|
|
|
|
|
|
/* Since the O_CREAT flag is not available on all systems, we can't get it
|
|
|
|
* from the standard library. Furthermore, even if we know that <fcntl.h>
|
|
|
|
* contains such a flag, it's not sure whether it can be used, since we
|
|
|
|
* might be cross-compiling for another system, which may use an entirely
|
|
|
|
* different value for O_CREAT (or not support such a mode). The safest
|
|
|
|
* thing is to just use the Version 7 semantics for open, and use creat()
|
|
|
|
* whenever necessary.
|
|
|
|
*
|
|
|
|
* Another problem is O_APPEND, for which the same holds. When "a"
|
|
|
|
* open-mode is used, an lseek() to the end is done before every write()
|
|
|
|
* system-call.
|
|
|
|
*
|
|
|
|
* The O_CREAT, O_TRUNC and O_APPEND given here, are only for convenience.
|
|
|
|
* They are not passed to open(), so the values don't have to match a value
|
|
|
|
* from the real world. It is enough when they are unique.
|
|
|
|
*/
|
|
|
|
#define O_CREAT 0x010
|
|
|
|
#define O_TRUNC 0x020
|
|
|
|
#define O_APPEND 0x040
|
|
|
|
|
1990-01-22 11:13:26 +00:00
|
|
|
int _open(const char *path, int flags);
|
|
|
|
int _creat(const char *path, int mode);
|
1989-12-18 15:04:14 +00:00
|
|
|
|
1990-01-22 11:13:26 +00:00
|
|
|
int _close(int d);
|
1989-05-30 13:34:25 +00:00
|
|
|
|
|
|
|
FILE *
|
|
|
|
fopen(const char *name, const char *mode)
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
int rwmode = 0, rwflags = 0;
|
|
|
|
FILE *stream;
|
|
|
|
int fd, flags = 0;
|
|
|
|
|
1989-12-18 15:04:14 +00:00
|
|
|
for (i = 0; __iotab[i] != 0 ; i++)
|
1989-05-30 13:34:25 +00:00
|
|
|
if ( i >= FOPEN_MAX )
|
|
|
|
return (FILE *)NULL;
|
|
|
|
|
|
|
|
switch(*mode++) {
|
|
|
|
case 'r':
|
1989-06-26 10:37:05 +00:00
|
|
|
flags |= _IOREAD | _IOREADING;
|
1989-05-30 13:34:25 +00:00
|
|
|
rwmode = O_RDONLY;
|
|
|
|
break;
|
|
|
|
case 'w':
|
1989-06-26 10:37:05 +00:00
|
|
|
flags |= _IOWRITE | _IOWRITING;
|
1989-05-30 13:34:25 +00:00
|
|
|
rwmode = O_WRONLY;
|
|
|
|
rwflags = O_CREAT | O_TRUNC;
|
|
|
|
break;
|
|
|
|
case 'a':
|
1989-12-18 15:04:14 +00:00
|
|
|
flags |= _IOWRITE | _IOWRITING | _IOAPPEND;
|
1989-05-30 13:34:25 +00:00
|
|
|
rwmode = O_WRONLY;
|
|
|
|
rwflags |= O_APPEND | O_CREAT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (FILE *)NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*mode) {
|
|
|
|
switch(*mode++) {
|
|
|
|
case 'b':
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
rwmode = O_RDWR;
|
|
|
|
flags |= _IOREAD | _IOWRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (FILE *)NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1989-12-18 15:04:14 +00:00
|
|
|
/* Perform a creat() when the file should be truncated or when
|
|
|
|
* the file is opened for writing and the open() failed.
|
|
|
|
*/
|
|
|
|
if ((rwflags & O_TRUNC)
|
1990-01-22 11:13:26 +00:00
|
|
|
|| (((fd = _open(name, rwmode)) < 0)
|
1989-12-18 15:04:14 +00:00
|
|
|
&& (flags & _IOWRITE)))
|
1990-01-22 11:13:26 +00:00
|
|
|
fd = _creat(name, PMODE);
|
1989-05-30 13:34:25 +00:00
|
|
|
|
|
|
|
if (fd < 0) return (FILE *)NULL;
|
|
|
|
|
|
|
|
if (( stream = (FILE *) malloc(sizeof(FILE))) == NULL ) {
|
1990-01-22 11:13:26 +00:00
|
|
|
_close(fd);
|
1989-05-30 13:34:25 +00:00
|
|
|
return (FILE *)NULL;
|
|
|
|
}
|
|
|
|
|
1989-12-18 15:04:14 +00:00
|
|
|
if ((flags & (_IOREAD | _IOWRITE)) == (_IOREAD | _IOWRITE))
|
1989-06-26 10:37:05 +00:00
|
|
|
flags &= ~(_IOREADING | _IOWRITING);
|
|
|
|
|
1989-05-30 13:34:25 +00:00
|
|
|
stream->_count = 0;
|
|
|
|
stream->_fd = fd;
|
|
|
|
stream->_flags = flags;
|
1989-06-26 10:37:05 +00:00
|
|
|
stream->_buf = NULL;
|
1989-12-18 15:04:14 +00:00
|
|
|
__iotab[i] = stream;
|
1989-05-30 13:34:25 +00:00
|
|
|
return stream;
|
|
|
|
}
|