unix/fiss

lib/libbio/bgetc.c in master
Repositories | Summary | Log | Files | LICENSE

bgetc.c (959B) download


 1#include "lib9.h"
 2
 3#include <bio.h>
 4
 5int Bgetc(Biobuf* bp) {
 6	int i;
 7
 8loop:
 9	i = bp->icount;
10	if (i != 0) {
11		bp->icount = i + 1;
12		return bp->ebuf[i];
13	}
14	if (bp->state != Bractive) {
15		if (bp->state == Bracteof)
16			bp->state = Bractive;
17		return Beof;
18	}
19	/*
20	 * get next buffer, try to keep Bungetsize
21	 * characters pre-catenated from the previous
22	 * buffer to allow that many ungets.
23	 */
24	memmove(bp->bbuf - Bungetsize, bp->ebuf - Bungetsize, Bungetsize);
25	i        = read(bp->fid, bp->bbuf, bp->bsize);
26	bp->gbuf = bp->bbuf;
27	if (i <= 0) {
28		bp->state = Bracteof;
29		if (i < 0)
30			bp->state = Binactive;
31		return Beof;
32	}
33	if (i < bp->bsize) {
34		memmove(bp->ebuf - i - Bungetsize, bp->bbuf - Bungetsize, i + Bungetsize);
35		bp->gbuf = bp->ebuf - i;
36	}
37	bp->icount = -i;
38	bp->offset += i;
39	goto loop;
40}
41
42int Bungetc(Biobuf* bp) {
43
44	if (bp->state == Bracteof)
45		bp->state = Bractive;
46	if (bp->state != Bractive)
47		return Beof;
48	bp->icount--;
49	return 1;
50}