xref: /csrg-svn/lib/libc/stdio/freopen.c (revision 2008)
1*2008Swnj /* @(#)freopen.c	4.1 (Berkeley) 12/21/80 */
2*2008Swnj #include <stdio.h>
3*2008Swnj 
4*2008Swnj FILE *
5*2008Swnj freopen(file, mode, iop)
6*2008Swnj char *file;
7*2008Swnj register char *mode;
8*2008Swnj register FILE *iop;
9*2008Swnj {
10*2008Swnj 	register f;
11*2008Swnj 
12*2008Swnj 	fclose(iop);
13*2008Swnj 	if (*mode=='w')
14*2008Swnj 		f = creat(file, 0666);
15*2008Swnj 	else if (*mode=='a') {
16*2008Swnj 		if ((f = open(file, 1)) < 0)
17*2008Swnj 			f = creat(file, 0666);
18*2008Swnj 		lseek(f, 0L, 2);
19*2008Swnj 	} else
20*2008Swnj 		f = open(file, 0);
21*2008Swnj 	if (f < 0)
22*2008Swnj 		return(NULL);
23*2008Swnj 	iop->_file = f;
24*2008Swnj 	if (*mode != 'r')
25*2008Swnj 		iop->_flag |= _IOWRT;
26*2008Swnj 	else
27*2008Swnj 		iop->_flag |= _IOREAD;
28*2008Swnj 	return(iop);
29*2008Swnj }
30