1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #if defined(LIBC_SCCS) && !defined(lint) 8 static char sccsid[] = "@(#)freopen.c 5.2 (Berkeley) 03/09/86"; 9 #endif LIBC_SCCS and not lint 10 11 #include <sys/types.h> 12 #include <sys/file.h> 13 #include <stdio.h> 14 15 FILE * 16 freopen(file, mode, iop) 17 char *file; 18 register char *mode; 19 register FILE *iop; 20 { 21 register f, rw, oflags; 22 23 rw = (mode[1] == '+'); 24 25 fclose(iop); 26 27 switch (*mode) { 28 case 'a': 29 oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY); 30 break; 31 case 'r': 32 oflags = rw ? O_RDWR : O_RDONLY; 33 break; 34 case 'w': 35 oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY); 36 break; 37 default: 38 return (NULL); 39 } 40 41 f = open(file, oflags, 0666); 42 if (f < 0) 43 return (NULL); 44 45 if (*mode == 'a') 46 lseek(f, (off_t)0, L_XTND); 47 48 iop->_cnt = 0; 49 iop->_file = f; 50 iop->_bufsiz = 0; 51 if (rw) 52 iop->_flag = _IORW; 53 else if (*mode == 'r') 54 iop->_flag = _IOREAD; 55 else 56 iop->_flag = _IOWRT; 57 iop->_base = iop->_ptr = NULL; 58 return (iop); 59 } 60