home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
minnie.tuhs.org
/
unixen.tar
/
unixen
/
PDP-11
/
Distributions
/
ucb
/
spencer_2bsd.tar.gz
/
2bsd.tar
/
src
/
libNS
/
freopen.c
< prev
next >
Wrap
C/C++ Source or Header
|
1980-02-17
|
923b
|
52 lines
/* Copyright (c) 1979 Regents of the University of California */
#include <stdio.h>
struct _iobuf *freopen(file, mode, iop)
register char *mode;
register struct _iobuf *iop;
{
register f;
int unixmode, a_flag;
fclose(iop);
a_flag = 0;
for(;*mode;mode++) {
switch(*mode) {
case 'w':
iop->_flag |= _IOWRT;
break;
case 'r':
iop->_flag |= _IOREAD;
break;
case 'a':
a_flag = 1;
iop->_flag |= _IOWRT ;
}
}
if((unixmode = (iop->_flag & 3) - 1) < 0) {
unixmode = 0;
iop->_flag = 1;
}
if ((iop->_flag & _IOWRT) && a_flag==0 ) {
f = creat(file, 0644);
if((iop->_flag & _IOREAD) && (f > 0)) {
close(f);
f = open(file,2);
}
} else if ((0>(f = open(file, unixmode))) && (a_flag || !(iop->_flag&_IOREAD))){
f = creat(file, 0644);
close(f);
f = open(file,unixmode);
}
if (f < 0)
return(NULL);
if (a_flag)
lseek(f, (long) 0, 2);
iop->_file = f;
return(iop);
}