1 | /*
|
---|
2 | =============================================================================
|
---|
3 | fopen.c -- open a stream file for the Buchla 700 C I/O Library
|
---|
4 | Version 4 -- 1987-06-30 -- D.N. Lynx Crowe
|
---|
5 | =============================================================================
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "stdio.h"
|
---|
9 | #include "fcntl.h"
|
---|
10 | #include "errno.h"
|
---|
11 | #include "stddefs.h"
|
---|
12 |
|
---|
13 | extern long lseek();
|
---|
14 |
|
---|
15 | FILE *
|
---|
16 | _opener(name, mode, aflag)
|
---|
17 | char *name, *mode;
|
---|
18 | int aflag;
|
---|
19 | {
|
---|
20 | register FILE *fp;
|
---|
21 | register int plusopt;
|
---|
22 |
|
---|
23 | fp = Cbuffs;
|
---|
24 |
|
---|
25 | while ( fp->_flags ) {
|
---|
26 |
|
---|
27 | if ( ++fp GE (Cbuffs + NSTREAMS))
|
---|
28 | return(NULL);
|
---|
29 | }
|
---|
30 |
|
---|
31 | plusopt = mode[1] EQ '+';
|
---|
32 |
|
---|
33 | switch (0x007F & *mode) {
|
---|
34 |
|
---|
35 | case 'r': /* read mode */
|
---|
36 | if ((fp->_unit = open(name, (plusopt ? O_RDWR : O_RDONLY)|aflag)) EQ -1)
|
---|
37 | return(NULL);
|
---|
38 | break;
|
---|
39 |
|
---|
40 | case 'w': /* write mode */
|
---|
41 | if ((fp->_unit = open(name, (plusopt ? O_RDWR : O_WRONLY)|aflag|O_CREAT|O_TRUNC)) EQ -1)
|
---|
42 | return(NULL);
|
---|
43 |
|
---|
44 | break;
|
---|
45 |
|
---|
46 | case 'a': /* append mode */
|
---|
47 | if ((fp->_unit = open(name, (plusopt ? O_RDWR : O_WRONLY)|aflag|O_CREAT)) EQ -1)
|
---|
48 | return(NULL);
|
---|
49 |
|
---|
50 | if (lseek(fp->_unit, 0L, 2) < 0) {
|
---|
51 |
|
---|
52 | close(fp->_unit);
|
---|
53 | return(NULL); /* couldn't append */
|
---|
54 | }
|
---|
55 |
|
---|
56 | break;
|
---|
57 |
|
---|
58 | default:
|
---|
59 | errno = EINVAL;
|
---|
60 | return(NULL);
|
---|
61 | }
|
---|
62 |
|
---|
63 | fp->_flags = _BUSY;
|
---|
64 | fp->_buflen = BUFSIZ;
|
---|
65 | fp->_buff = 0;
|
---|
66 | fp->_bend = 0; /* nothing in buffer */
|
---|
67 | fp->_bp = 0;
|
---|
68 | return(fp);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /* |
---|
72 |
|
---|
73 | */
|
---|
74 |
|
---|
75 | FILE *
|
---|
76 | fopen(name, mode)
|
---|
77 | char *name, *mode;
|
---|
78 | {
|
---|
79 | return(_opener(name, mode, 0));
|
---|
80 | }
|
---|
81 |
|
---|
82 | FILE *
|
---|
83 | fopena(name, mode)
|
---|
84 | char *name, *mode;
|
---|
85 | {
|
---|
86 | return(_opener(name, mode, 0));
|
---|
87 | }
|
---|
88 |
|
---|
89 | FILE *
|
---|
90 | fopenb(name, mode)
|
---|
91 | char *name, *mode;
|
---|
92 | {
|
---|
93 | return(_opener(name, mode, O_RAW));
|
---|
94 | }
|
---|