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 int32_t lseek(int16_t fd, int32_t pos, int16_t how);
|
---|
14 |
|
---|
15 | FILE *_opener(int8_t *name, int8_t *mode, int16_t aflag)
|
---|
16 | {
|
---|
17 | register FILE *fp;
|
---|
18 | register int16_t plusopt;
|
---|
19 |
|
---|
20 | fp = Cbuffs;
|
---|
21 |
|
---|
22 | while ( fp->_flags ) {
|
---|
23 |
|
---|
24 | if ( ++fp GE (Cbuffs + NSTREAMS))
|
---|
25 | return(NULL);
|
---|
26 | }
|
---|
27 |
|
---|
28 | plusopt = mode[1] EQ '+';
|
---|
29 |
|
---|
30 | switch (0x007F & *mode) {
|
---|
31 |
|
---|
32 | case 'r': /* read mode */
|
---|
33 | if ((fp->_unit = open(name, (plusopt ? O_RDWR : O_RDONLY)|aflag)) EQ -1)
|
---|
34 | return(NULL);
|
---|
35 | break;
|
---|
36 |
|
---|
37 | case 'w': /* write mode */
|
---|
38 | if ((fp->_unit = open(name, (plusopt ? O_RDWR : O_WRONLY)|aflag|O_CREAT|O_TRUNC)) EQ -1)
|
---|
39 | return(NULL);
|
---|
40 |
|
---|
41 | break;
|
---|
42 |
|
---|
43 | case 'a': /* append mode */
|
---|
44 | if ((fp->_unit = open(name, (plusopt ? O_RDWR : O_WRONLY)|aflag|O_CREAT)) EQ -1)
|
---|
45 | return(NULL);
|
---|
46 |
|
---|
47 | if (lseek(fp->_unit, 0L, 2) < 0) {
|
---|
48 |
|
---|
49 | close(fp->_unit);
|
---|
50 | return(NULL); /* couldn't append */
|
---|
51 | }
|
---|
52 |
|
---|
53 | break;
|
---|
54 |
|
---|
55 | default:
|
---|
56 | errno = EINVAL;
|
---|
57 | return(NULL);
|
---|
58 | }
|
---|
59 |
|
---|
60 | fp->_flags = _BUSY;
|
---|
61 | fp->_buflen = BUFSIZ;
|
---|
62 | fp->_buff = 0;
|
---|
63 | fp->_bend = 0; /* nothing in buffer */
|
---|
64 | fp->_bp = 0;
|
---|
65 | return(fp);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* |
---|
69 |
|
---|
70 | */
|
---|
71 |
|
---|
72 | FILE *fopen(int8_t *name, int8_t *mode)
|
---|
73 | {
|
---|
74 | return(_opener(name, mode, 0));
|
---|
75 | }
|
---|
76 |
|
---|
77 | FILE *fopena(int8_t *name, int8_t *mode)
|
---|
78 | {
|
---|
79 | return(_opener(name, mode, 0));
|
---|
80 | }
|
---|
81 |
|
---|
82 | FILE *fopenb(int8_t *name, int8_t *mode)
|
---|
83 | {
|
---|
84 | return(_opener(name, mode, O_RAW));
|
---|
85 | }
|
---|