source: buchla-68k/libcio/putc.c@ 7258c6a

Last change on this file since 7258c6a was 7258c6a, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Use standard integer types.

  • Property mode set to 100644
File size: 3.4 KB
Line 
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
11extern int16_t write(int16_t fd, int8_t *buff, uint16_t len);
12extern void getbuff(FILE *ptr);
13extern int16_t close(int16_t fd);
14
15extern int16_t (*_clsall)();
16
17static int16_t (*cls_rtn)();
18
19int16_t _ClFlag;
20
21int16_t fclose(FILE *ptr);
22
23/*
24 ============================================================================
25 closall() -- close all files at exit
26 ============================================================================
27*/
28
29static void closall(void)
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*/
48
49int16_t flush_(FILE *ptr, int16_t data)
50{
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 ? */
61
62 size = (int16_t)((int32_t)ptr->_bp - (int32_t)ptr->_buff);
63
64 if (write(ptr->_unit, ptr->_buff, size) EQ -1) {
65
66ioerr:
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 ============================================================================
105*/
106
107int16_t fflush(FILE *ptr)
108{
109 return(flush_(ptr, -1));
110}
111
112/*
113 ============================================================================
114 fclose() -- close a stream file
115 ============================================================================
116*/
117
118int16_t fclose(FILE *ptr)
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
131 if (ptr->_flags & _ALLBUF) { /* deallocate standard buffer */
132
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
149 ============================================================================
150*/
151
152int16_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
163 ============================================================================
164*/
165
166int16_t puterr(int16_t c)
167{
168 return(putc(c, stderr));
169}
170
Note: See TracBrowser for help on using the repository browser.