xref: /csrg-svn/old/as.vax/asio.c (revision 19823)
15824Srrh /*
2*19823Sdist  * Copyright (c) 1982 Regents of the University of California.
3*19823Sdist  * All rights reserved.  The Berkeley software License Agreement
4*19823Sdist  * specifies the terms and conditions for redistribution.
55824Srrh  */
6*19823Sdist 
75824Srrh #ifndef lint
8*19823Sdist static char sccsid[] = "@(#)asio.c	5.1 (Berkeley) 04/30/85";
95824Srrh #endif not lint
105824Srrh 
11594Sbill #include <stdio.h>
12594Sbill #include "as.h"
13594Sbill /*
14594Sbill  *	Block I/O routines for logical I/O concurrently in
15594Sbill  *	more than one place in the same file.
16594Sbill  */
17594Sbill int	biofd;			/* file descriptor for block I/O file */
1816070Sralph int	biobufsize;		/* optimal block size for I/O */
19594Sbill off_t	boffset;		/* physical position in logical file */
20594Sbill BFILE	*biobufs;		/* the block I/O buffers */
21594Sbill 
22594Sbill #define	error(severity, message) \
23594Sbill 	{yyerror(message); if (severity) delexit();}
24594Sbill 
Flushfield(n)25594Sbill Flushfield(n)
26594Sbill 	register int n;
27594Sbill {
28594Sbill 	while (n>0) {
29594Sbill 		outb(bitfield);
30594Sbill 		bitfield >>= 8;
31594Sbill 		n -= 8;
32594Sbill 	}
33594Sbill 	bitoff=0;
34594Sbill 	bitfield=0;
35594Sbill }
36594Sbill 
37594Sbill /*
38648Shenry  *	Block I/O Routines
39594Sbill  */
40594Sbill bopen(bp, off)
41594Sbill 	struct biobuf *bp;
42594Sbill 	off_t	off;
43594Sbill {
44594Sbill 
4516070Sralph 	bp->b_ptr = bp->b_buf = Calloc(1, biobufsize);
4616070Sralph 	bp->b_nleft = biobufsize - (off % biobufsize);
47594Sbill 	bp->b_off = off;
48594Sbill 	bp->b_link = biobufs;
49594Sbill 	biobufs = bp;
50594Sbill }
51594Sbill 
52594Sbill int	bwrerror;
53594Sbill 
bwrite(p,cnt,bp)54594Sbill bwrite(p, cnt, bp)
55594Sbill 	register char *p;
56594Sbill 	register int cnt;
57594Sbill 	register struct biobuf *bp;
58594Sbill {
59594Sbill 	register int put;
60594Sbill 	register char *to;
61594Sbill 
62594Sbill top:
63594Sbill 	if (cnt == 0)
64594Sbill 		return;
65594Sbill 	if (bp->b_nleft) {
66594Sbill 		put = bp->b_nleft;
67594Sbill 		if (put > cnt)
68594Sbill 			put = cnt;
69594Sbill 		bp->b_nleft -= put;
70594Sbill 		to = bp->b_ptr;
715824Srrh #ifdef lint
725824Srrh 		*to = *to;
735824Srrh #endif lint
74594Sbill 		asm("movc3 r8,(r11),(r7)");
75594Sbill 		bp->b_ptr += put;
76594Sbill 		p += put;
77594Sbill 		cnt -= put;
78594Sbill 		goto top;
79594Sbill 	}
8016070Sralph 	if (cnt >= biobufsize) {
81594Sbill 		if (bp->b_ptr != bp->b_buf)
82594Sbill 			bflush1(bp);
8316070Sralph 		put = cnt - cnt % biobufsize;
84594Sbill 		if (boffset != bp->b_off)
855824Srrh 			(void)lseek(biofd, (long)bp->b_off, 0);
86594Sbill 		if (write(biofd, p, put) != put) {
87594Sbill 			bwrerror = 1;
88594Sbill 			error(1, "Output write error");
89594Sbill 		}
90594Sbill 		bp->b_off += put;
91594Sbill 		boffset = bp->b_off;
92594Sbill 		p += put;
93594Sbill 		cnt -= put;
94594Sbill 		goto top;
95594Sbill 	}
96594Sbill 	bflush1(bp);
97594Sbill 	goto top;
98594Sbill }
99594Sbill 
bflush()100594Sbill bflush()
101594Sbill {
102594Sbill 	register struct biobuf *bp;
103594Sbill 
104594Sbill 	if (bwrerror)
105594Sbill 		return;
106594Sbill 	for (bp = biobufs; bp; bp = bp->b_link)
107594Sbill 		bflush1(bp);
108594Sbill }
109594Sbill 
bflush1(bp)110594Sbill bflush1(bp)
111594Sbill 	register struct biobuf *bp;
112594Sbill {
113594Sbill 	register int cnt = bp->b_ptr - bp->b_buf;
114594Sbill 
115594Sbill 	if (cnt == 0)
116594Sbill 		return;
117594Sbill 	if (boffset != bp->b_off)
1185824Srrh 		(void)lseek(biofd, (long)bp->b_off, 0);
119594Sbill 	if (write(biofd, bp->b_buf, cnt) != cnt) {
120594Sbill 		bwrerror = 1;
121594Sbill 		error(1, "Output write error");
122594Sbill 	}
123594Sbill 	bp->b_off += cnt;
124594Sbill 	boffset = bp->b_off;
125594Sbill 	bp->b_ptr = bp->b_buf;
12616070Sralph 	bp->b_nleft = biobufsize;
127594Sbill }
128594Sbill 
bflushc(bp,c)129594Sbill bflushc(bp, c)
130594Sbill 	register struct biobuf *bp;
131594Sbill 	char	c;
132594Sbill {
133594Sbill 	bflush1(bp);
134594Sbill 	bputc(c, bp);
135594Sbill }
136