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

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

Added last missing function pointer prototypes.

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