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