ack/lang/cem/libcc.ansi/stdio/freopen.c

96 lines
1.8 KiB
C
Raw Normal View History

1989-05-30 13:34:25 +00:00
/*
* freopen.c - open a file and associate a stream with it
*/
/* $Header$ */
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
1989-12-18 15:04:14 +00:00
#define PMODE 0666
/* Do not "optimize" this file to use the open with O_CREAT if the file
* does not exist. The reason is given in fopen.c.
*/
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define O_CREAT 0x010
#define O_TRUNC 0x020
#define O_APPEND 0x040
int open(const char *path, int flags);
int creat(const char *path, int mode);
1989-05-30 13:34:25 +00:00
int close(int d);
FILE *
freopen(const char *name, const char *mode, FILE *stream)
{
register int i;
int rwmode = 0, rwflags = 0;
1989-12-18 15:04:14 +00:00
int fd, flags = stream->_flags & (_IONBF | _IOFBF | _IOLBF | _IOMYBUF);
1989-05-30 13:34:25 +00:00
(void) fflush(stream); /* ignore errors */
(void) close(fileno(stream));
switch(*mode++) {
case 'r':
flags |= _IOREAD;
rwmode = O_RDONLY;
break;
case 'w':
flags |= _IOWRITE;
rwmode = O_WRONLY;
rwflags = O_CREAT | O_TRUNC;
break;
case 'a':
1989-12-18 15:04:14 +00:00
flags |= _IOWRITE | _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
if ((rwflags & O_TRUNC)
|| (((fd = open(name, rwmode)) < 0)
&& (flags & _IOWRITE)))
fd = creat(name, PMODE);
1989-05-30 13:34:25 +00:00
if (fd < 0) {
for( i = 0; i < FOPEN_MAX; i++) {
1989-12-18 15:04:14 +00:00
if (stream == __iotab[i]) {
__iotab[i] = 0;
1989-05-30 13:34:25 +00:00
break;
}
}
if (stream != stdin && stream != stdout && stream != stderr)
free((void *)stream);
return (FILE *)NULL;
}
stream->_count = 0;
if (stream->_buf && !(flags & _IONBF) && (flags & _IOWRITE))
if (flags & _IOLBF)
stream->_count = 0;
else stream->_count = stream->_bufsiz;
stream->_fd = fd;
stream->_flags = flags;
1989-06-26 10:37:05 +00:00
return stream;
1989-05-30 13:34:25 +00:00
}