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

59 lines
969 B
C
Raw Normal View History

1987-03-31 10:45:53 +00:00
/* $Header$ */
1987-01-27 15:57:55 +00:00
#include <stdio.h>
#define PMODE 0666
FILE *freopen(name,mode,fp)
char *name , *mode;
1987-08-17 14:23:16 +00:00
register FILE *fp;
1987-01-27 15:57:55 +00:00
{
int fd,
1987-08-17 14:23:16 +00:00
flags = fp->_flags & ~(IO_WRITEMODE|IO_READMODE|IO_ERR|IO_EOF|IO_PERPRINTF);
1987-01-27 15:57:55 +00:00
1987-06-12 19:10:12 +00:00
fflush(fp);
1987-01-27 15:57:55 +00:00
close(fileno(fp));
switch(*mode){
case 'w':
flags |= IO_WRITEMODE;
fd = creat (name,PMODE);
break;
case 'a':
flags |= IO_WRITEMODE;
if (( fd = open(name, 1)) < 0 )
fd = creat(name, PMODE);
if (fd >= 0) lseek(fd,0L,2);
break;
case 'r':
flags |= IO_READMODE;
fd = open(name, 0);
break;
default:
fd = -1;
}
if (fd < 0) {
register int i;
for (i = 0; i < _NFILES; i++) {
if (fp == _io_table[i]) {
_io_table[i] = 0;
break;
}
}
1987-07-16 16:08:13 +00:00
if (fp != &_stdin && fp != &_stdout && fp != &_stderr) free(fp);
1987-01-27 15:57:55 +00:00
return NULL;
}
fp->_count = 0;
1987-09-02 12:52:44 +00:00
if (fp->_buf && !(flags & IO_UNBUFF) && (flags & IO_WRITEMODE))
1989-02-21 10:01:55 +00:00
fp->_count = fp->_bufsiz;
1987-01-27 15:57:55 +00:00
fp->_fd = fd;
fp->_flags = flags;
return(fp);
}