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