source: buchla-68k/orig/IOLIB/PUTC.C@ 6099cac

Last change on this file since 6099cac was 3ae31e9, checked in by Thomas Lopatic <thomas@…>, 8 years ago

Imported original source code.

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