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