1989-05-30 13:34:25 +00:00
|
|
|
/*
|
|
|
|
* fopen.c - open a stream
|
|
|
|
*/
|
|
|
|
/* $Header$ */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include "loc_incl.h"
|
|
|
|
|
|
|
|
#define PMODE 0666
|
|
|
|
|
|
|
|
int open(const char *path, int flags, int mode);
|
|
|
|
int close(int d);
|
|
|
|
|
|
|
|
FILE *
|
|
|
|
fopen(const char *name, const char *mode)
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
int rwmode = 0, rwflags = 0;
|
|
|
|
FILE *stream;
|
|
|
|
int fd, flags = 0;
|
|
|
|
|
|
|
|
for (i = 0; _iotable[i] != 0 ; i++)
|
|
|
|
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-06-26 10:37:05 +00:00
|
|
|
flags |= _IOWRITE | _IOWRITING;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(name, rwmode | rwflags, PMODE);
|
|
|
|
|
|
|
|
if (fd < 0) return (FILE *)NULL;
|
|
|
|
|
|
|
|
if (( stream = (FILE *) malloc(sizeof(FILE))) == NULL ) {
|
|
|
|
close(fd);
|
|
|
|
return (FILE *)NULL;
|
|
|
|
}
|
|
|
|
|
1989-06-26 10:37:05 +00:00
|
|
|
if ((flags & _IOREAD) && (flags & _IOWRITE))
|
|
|
|
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-05-30 13:34:25 +00:00
|
|
|
_iotable[i] = stream;
|
|
|
|
return stream;
|
|
|
|
}
|