1989-05-30 13:34:25 +00:00
|
|
|
/*
|
|
|
|
* fopen.c - open a stream
|
|
|
|
*/
|
1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1989-05-30 13:34:25 +00:00
|
|
|
|
2007-04-21 23:18:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "loc_incl.h"
|
1989-05-30 13:34:25 +00:00
|
|
|
|
|
|
|
#define PMODE 0666
|
|
|
|
|
1989-12-18 15:04:14 +00:00
|
|
|
/* 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.
|
2007-04-21 23:18:14 +00:00
|
|
|
*
|
|
|
|
* FIXME dtrg: I'm not sure this is relevant any more. Implementing O_CREAT
|
|
|
|
* and O_APPEND ought to be the job of the syscall library, no? Besides, the
|
|
|
|
* code requires valid definitions.
|
|
|
|
*
|
|
|
|
* Remember to fix freopen.c if changing this.
|
1989-12-18 15:04:14 +00:00
|
|
|
*/
|
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++)
|
1991-02-22 16:32:05 +00:00
|
|
|
if ( i >= FOPEN_MAX-1 )
|
1989-05-30 13:34:25 +00:00
|
|
|
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':
|
1990-04-24 09:30:15 +00:00
|
|
|
continue;
|
1989-05-30 13:34:25 +00:00
|
|
|
case '+':
|
|
|
|
rwmode = O_RDWR;
|
|
|
|
flags |= _IOREAD | _IOWRITE;
|
1990-04-24 09:30:15 +00:00
|
|
|
continue;
|
|
|
|
/* The sequence may be followed by additional characters */
|
1989-05-30 13:34:25 +00:00
|
|
|
default:
|
1990-04-24 09:30:15 +00:00
|
|
|
break;
|
1989-05-30 13:34:25 +00:00
|
|
|
}
|
1990-04-24 09:30:15 +00:00
|
|
|
break;
|
1989-05-30 13:34:25 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2007-04-21 23:18:14 +00:00
|
|
|
|| (((fd = open(name, rwmode)) < 0)
|
1990-04-09 15:55:51 +00:00
|
|
|
&& (rwflags & O_CREAT))) {
|
2007-04-21 23:18:14 +00:00
|
|
|
if (((fd = creat(name, PMODE)) > 0) && flags | _IOREAD) {
|
|
|
|
(void) close(fd);
|
|
|
|
fd = open(name, rwmode);
|
1990-04-09 15:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
1989-05-30 13:34:25 +00:00
|
|
|
|
|
|
|
if (fd < 0) return (FILE *)NULL;
|
|
|
|
|
|
|
|
if (( stream = (FILE *) malloc(sizeof(FILE))) == NULL ) {
|
2007-04-21 23:18:14 +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;
|
|
|
|
}
|