6 badd(char *p, int *np, char *data, int ndata, int delim, int nulldelim)
11 p = realloc(p, n+ndata+1);
13 memmove(p+n, data, ndata);
15 if(n>0 && nulldelim && p[n-1]==delim)
25 Brdstr(Biobufhdr *bp, int delim, int nulldelim)
36 if(bp->state != Bractive) {
37 if(bp->state == Bracteof)
45 * first try in remainder of buffer (gbuf doesn't change)
47 ip = (char*)bp->ebuf - i;
48 ep = memchr(ip, delim, i);
52 return badd(nil, &bp->rdline, ip, j, delim, nulldelim);
56 * copy data to beginning of buffer
59 memmove(bp->bbuf, ip, i);
63 * append to buffer looking for the delim
67 ip = (char*)bp->bbuf + i;
68 while(i < bp->bsize) {
69 j = read(bp->fid, ip, bp->bsize-i);
71 Berror(bp, "read error: %r");
76 * end of file but no delim. pretend we got a delim
77 * by making the delim \0 and smashing it with nulldelim.
83 *ep = delim; /* there will be room for this */
86 ep = memchr(ip, delim, j);
92 * copy back up and reset everything
94 ip = (char*)bp->ebuf - i;
96 memmove(ip, bp->bbuf, i);
97 bp->gbuf = (uchar*)ip;
99 j = (ep - (char*)bp->bbuf) + 1;
101 return badd(p, &bp->rdline, ip, j, delim, nulldelim);
107 * full buffer without finding; add to user string and continue
109 p = badd(p, &bp->rdline, (char*)bp->bbuf, bp->bsize, 0, 0);