[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 |
|
---|
[0580615] | 11 | extern int write(int fd, char *buff, unsigned len);
|
---|
| 12 | extern void getbuff(FILE *ptr);
|
---|
| 13 | extern int close(int fd);
|
---|
[f40a309] | 14 |
|
---|
| 15 | extern int (*_clsall)();
|
---|
| 16 |
|
---|
| 17 | static int (*cls_rtn)();
|
---|
| 18 |
|
---|
| 19 | int _ClFlag;
|
---|
| 20 |
|
---|
[0580615] | 21 | int 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 | */
|
---|
[0580615] | 48 |
|
---|
[f40a309] | 49 | int flush_(FILE *ptr, int data)
|
---|
| 50 | {
|
---|
| 51 | register int 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 ? */
|
---|
| 61 |
|
---|
| 62 | size = (int)((long)ptr->_bp - (long)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 | ============================================================================
|
---|
[0580615] | 105 | */
|
---|
[f40a309] | 106 |
|
---|
| 107 | int fflush(FILE *ptr)
|
---|
| 108 | {
|
---|
| 109 | return(flush_(ptr, -1));
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /*
|
---|
| 113 | ============================================================================
|
---|
| 114 | fclose() -- close a stream file
|
---|
| 115 | ============================================================================
|
---|
[0580615] | 116 | */
|
---|
[f40a309] | 117 |
|
---|
| 118 | int fclose(FILE *ptr)
|
---|
| 119 | {
|
---|
| 120 | int 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 |
|
---|
| 131 | if (ptr->_flags & _ALLBUF) { /* deallocate standard buffer */
|
---|
| 132 |
|
---|
| 133 | *(long **)ptr->_buff = Stdbufs;
|
---|
| 134 | Stdbufs = (long *)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
|
---|
[0580615] | 149 | ============================================================================
|
---|
[f40a309] | 150 | */
|
---|
| 151 |
|
---|
| 152 | int putc(int 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
|
---|
[0580615] | 163 | ============================================================================
|
---|
[f40a309] | 164 | */
|
---|
| 165 |
|
---|
| 166 | int puterr(c)
|
---|
| 167 | {
|
---|
| 168 | return(putc(c, stderr));
|
---|
| 169 | }
|
---|
| 170 |
|
---|