BSD 3 development
[unix-history] / usr / src / libc / stdio / fopen.c
CommitLineData
042f6a81
BJ
1#include <stdio.h>
2#include <errno.h>
3
4FILE *
5fopen(file, mode)
6char *file;
7register char *mode;
8{
9 extern int errno;
10 register f;
11 register FILE *iop;
12 extern FILE *_lastbuf;
13
14 for (iop = _iob; iop->_flag&(_IOREAD|_IOWRT); iop++)
15 if (iop >= _lastbuf)
16 return(NULL);
17 if (*mode=='w')
18 f = creat(file, 0666);
19 else if (*mode=='a') {
20 if ((f = open(file, 1)) < 0) {
21 if (errno == ENOENT)
22 f = creat(file, 0666);
23 }
24 if (f >= 0)
25 lseek(f, 0L, 2);
26 } else
27 f = open(file, 0);
28 if (f < 0)
29 return(NULL);
30 iop->_cnt = 0;
31 iop->_file = f;
32 if (*mode != 'r')
33 iop->_flag |= _IOWRT;
34 else
35 iop->_flag |= _IOREAD;
36 return(iop);
37}