xref: /csrg-svn/old/as.vax/asio.c (revision 648)
1594Sbill /* Coypright (c) 1980 Regents of the University of California */
2*648Shenry static	char sccsid[] = "@(#)asio.c 4.3 08/16/80";
3594Sbill #include <stdio.h>
4594Sbill #include "as.h"
5594Sbill /*
6594Sbill  *	Block I/O routines for logical I/O concurrently in
7594Sbill  *	more than one place in the same file.
8594Sbill  */
9594Sbill int	biofd;			/* file descriptor for block I/O file */
10594Sbill off_t	boffset;		/* physical position in logical file */
11594Sbill BFILE	*biobufs;		/* the block I/O buffers */
12594Sbill 
13594Sbill #define	error(severity, message) \
14594Sbill 	{yyerror(message); if (severity) delexit();}
15594Sbill 
16594Sbill Flushfield(n)
17594Sbill 	register int n;
18594Sbill {
19594Sbill 	while (n>0) {
20594Sbill 		outb(bitfield);
21594Sbill 		bitfield >>= 8;
22594Sbill 		n -= 8;
23594Sbill 	}
24594Sbill 	bitoff=0;
25594Sbill 	bitfield=0;
26594Sbill }
27594Sbill 
28594Sbill /*
29*648Shenry  *	Block I/O Routines
30594Sbill  */
31594Sbill bopen(bp, off)
32594Sbill 	struct biobuf *bp;
33594Sbill 	off_t	off;
34594Sbill {
35594Sbill 
36594Sbill 	bp->b_ptr = bp->b_buf;
37594Sbill 	bp->b_nleft = BUFSIZ - off % BUFSIZ;
38594Sbill 	bp->b_off = off;
39594Sbill 	bp->b_link = biobufs;
40594Sbill 	biobufs = bp;
41594Sbill }
42594Sbill 
43594Sbill int	bwrerror;
44594Sbill 
45594Sbill bwrite(p, cnt, bp)
46594Sbill 	register char *p;
47594Sbill 	register int cnt;
48594Sbill 	register struct biobuf *bp;
49594Sbill {
50594Sbill 	register int put;
51594Sbill 	register char *to;
52594Sbill 
53594Sbill top:
54594Sbill 	if (cnt == 0)
55594Sbill 		return;
56594Sbill 	if (bp->b_nleft) {
57594Sbill 		put = bp->b_nleft;
58594Sbill 		if (put > cnt)
59594Sbill 			put = cnt;
60594Sbill 		bp->b_nleft -= put;
61594Sbill 		to = bp->b_ptr;
62594Sbill 		asm("movc3 r8,(r11),(r7)");
63594Sbill 		bp->b_ptr += put;
64594Sbill 		p += put;
65594Sbill 		cnt -= put;
66594Sbill 		goto top;
67594Sbill 	}
68594Sbill 	if (cnt >= BUFSIZ) {
69594Sbill 		if (bp->b_ptr != bp->b_buf)
70594Sbill 			bflush1(bp);
71594Sbill 		put = cnt - cnt % BUFSIZ;
72594Sbill 		if (boffset != bp->b_off)
73594Sbill 			lseek(biofd, bp->b_off, 0);
74594Sbill 		if (write(biofd, p, put) != put) {
75594Sbill 			bwrerror = 1;
76594Sbill 			error(1, "Output write error");
77594Sbill 		}
78594Sbill 		bp->b_off += put;
79594Sbill 		boffset = bp->b_off;
80594Sbill 		p += put;
81594Sbill 		cnt -= put;
82594Sbill 		goto top;
83594Sbill 	}
84594Sbill 	bflush1(bp);
85594Sbill 	goto top;
86594Sbill }
87594Sbill 
88594Sbill bflush()
89594Sbill {
90594Sbill 	register struct biobuf *bp;
91594Sbill 
92594Sbill 	if (bwrerror)
93594Sbill 		return;
94594Sbill 	for (bp = biobufs; bp; bp = bp->b_link)
95594Sbill 		bflush1(bp);
96594Sbill }
97594Sbill 
98594Sbill bflush1(bp)
99594Sbill 	register struct biobuf *bp;
100594Sbill {
101594Sbill 	register int cnt = bp->b_ptr - bp->b_buf;
102594Sbill 
103594Sbill 	if (cnt == 0)
104594Sbill 		return;
105594Sbill 	if (boffset != bp->b_off)
106594Sbill 		lseek(biofd, bp->b_off, 0);
107594Sbill 	if (write(biofd, bp->b_buf, cnt) != cnt) {
108594Sbill 		bwrerror = 1;
109594Sbill 		error(1, "Output write error");
110594Sbill 	}
111594Sbill 	bp->b_off += cnt;
112594Sbill 	boffset = bp->b_off;
113594Sbill 	bp->b_ptr = bp->b_buf;
114594Sbill 	bp->b_nleft = BUFSIZ;
115594Sbill }
116594Sbill 
117594Sbill bflushc(bp, c)
118594Sbill 	register struct biobuf *bp;
119594Sbill 	char	c;
120594Sbill {
121594Sbill 
122594Sbill 	bflush1(bp);
123594Sbill 	bputc(c, bp);
124594Sbill }
125