| 1 | /* | 
|---|
| 2 | ============================================================================ | 
|---|
| 3 | putc.c -- stream file output and close functions | 
|---|
| 4 | Version 7 -- 1987-10-19 -- D.N. Lynx Crowe | 
|---|
| 5 | ============================================================================ | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "ram.h" | 
|---|
| 9 |  | 
|---|
| 10 | static void (*cls_rtn)(void); | 
|---|
| 11 |  | 
|---|
| 12 | int16_t _ClFlag; | 
|---|
| 13 |  | 
|---|
| 14 | /* | 
|---|
| 15 | ============================================================================ | 
|---|
| 16 | closall() -- close all files at exit | 
|---|
| 17 | ============================================================================ | 
|---|
| 18 | */ | 
|---|
| 19 |  | 
|---|
| 20 | static void closall(void) | 
|---|
| 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 |  | 
|---|
| 36 | int16_t flush_(FILE *ptr, int16_t data) | 
|---|
| 37 | { | 
|---|
| 38 | register int16_t size; | 
|---|
| 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 |  | 
|---|
| 49 | size = (int16_t)((int32_t)ptr->_bp - (int32_t)ptr->_buff); | 
|---|
| 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 |  | 
|---|
| 61 | ptr->_flags = (uint8_t)(ptr->_flags & ~_DIRTY); | 
|---|
| 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 |  | 
|---|
| 81 | return((*ptr->_bp++ = (int8_t)data) & 0x00FF); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | /* | 
|---|
| 85 | ============================================================================ | 
|---|
| 86 | fflush() -- flush a stream file | 
|---|
| 87 | ============================================================================ | 
|---|
| 88 | */ | 
|---|
| 89 |  | 
|---|
| 90 | int16_t fflush(FILE *ptr) | 
|---|
| 91 | { | 
|---|
| 92 | return(flush_(ptr, -1)); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | /* | 
|---|
| 96 | ============================================================================ | 
|---|
| 97 | fclose() -- close a stream file | 
|---|
| 98 | ============================================================================ | 
|---|
| 99 | */ | 
|---|
| 100 |  | 
|---|
| 101 | int16_t fclose(FILE *ptr) | 
|---|
| 102 | { | 
|---|
| 103 | int16_t err; | 
|---|
| 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 |  | 
|---|
| 116 | *(int32_t **)ptr->_buff = Stdbufs; | 
|---|
| 117 | Stdbufs = (int32_t *)ptr->_buff; | 
|---|
| 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 |  | 
|---|
| 131 | int16_t putc(int16_t c, FILE *ptr) | 
|---|
| 132 | { | 
|---|
| 133 | if (ptr->_bp GE ptr->_bend) | 
|---|
| 134 | return(flush_(ptr, c & 0xFF)); | 
|---|
| 135 |  | 
|---|
| 136 | return((*ptr->_bp++ = (int8_t)c) & 0xFF); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | /* | 
|---|
| 140 | ============================================================================ | 
|---|
| 141 | puterr() -- write a character to stderr | 
|---|
| 142 | ============================================================================ | 
|---|
| 143 | */ | 
|---|
| 144 |  | 
|---|
| 145 | int16_t puterr(int16_t c) | 
|---|
| 146 | { | 
|---|
| 147 | return(putc(c, stderr)); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 |  | 
|---|