xref: /csrg-svn/lib/libc/stdio/fopen.c (revision 2004)
1 /* @(#)fopen.c	4.1 (Berkeley) 12/21/80 */
2 #include	<stdio.h>
3 #include	<errno.h>
4 
5 FILE *
6 fopen(file, mode)
7 char *file;
8 register char *mode;
9 {
10 	extern int errno;
11 	register f;
12 	register FILE *iop;
13 	extern FILE *_lastbuf;
14 
15 	for (iop = _iob; iop->_flag&(_IOREAD|_IOWRT); iop++)
16 		if (iop >= _lastbuf)
17 			return(NULL);
18 	if (*mode=='w')
19 		f = creat(file, 0666);
20 	else if (*mode=='a') {
21 		if ((f = open(file, 1)) < 0) {
22 			if (errno == ENOENT)
23 				f = creat(file, 0666);
24 		}
25 		if (f >= 0)
26 			lseek(f, 0L, 2);
27 	} else
28 		f = open(file, 0);
29 	if (f < 0)
30 		return(NULL);
31 	iop->_cnt = 0;
32 	iop->_file = f;
33 	if (*mode != 'r')
34 		iop->_flag |= _IOWRT;
35 	else
36 		iop->_flag |= _IOREAD;
37 	return(iop);
38 }
39