Commit | Line | Data |
---|---|---|
c6bee192 BJ |
1 | /* Copyright (c) 1979 Regents of the University of California */ |
2 | #include <nstdio.h> | |
3 | ||
4 | struct _iobuf *fopen(file, mode) | |
5 | register char *mode; | |
6 | { | |
7 | register f; | |
8 | register struct _iobuf *iop; | |
9 | int unixmode, a_flag, noclob; | |
10 | ||
11 | for (iop = _iob; iop->_flag&(_IOREAD|_IOWRT); iop++) | |
12 | if (iop >= _iob + _NFILE) | |
13 | return(NULL); | |
14 | iop->_flag = 0; | |
15 | iop->_delta = 0; | |
16 | a_flag = 0; | |
17 | noclob = 0; | |
18 | for(;*mode;mode++) { | |
19 | switch(*mode) { | |
20 | ||
21 | case 'w': | |
22 | iop->_flag |= _IOWRT; | |
23 | break; | |
24 | ||
25 | case 'r': | |
26 | iop->_flag |= _IOREAD; | |
27 | break; | |
28 | ||
29 | case 'a': | |
30 | a_flag = 1; | |
31 | noclob = 1; | |
32 | iop->_flag |= _IOWRT ; | |
33 | break; | |
34 | ||
35 | case '+': | |
36 | if (iop->_flag & _IOREAD) | |
37 | noclob = 1; | |
38 | iop->_flag |= (_IOREAD | _IOWRT); | |
39 | break; | |
40 | } | |
41 | } | |
42 | if((unixmode = (iop->_flag & 3) - 1) < 0) { | |
43 | unixmode = 0; | |
44 | iop->_flag = 1; | |
45 | } | |
46 | if ((iop->_flag & _IOWRT) && noclob==0 ) { | |
47 | f = creat(file, 0644); | |
48 | if((iop->_flag & _IOREAD) && (f>0)) { | |
49 | close(f); | |
50 | f = open(file,2); | |
51 | } | |
52 | } | |
53 | else | |
54 | if ((0 >(f = open(file, unixmode))) && (noclob || !(iop->_flag&_IOREAD))) { | |
55 | f = creat(file, 0644); | |
56 | close(f); | |
57 | f = open(file,unixmode); | |
58 | } | |
59 | ||
60 | if (f < 0) | |
61 | return(NULL); | |
62 | if (a_flag) | |
63 | lseek(f, (long) 0, 2); | |
64 | iop->_file = f; | |
65 | return(iop); | |
66 | } |