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