1*594Sbill /* Coypright (c) 1980 Regents of the University of California */ 2*594Sbill static char sccsid[] = "@(#)asio.c 4.1 08/13/80"; 3*594Sbill #include <stdio.h> 4*594Sbill #include <sys/types.h> 5*594Sbill #include "as.h" 6*594Sbill /* 7*594Sbill * Block I/O routines for logical I/O concurrently in 8*594Sbill * more than one place in the same file. 9*594Sbill */ 10*594Sbill int biofd; /* file descriptor for block I/O file */ 11*594Sbill off_t boffset; /* physical position in logical file */ 12*594Sbill BFILE *biobufs; /* the block I/O buffers */ 13*594Sbill 14*594Sbill #define error(severity, message) \ 15*594Sbill {yyerror(message); if (severity) delexit();} 16*594Sbill 17*594Sbill Flushfield(n) 18*594Sbill register int n; 19*594Sbill { 20*594Sbill while (n>0) { 21*594Sbill outb(bitfield); 22*594Sbill bitfield >>= 8; 23*594Sbill n -= 8; 24*594Sbill } 25*594Sbill bitoff=0; 26*594Sbill bitfield=0; 27*594Sbill } 28*594Sbill 29*594Sbill #ifdef ASFWRITE 30*594Sbill /* 31*594Sbill * This is our version of fwrite... 32*594Sbill * Hacked out fast version of fwrite that 33*594Sbill * doesn't iterate over each and every character; 34*594Sbill * We poke directly into the buffer area, and move things 35*594Sbill * with a movc3. 36*594Sbill */ 37*594Sbill fwrite(p, n, m, f) 38*594Sbill register char *p; 39*594Sbill int n, m; 40*594Sbill register FILE *f; 41*594Sbill { 42*594Sbill register int cnt = n * m; 43*594Sbill register int put; 44*594Sbill register char *to; 45*594Sbill 46*594Sbill top: 47*594Sbill if (cnt == 0) 48*594Sbill return; 49*594Sbill if (f->_cnt) { 50*594Sbill put = f->_cnt; 51*594Sbill if (put > cnt) 52*594Sbill put = cnt; 53*594Sbill f->_cnt -= put; 54*594Sbill to = f->_ptr; 55*594Sbill asm("movc3 r8,(r11),(r7)"); 56*594Sbill f->_ptr += put; 57*594Sbill p += put; 58*594Sbill cnt -= put; 59*594Sbill goto top; 60*594Sbill } 61*594Sbill if (cnt >= BUFSIZ) { 62*594Sbill fflush(f); 63*594Sbill put = cnt - cnt % BUFSIZ; 64*594Sbill if (write(f->_file, p, put) != put) 65*594Sbill error(1, "Output write error in fwrite"); 66*594Sbill p += put; 67*594Sbill cnt -= put; 68*594Sbill goto top; 69*594Sbill } 70*594Sbill _flsbuf(*p++, f); 71*594Sbill --cnt; 72*594Sbill goto top; 73*594Sbill } 74*594Sbill 75*594Sbill /* 76*594Sbill * This has been stolen from the usual place... 77*594Sbill * It is put here so that the loader doesn't complain 78*594Sbill * about multiple definitions in the archived object module. 79*594Sbill * 80*594Sbill * archived in: /lib/libc.a 81*594Sbill * object module from: /usr/src/libc/stdio/rdwr.c 82*594Sbill */ 83*594Sbill fread(ptr, size, count, iop) 84*594Sbill unsigned size, count; 85*594Sbill register char *ptr; 86*594Sbill register FILE *iop; 87*594Sbill { 88*594Sbill register c; 89*594Sbill unsigned ndone, s; 90*594Sbill 91*594Sbill ndone = 0; 92*594Sbill if (size) 93*594Sbill for (; ndone<count; ndone++) { 94*594Sbill s = size; 95*594Sbill do { 96*594Sbill if ((c = getc(iop)) >= 0) 97*594Sbill *ptr++ = c; 98*594Sbill else 99*594Sbill return(ndone); 100*594Sbill } while (--s); 101*594Sbill } 102*594Sbill return(ndone); 103*594Sbill } 104*594Sbill #endif ASFWRITE 105*594Sbill 106*594Sbill bopen(bp, off) 107*594Sbill struct biobuf *bp; 108*594Sbill off_t off; 109*594Sbill { 110*594Sbill 111*594Sbill bp->b_ptr = bp->b_buf; 112*594Sbill bp->b_nleft = BUFSIZ - off % BUFSIZ; 113*594Sbill bp->b_off = off; 114*594Sbill bp->b_link = biobufs; 115*594Sbill biobufs = bp; 116*594Sbill } 117*594Sbill 118*594Sbill int bwrerror; 119*594Sbill 120*594Sbill bwrite(p, cnt, bp) 121*594Sbill register char *p; 122*594Sbill register int cnt; 123*594Sbill register struct biobuf *bp; 124*594Sbill { 125*594Sbill register int put; 126*594Sbill register char *to; 127*594Sbill 128*594Sbill top: 129*594Sbill if (cnt == 0) 130*594Sbill return; 131*594Sbill if (bp->b_nleft) { 132*594Sbill put = bp->b_nleft; 133*594Sbill if (put > cnt) 134*594Sbill put = cnt; 135*594Sbill bp->b_nleft -= put; 136*594Sbill to = bp->b_ptr; 137*594Sbill asm("movc3 r8,(r11),(r7)"); 138*594Sbill bp->b_ptr += put; 139*594Sbill p += put; 140*594Sbill cnt -= put; 141*594Sbill goto top; 142*594Sbill } 143*594Sbill if (cnt >= BUFSIZ) { 144*594Sbill if (bp->b_ptr != bp->b_buf) 145*594Sbill bflush1(bp); 146*594Sbill put = cnt - cnt % BUFSIZ; 147*594Sbill if (boffset != bp->b_off) 148*594Sbill lseek(biofd, bp->b_off, 0); 149*594Sbill if (write(biofd, p, put) != put) { 150*594Sbill bwrerror = 1; 151*594Sbill error(1, "Output write error"); 152*594Sbill } 153*594Sbill bp->b_off += put; 154*594Sbill boffset = bp->b_off; 155*594Sbill p += put; 156*594Sbill cnt -= put; 157*594Sbill goto top; 158*594Sbill } 159*594Sbill bflush1(bp); 160*594Sbill goto top; 161*594Sbill } 162*594Sbill 163*594Sbill bflush() 164*594Sbill { 165*594Sbill register struct biobuf *bp; 166*594Sbill 167*594Sbill if (bwrerror) 168*594Sbill return; 169*594Sbill for (bp = biobufs; bp; bp = bp->b_link) 170*594Sbill bflush1(bp); 171*594Sbill } 172*594Sbill 173*594Sbill bflush1(bp) 174*594Sbill register struct biobuf *bp; 175*594Sbill { 176*594Sbill register int cnt = bp->b_ptr - bp->b_buf; 177*594Sbill 178*594Sbill if (cnt == 0) 179*594Sbill return; 180*594Sbill if (boffset != bp->b_off) 181*594Sbill lseek(biofd, bp->b_off, 0); 182*594Sbill if (write(biofd, bp->b_buf, cnt) != cnt) { 183*594Sbill bwrerror = 1; 184*594Sbill error(1, "Output write error"); 185*594Sbill } 186*594Sbill bp->b_off += cnt; 187*594Sbill boffset = bp->b_off; 188*594Sbill bp->b_ptr = bp->b_buf; 189*594Sbill bp->b_nleft = BUFSIZ; 190*594Sbill } 191*594Sbill 192*594Sbill bflushc(bp, c) 193*594Sbill register struct biobuf *bp; 194*594Sbill char c; 195*594Sbill { 196*594Sbill 197*594Sbill bflush1(bp); 198*594Sbill bputc(c, bp); 199*594Sbill } 200