6 badd(char *oldp, int *np, char *data, int ndata, int delim, int nulldelim)
12 p = realloc(oldp, n+ndata+1);
14 memmove(p+n, data, ndata);
16 if(n>0 && nulldelim && p[n-1]==delim)
27 Brdstr(Biobufhdr *bp, int delim, int nulldelim)
38 if(bp->state != Bractive) {
39 if(bp->state == Bracteof)
47 * first try in remainder of buffer (gbuf doesn't change)
49 ip = (char*)bp->ebuf - i;
50 ep = memchr(ip, delim, i);
54 p = badd(nil, &bp->rdline, ip, j, delim, nulldelim);
59 * copy data to beginning of buffer
62 memmove(bp->bbuf, ip, i);
66 * append to buffer looking for the delim
70 ip = (char*)bp->bbuf + i;
71 while(i < bp->bsize) {
72 j = read(bp->fid, ip, bp->bsize-i);
74 Berror(bp, "read error: %r");
79 * end of file but no delim. pretend we got a delim
80 * by making the delim \0 and smashing it with nulldelim.
86 *ep = delim; /* there will be room for this */
89 ep = memchr(ip, delim, j);
95 * copy back up and reset everything
97 ip = (char*)bp->ebuf - i;
99 memmove(ip, bp->bbuf, i);
100 bp->gbuf = (uchar*)ip;
102 j = (ep - (char*)bp->bbuf) + 1;
104 p = badd(p, &bp->rdline, ip, j, delim, nulldelim);
111 * full buffer without finding; add to user string and continue
113 p = badd(p, &bp->rdline, (char*)bp->bbuf, bp->bsize, 0, 0);
119 setmalloctag(p, getcallerpc(&bp));