[f40a309] | 1 | /*
|
---|
| 2 | ============================================================================
|
---|
| 3 | putc.c -- stream file output and close functions
|
---|
| 4 | Version 7 -- 1987-10-19 -- D.N. Lynx Crowe
|
---|
| 5 | ============================================================================
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[b28a12e] | 8 | #include "ram.h"
|
---|
[f40a309] | 9 |
|
---|
[0170798] | 10 | static void (*cls_rtn)(void);
|
---|
[f40a309] | 11 |
|
---|
[7258c6a] | 12 | int16_t _ClFlag;
|
---|
[f40a309] | 13 |
|
---|
| 14 | /*
|
---|
| 15 | ============================================================================
|
---|
| 16 | closall() -- close all files at exit
|
---|
| 17 | ============================================================================
|
---|
| 18 | */
|
---|
| 19 |
|
---|
[0580615] | 20 | static void closall(void)
|
---|
[f40a309] | 21 | {
|
---|
| 22 | register FILE *fp;
|
---|
| 23 |
|
---|
| 24 | for (fp = Cbuffs; fp < (Cbuffs + NSTREAMS); ) /* close each file */
|
---|
| 25 | fclose(fp++);
|
---|
| 26 |
|
---|
| 27 | (*cls_rtn)(); /* do final closeout */
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | /*
|
---|
| 31 | ============================================================================
|
---|
| 32 | flush_() -- do the dirty work of flushing a file
|
---|
| 33 | ============================================================================
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[7258c6a] | 36 | int16_t flush_(FILE *ptr, int16_t data)
|
---|
[f40a309] | 37 | {
|
---|
[7258c6a] | 38 | register int16_t size;
|
---|
[f40a309] | 39 |
|
---|
| 40 | if (_ClFlag EQ 0) {
|
---|
| 41 |
|
---|
| 42 | cls_rtn = _clsall;
|
---|
| 43 | _clsall = closall;
|
---|
| 44 | _ClFlag = 1;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | if (ptr->_flags & _DIRTY) { /* something in the buffer ? */
|
---|
| 48 |
|
---|
[7258c6a] | 49 | size = (int16_t)((int32_t)ptr->_bp - (int32_t)ptr->_buff);
|
---|
[f40a309] | 50 |
|
---|
| 51 | if (write(ptr->_unit, ptr->_buff, size) EQ -1) {
|
---|
| 52 |
|
---|
| 53 | ioerr:
|
---|
| 54 | ptr->_flags |= _IOERR;
|
---|
| 55 | return(EOF);
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | if (data EQ -1) { /* just flushing, not adding data */
|
---|
| 60 |
|
---|
[8973acd] | 61 | ptr->_flags = (uint8_t)(ptr->_flags & ~_DIRTY);
|
---|
[f40a309] | 62 | ptr->_bend = ptr->_bp = NULL;
|
---|
| 63 | return(0);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | if (ptr->_buff EQ NULL) /* get a buffer if we don't have one */
|
---|
| 67 | getbuff(ptr);
|
---|
| 68 |
|
---|
| 69 | if (ptr->_buflen EQ 1) { /* unbuffered I/O */
|
---|
| 70 |
|
---|
| 71 | if (write(ptr->_unit, &data, 1) EQ -1)
|
---|
| 72 | goto ioerr;
|
---|
| 73 |
|
---|
| 74 | return(data);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | ptr->_bp = ptr->_buff;
|
---|
| 78 | ptr->_bend = ptr->_buff + ptr->_buflen;
|
---|
| 79 | ptr->_flags |= _DIRTY;
|
---|
| 80 |
|
---|
[8973acd] | 81 | return((*ptr->_bp++ = (int8_t)data) & 0x00FF);
|
---|
[f40a309] | 82 | }
|
---|
| 83 |
|
---|
| 84 | /*
|
---|
| 85 | ============================================================================
|
---|
| 86 | fflush() -- flush a stream file
|
---|
| 87 | ============================================================================
|
---|
| 88 | */
|
---|
| 89 |
|
---|
[7258c6a] | 90 | int16_t fflush(FILE *ptr)
|
---|
[f40a309] | 91 | {
|
---|
| 92 | return(flush_(ptr, -1));
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /*
|
---|
| 96 | ============================================================================
|
---|
| 97 | fclose() -- close a stream file
|
---|
| 98 | ============================================================================
|
---|
| 99 | */
|
---|
| 100 |
|
---|
[7258c6a] | 101 | int16_t fclose(FILE *ptr)
|
---|
[f40a309] | 102 | {
|
---|
[7258c6a] | 103 | int16_t err;
|
---|
[f40a309] | 104 |
|
---|
| 105 | err = 0;
|
---|
| 106 |
|
---|
| 107 | if (ptr->_flags) {
|
---|
| 108 |
|
---|
| 109 | if (ptr->_flags & _DIRTY) /* if modifed, flush buffer */
|
---|
| 110 | err = flush_(ptr, -1);
|
---|
| 111 |
|
---|
| 112 | err |= close(ptr->_unit);
|
---|
| 113 |
|
---|
| 114 | if (ptr->_flags & _ALLBUF) { /* deallocate standard buffer */
|
---|
| 115 |
|
---|
[7258c6a] | 116 | *(int32_t **)ptr->_buff = Stdbufs;
|
---|
| 117 | Stdbufs = (int32_t *)ptr->_buff;
|
---|
[f40a309] | 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | ptr->_flags = 0;
|
---|
| 122 | return(err);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /*
|
---|
| 126 | ============================================================================
|
---|
| 127 | putc() -- write a character on a file
|
---|
| 128 | ============================================================================
|
---|
| 129 | */
|
---|
| 130 |
|
---|
[7258c6a] | 131 | int16_t putc(int16_t c, FILE *ptr)
|
---|
[f40a309] | 132 | {
|
---|
| 133 | if (ptr->_bp GE ptr->_bend)
|
---|
| 134 | return(flush_(ptr, c & 0xFF));
|
---|
| 135 |
|
---|
[8973acd] | 136 | return((*ptr->_bp++ = (int8_t)c) & 0xFF);
|
---|
[f40a309] | 137 | }
|
---|
| 138 |
|
---|
| 139 | /*
|
---|
| 140 | ============================================================================
|
---|
| 141 | puterr() -- write a character to stderr
|
---|
| 142 | ============================================================================
|
---|
| 143 | */
|
---|
| 144 |
|
---|
[7258c6a] | 145 | int16_t puterr(int16_t c)
|
---|
[f40a309] | 146 | {
|
---|
| 147 | return(putc(c, stderr));
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[6262b5c] | 150 |
|
---|