xref: /csrg-svn/old/as.vax/asio.c (revision 16070)
15824Srrh /*
25824Srrh  *	Copyright (c) 1982 Regents of the University of California
35824Srrh  */
45824Srrh #ifndef lint
5*16070Sralph static char sccsid[] = "@(#)asio.c 4.5 02/17/84";
65824Srrh #endif not lint
75824Srrh 
8594Sbill #include <stdio.h>
9594Sbill #include "as.h"
10594Sbill /*
11594Sbill  *	Block I/O routines for logical I/O concurrently in
12594Sbill  *	more than one place in the same file.
13594Sbill  */
14594Sbill int	biofd;			/* file descriptor for block I/O file */
15*16070Sralph int	biobufsize;		/* optimal block size for I/O */
16594Sbill off_t	boffset;		/* physical position in logical file */
17594Sbill BFILE	*biobufs;		/* the block I/O buffers */
18594Sbill 
19594Sbill #define	error(severity, message) \
20594Sbill 	{yyerror(message); if (severity) delexit();}
21594Sbill 
22594Sbill Flushfield(n)
23594Sbill 	register int n;
24594Sbill {
25594Sbill 	while (n>0) {
26594Sbill 		outb(bitfield);
27594Sbill 		bitfield >>= 8;
28594Sbill 		n -= 8;
29594Sbill 	}
30594Sbill 	bitoff=0;
31594Sbill 	bitfield=0;
32594Sbill }
33594Sbill 
34594Sbill /*
35648Shenry  *	Block I/O Routines
36594Sbill  */
37594Sbill bopen(bp, off)
38594Sbill 	struct biobuf *bp;
39594Sbill 	off_t	off;
40594Sbill {
41594Sbill 
42*16070Sralph 	bp->b_ptr = bp->b_buf = Calloc(1, biobufsize);
43*16070Sralph 	bp->b_nleft = biobufsize - (off % biobufsize);
44594Sbill 	bp->b_off = off;
45594Sbill 	bp->b_link = biobufs;
46594Sbill 	biobufs = bp;
47594Sbill }
48594Sbill 
49594Sbill int	bwrerror;
50594Sbill 
51594Sbill bwrite(p, cnt, bp)
52594Sbill 	register char *p;
53594Sbill 	register int cnt;
54594Sbill 	register struct biobuf *bp;
55594Sbill {
56594Sbill 	register int put;
57594Sbill 	register char *to;
58594Sbill 
59594Sbill top:
60594Sbill 	if (cnt == 0)
61594Sbill 		return;
62594Sbill 	if (bp->b_nleft) {
63594Sbill 		put = bp->b_nleft;
64594Sbill 		if (put > cnt)
65594Sbill 			put = cnt;
66594Sbill 		bp->b_nleft -= put;
67594Sbill 		to = bp->b_ptr;
685824Srrh #ifdef lint
695824Srrh 		*to = *to;
705824Srrh #endif lint
71594Sbill 		asm("movc3 r8,(r11),(r7)");
72594Sbill 		bp->b_ptr += put;
73594Sbill 		p += put;
74594Sbill 		cnt -= put;
75594Sbill 		goto top;
76594Sbill 	}
77*16070Sralph 	if (cnt >= biobufsize) {
78594Sbill 		if (bp->b_ptr != bp->b_buf)
79594Sbill 			bflush1(bp);
80*16070Sralph 		put = cnt - cnt % biobufsize;
81594Sbill 		if (boffset != bp->b_off)
825824Srrh 			(void)lseek(biofd, (long)bp->b_off, 0);
83594Sbill 		if (write(biofd, p, put) != put) {
84594Sbill 			bwrerror = 1;
85594Sbill 			error(1, "Output write error");
86594Sbill 		}
87594Sbill 		bp->b_off += put;
88594Sbill 		boffset = bp->b_off;
89594Sbill 		p += put;
90594Sbill 		cnt -= put;
91594Sbill 		goto top;
92594Sbill 	}
93594Sbill 	bflush1(bp);
94594Sbill 	goto top;
95594Sbill }
96594Sbill 
97594Sbill bflush()
98594Sbill {
99594Sbill 	register struct biobuf *bp;
100594Sbill 
101594Sbill 	if (bwrerror)
102594Sbill 		return;
103594Sbill 	for (bp = biobufs; bp; bp = bp->b_link)
104594Sbill 		bflush1(bp);
105594Sbill }
106594Sbill 
107594Sbill bflush1(bp)
108594Sbill 	register struct biobuf *bp;
109594Sbill {
110594Sbill 	register int cnt = bp->b_ptr - bp->b_buf;
111594Sbill 
112594Sbill 	if (cnt == 0)
113594Sbill 		return;
114594Sbill 	if (boffset != bp->b_off)
1155824Srrh 		(void)lseek(biofd, (long)bp->b_off, 0);
116594Sbill 	if (write(biofd, bp->b_buf, cnt) != cnt) {
117594Sbill 		bwrerror = 1;
118594Sbill 		error(1, "Output write error");
119594Sbill 	}
120594Sbill 	bp->b_off += cnt;
121594Sbill 	boffset = bp->b_off;
122594Sbill 	bp->b_ptr = bp->b_buf;
123*16070Sralph 	bp->b_nleft = biobufsize;
124594Sbill }
125594Sbill 
126594Sbill bflushc(bp, c)
127594Sbill 	register struct biobuf *bp;
128594Sbill 	char	c;
129594Sbill {
130594Sbill 	bflush1(bp);
131594Sbill 	bputc(c, bp);
132594Sbill }
133