xref: /original-bsd/old/as.tahoe/asio.c (revision 1b4ef7de)
1 /*
2  *	Copyright (c) 1982 Regents of the University of California
3  */
4 #ifndef lint
5 static char sccsid[] = "@(#)asio.c 4.4 2/14/82";
6 #endif not lint
7 
8 #include <stdio.h>
9 #include "as.h"
10 /*
11  *	Block I/O routines for logical I/O concurrently in
12  *	more than one place in the same file.
13  */
14 int	biofd;			/* file descriptor for block I/O file */
15 off_t	boffset;		/* physical position in logical file */
16 BFILE	*biobufs;		/* the block I/O buffers */
17 static char hexdigit [] =
18 	"0123456789abcdef";
19 
20 #define	error(severity, message) \
21 	{yyerror(message); if (severity) delexit();}
22 
23 Flushfield(n)
24 	register int n;
25 {
26 	while (n>0) {
27 		n -= 8;
28 		outb((bitfield >> n) & 0xFF);
29 		if (liston && (passno == 2))
30 			byte_out((bitfield >> n) & 0xFF);
31 	}
32 	bitoff=0;
33 	bitfield=0;
34 }
35 
36 /*
37  *	Block I/O Routines
38  */
39 bopen(bp, off)
40 	struct biobuf *bp;
41 	off_t	off;
42 {
43 
44 	bp->b_ptr = bp->b_buf;
45 	bp->b_nleft = BUFSIZ - off % BUFSIZ;
46 	bp->b_off = off;
47 	bp->b_link = biobufs;
48 	biobufs = bp;
49 }
50 
51 int	bwrerror;
52 
53 bwrite(p, cnt, bp)
54 	register char *p;
55 	register int cnt;
56 	register struct biobuf *bp;
57 {
58 	register int put;
59 	register char *to;
60 
61 top:
62 	if (cnt == 0)
63 		return;
64 	if (bp->b_nleft) {
65 		put = bp->b_nleft;
66 		if (put > cnt)
67 			put = cnt;
68 		bp->b_nleft -= put;
69 		to = bp->b_ptr;
70 #ifdef lint
71 		*to = *to;
72 #endif lint
73 		movblk (p, to, put);
74 		bp->b_ptr += put;
75 		p += put;
76 		cnt -= put;
77 		goto top;
78 	}
79 	if (cnt >= BUFSIZ) {
80 		if (bp->b_ptr != bp->b_buf)
81 			bflush1(bp);
82 		put = cnt - cnt % BUFSIZ;
83 		if (boffset != bp->b_off)
84 			(void)lseek(biofd, (long)bp->b_off, 0);
85 		if (write(biofd, p, put) != put) {
86 			bwrerror = 1;
87 			error(1, "Output write error");
88 		}
89 		bp->b_off += put;
90 		boffset = bp->b_off;
91 		p += put;
92 		cnt -= put;
93 		goto top;
94 	}
95 	bflush1(bp);
96 	goto top;
97 }
98 
99 bflush()
100 {
101 	register struct biobuf *bp;
102 
103 	if (bwrerror)
104 		return;
105 	for (bp = biobufs; bp; bp = bp->b_link)
106 		bflush1(bp);
107 }
108 
109 bflush1(bp)
110 	register struct biobuf *bp;
111 {
112 	register int cnt = bp->b_ptr - bp->b_buf;
113 
114 	if (cnt == 0)
115 		return;
116 	if (boffset != bp->b_off)
117 		(void)lseek(biofd, (long)bp->b_off, 0);
118 	if (write(biofd, bp->b_buf, cnt) != cnt) {
119 		bwrerror = 1;
120 		error(1, "Output write error");
121 	}
122 	bp->b_off += cnt;
123 	boffset = bp->b_off;
124 	bp->b_ptr = bp->b_buf;
125 	bp->b_nleft = BUFSIZ;
126 }
127 
128 bflushc(bp, c)
129 	register struct biobuf *bp;
130 	char	c;
131 {
132 	bflush1(bp);
133 	bputc(c, bp);
134 }
135 
136 movblk (src, dest, cnt)
137 char *src, *dest;
138 
139 {
140 	while (cnt--)
141 		*dest++ = *src++;
142 }
143 
144 byte_out (byte)
145 u_char byte;
146 {
147 	*layoutpos++ = hexdigit [(byte >> 4) & 0xf];
148 	*layoutpos++ = hexdigit [byte & 0xf];
149 }
150 
151 word_out (word)
152 u_short (word);
153 {
154 	union
155 	{
156 		u_short	_word;
157 		char	_ch[2];
158 	} charr;
159 
160 	charr._word = word;
161 	byte_out (charr._ch[0]);
162 	byte_out (charr._ch[1]);
163 }
164 
165 long_out (longword)
166 u_int longword;
167 {
168 	union
169 	{
170 		u_int	_int;
171 		char	_ch[4];
172 	} charr;
173 
174 	charr._int = longword;
175 	byte_out (charr._ch[0]);
176 	byte_out (charr._ch[1]);
177 	byte_out (charr._ch[2]);
178 	byte_out (charr._ch[3]);
179 }
180