xref: /csrg-svn/old/as.vax/asio.c (revision 594)
1 /* Coypright (c) 1980 Regents of the University of California */
2 static	char sccsid[] = "@(#)asio.c 4.1 08/13/80";
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include "as.h"
6 /*
7  *	Block I/O routines for logical I/O concurrently in
8  *	more than one place in the same file.
9  */
10 int	biofd;			/* file descriptor for block I/O file */
11 off_t	boffset;		/* physical position in logical file */
12 BFILE	*biobufs;		/* the block I/O buffers */
13 
14 #define	error(severity, message) \
15 	{yyerror(message); if (severity) delexit();}
16 
17 Flushfield(n)
18 	register int n;
19 {
20 	while (n>0) {
21 		outb(bitfield);
22 		bitfield >>= 8;
23 		n -= 8;
24 	}
25 	bitoff=0;
26 	bitfield=0;
27 }
28 
29 #ifdef ASFWRITE
30 /*
31  *	This is our version of fwrite...
32  *	Hacked out fast version of fwrite that
33  *	doesn't iterate over each and every character;
34  *	We poke directly into the buffer area, and move things
35  *	with a movc3.
36  */
37 fwrite(p, n, m, f)
38 	register char *p;
39 	int n, m;
40 	register FILE *f;
41 {
42 	register int cnt = n * m;
43 	register int put;
44 	register char *to;
45 
46 top:
47 	if (cnt == 0)
48 		return;
49 	if (f->_cnt) {
50 		put = f->_cnt;
51 		if (put > cnt)
52 			put = cnt;
53 		f->_cnt -= put;
54 		to = f->_ptr;
55 		asm("movc3 r8,(r11),(r7)");
56 		f->_ptr += put;
57 		p += put;
58 		cnt -= put;
59 		goto top;
60 	}
61 	if (cnt >= BUFSIZ) {
62 		fflush(f);
63 		put = cnt - cnt % BUFSIZ;
64 		if (write(f->_file, p, put) != put)
65 			error(1, "Output write error in fwrite");
66 		p += put;
67 		cnt -= put;
68 		goto top;
69 	}
70 	_flsbuf(*p++, f);
71 	--cnt;
72 	goto top;
73 }
74 
75 /*
76  *	This has been stolen from the usual place...
77  *	It is put here so that the loader doesn't complain
78  *	about multiple definitions in the archived object module.
79  *
80  *	archived in: /lib/libc.a
81  *	object module from: /usr/src/libc/stdio/rdwr.c
82  */
83 fread(ptr, size, count, iop)
84 	unsigned size, count;
85 	register char *ptr;
86 	register FILE *iop;
87 {
88 	register c;
89 	unsigned ndone, s;
90 
91 	ndone = 0;
92 	if (size)
93 	for (; ndone<count; ndone++) {
94 		s = size;
95 		do {
96 			if ((c = getc(iop)) >= 0)
97 				*ptr++ = c;
98 			else
99 				return(ndone);
100 		} while (--s);
101 	}
102 	return(ndone);
103 }
104 #endif ASFWRITE
105 
106 bopen(bp, off)
107 	struct biobuf *bp;
108 	off_t	off;
109 {
110 
111 	bp->b_ptr = bp->b_buf;
112 	bp->b_nleft = BUFSIZ - off % BUFSIZ;
113 	bp->b_off = off;
114 	bp->b_link = biobufs;
115 	biobufs = bp;
116 }
117 
118 int	bwrerror;
119 
120 bwrite(p, cnt, bp)
121 	register char *p;
122 	register int cnt;
123 	register struct biobuf *bp;
124 {
125 	register int put;
126 	register char *to;
127 
128 top:
129 	if (cnt == 0)
130 		return;
131 	if (bp->b_nleft) {
132 		put = bp->b_nleft;
133 		if (put > cnt)
134 			put = cnt;
135 		bp->b_nleft -= put;
136 		to = bp->b_ptr;
137 		asm("movc3 r8,(r11),(r7)");
138 		bp->b_ptr += put;
139 		p += put;
140 		cnt -= put;
141 		goto top;
142 	}
143 	if (cnt >= BUFSIZ) {
144 		if (bp->b_ptr != bp->b_buf)
145 			bflush1(bp);
146 		put = cnt - cnt % BUFSIZ;
147 		if (boffset != bp->b_off)
148 			lseek(biofd, bp->b_off, 0);
149 		if (write(biofd, p, put) != put) {
150 			bwrerror = 1;
151 			error(1, "Output write error");
152 		}
153 		bp->b_off += put;
154 		boffset = bp->b_off;
155 		p += put;
156 		cnt -= put;
157 		goto top;
158 	}
159 	bflush1(bp);
160 	goto top;
161 }
162 
163 bflush()
164 {
165 	register struct biobuf *bp;
166 
167 	if (bwrerror)
168 		return;
169 	for (bp = biobufs; bp; bp = bp->b_link)
170 		bflush1(bp);
171 }
172 
173 bflush1(bp)
174 	register struct biobuf *bp;
175 {
176 	register int cnt = bp->b_ptr - bp->b_buf;
177 
178 	if (cnt == 0)
179 		return;
180 	if (boffset != bp->b_off)
181 		lseek(biofd, bp->b_off, 0);
182 	if (write(biofd, bp->b_buf, cnt) != cnt) {
183 		bwrerror = 1;
184 		error(1, "Output write error");
185 	}
186 	bp->b_off += cnt;
187 	boffset = bp->b_off;
188 	bp->b_ptr = bp->b_buf;
189 	bp->b_nleft = BUFSIZ;
190 }
191 
192 bflushc(bp, c)
193 	register struct biobuf *bp;
194 	char	c;
195 {
196 
197 	bflush1(bp);
198 	bputc(c, bp);
199 }
200