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

61 lines
893 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;
FILE *fp;
{
char *malloc();
int fd,
flags = 0;
1987-06-12 19:10:12 +00:00
fflush(fp);
1987-01-27 15:57:55 +00:00
close(fileno(fp));
if (io_testflag(fp, IO_MYBUF) && fp->_buf) free(fp->_buf);
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;
fp->_fd = fd;
fp->_flags = flags;
fp->_buf = 0;
return(fp);
}