64 lines
1.3 KiB
C
64 lines
1.3 KiB
C
/*
|
|
* This file is part of the UCB release of Plan 9. It is subject to the license
|
|
* terms in the LICENSE file found in the top-level directory of this
|
|
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
|
|
* part of the UCB release of Plan 9, including this file, may be copied,
|
|
* modified, propagated, or distributed except according to the terms contained
|
|
* in the LICENSE file.
|
|
*/
|
|
|
|
#include <u.h>
|
|
#include <lib9.h>
|
|
#include <bio.h>
|
|
|
|
int
|
|
Bgetc(Biobufhdr *bp)
|
|
{
|
|
int i;
|
|
|
|
loop:
|
|
i = bp->icount;
|
|
if(i != 0) {
|
|
bp->icount = i+1;
|
|
return bp->ebuf[i];
|
|
}
|
|
if(bp->state != Bractive) {
|
|
if(bp->state == Bracteof)
|
|
bp->state = Bractive;
|
|
return Beof;
|
|
}
|
|
/*
|
|
* get next buffer, try to keep Bungetsize
|
|
* characters pre-catenated from the previous
|
|
* buffer to allow that many ungets.
|
|
*/
|
|
memmove(bp->bbuf-Bungetsize, bp->ebuf-Bungetsize, Bungetsize);
|
|
i = read(bp->fid, bp->bbuf, bp->bsize);
|
|
bp->gbuf = bp->bbuf;
|
|
if(i <= 0) {
|
|
bp->state = Bracteof;
|
|
if(i < 0)
|
|
bp->state = Binactive;
|
|
return Beof;
|
|
}
|
|
if(i < bp->bsize) {
|
|
memmove(bp->ebuf-i-Bungetsize, bp->bbuf-Bungetsize, i+Bungetsize);
|
|
bp->gbuf = bp->ebuf-i;
|
|
}
|
|
bp->icount = -i;
|
|
bp->offset += i;
|
|
goto loop;
|
|
}
|
|
|
|
int
|
|
Bungetc(Biobufhdr *bp)
|
|
{
|
|
|
|
if(bp->state == Bracteof)
|
|
bp->state = Bractive;
|
|
if(bp->state != Bractive)
|
|
return Beof;
|
|
bp->icount--;
|
|
return 1;
|
|
}
|