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