BSD 4_3 release
[unix-history] / usr / src / bin / as / asio.c
CommitLineData
f70ab843 1/*
bcf1365c
DF
2 * Copyright (c) 1982 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
f70ab843 5 */
bcf1365c 6
f70ab843 7#ifndef lint
95f51977 8static char sccsid[] = "@(#)asio.c 5.1 (Berkeley) 4/30/85";
f70ab843
RH
9#endif not lint
10
8ad3c13d 11#include <stdio.h>
8ad3c13d
BJ
12#include "as.h"
13/*
14 * Block I/O routines for logical I/O concurrently in
15 * more than one place in the same file.
16 */
17int biofd; /* file descriptor for block I/O file */
8192c7f0 18int biobufsize; /* optimal block size for I/O */
8ad3c13d
BJ
19off_t boffset; /* physical position in logical file */
20BFILE *biobufs; /* the block I/O buffers */
21
22#define error(severity, message) \
23 {yyerror(message); if (severity) delexit();}
24
25Flushfield(n)
26 register int n;
27{
28 while (n>0) {
29 outb(bitfield);
30 bitfield >>= 8;
31 n -= 8;
32 }
33 bitoff=0;
34 bitfield=0;
35}
36
8ad3c13d 37/*
bef6dadc 38 * Block I/O Routines
8ad3c13d 39 */
8ad3c13d
BJ
40bopen(bp, off)
41 struct biobuf *bp;
42 off_t off;
43{
44
8192c7f0
RC
45 bp->b_ptr = bp->b_buf = Calloc(1, biobufsize);
46 bp->b_nleft = biobufsize - (off % biobufsize);
8ad3c13d
BJ
47 bp->b_off = off;
48 bp->b_link = biobufs;
49 biobufs = bp;
50}
51
52int bwrerror;
53
54bwrite(p, cnt, bp)
55 register char *p;
56 register int cnt;
57 register struct biobuf *bp;
58{
59 register int put;
60 register char *to;
61
62top:
63 if (cnt == 0)
64 return;
65 if (bp->b_nleft) {
66 put = bp->b_nleft;
67 if (put > cnt)
68 put = cnt;
69 bp->b_nleft -= put;
70 to = bp->b_ptr;
f70ab843
RH
71#ifdef lint
72 *to = *to;
73#endif lint
8ad3c13d
BJ
74 asm("movc3 r8,(r11),(r7)");
75 bp->b_ptr += put;
76 p += put;
77 cnt -= put;
78 goto top;
79 }
8192c7f0 80 if (cnt >= biobufsize) {
8ad3c13d
BJ
81 if (bp->b_ptr != bp->b_buf)
82 bflush1(bp);
8192c7f0 83 put = cnt - cnt % biobufsize;
8ad3c13d 84 if (boffset != bp->b_off)
f70ab843 85 (void)lseek(biofd, (long)bp->b_off, 0);
8ad3c13d
BJ
86 if (write(biofd, p, put) != put) {
87 bwrerror = 1;
88 error(1, "Output write error");
89 }
90 bp->b_off += put;
91 boffset = bp->b_off;
92 p += put;
93 cnt -= put;
94 goto top;
95 }
96 bflush1(bp);
97 goto top;
98}
99
100bflush()
101{
102 register struct biobuf *bp;
103
104 if (bwrerror)
105 return;
106 for (bp = biobufs; bp; bp = bp->b_link)
107 bflush1(bp);
108}
109
110bflush1(bp)
111 register struct biobuf *bp;
112{
113 register int cnt = bp->b_ptr - bp->b_buf;
114
115 if (cnt == 0)
116 return;
117 if (boffset != bp->b_off)
f70ab843 118 (void)lseek(biofd, (long)bp->b_off, 0);
8ad3c13d
BJ
119 if (write(biofd, bp->b_buf, cnt) != cnt) {
120 bwrerror = 1;
121 error(1, "Output write error");
122 }
123 bp->b_off += cnt;
124 boffset = bp->b_off;
125 bp->b_ptr = bp->b_buf;
8192c7f0 126 bp->b_nleft = biobufsize;
8ad3c13d
BJ
127}
128
129bflushc(bp, c)
130 register struct biobuf *bp;
131 char c;
132{
8ad3c13d
BJ
133 bflush1(bp);
134 bputc(c, bp);
135}