5 badd(char *p, int *np, char *data, int ndata, int delim, int nulldelim)
10 p = realloc(p, n+ndata+1);
12 memmove(p+n, data, ndata);
14 if(n>0 && nulldelim && p[n-1]==delim)
24 Brdstr(Biobuf *bp, int delim, int nulldelim)
35 if(bp->state != Bractive) {
36 if(bp->state == Bracteof)
44 * first try in remainder of buffer (gbuf doesn't change)
46 ip = (char*)bp->ebuf - i;
47 ep = memchr(ip, delim, i);
51 return badd(nil, &bp->rdline, ip, j, delim, nulldelim);
55 * copy data to beginning of buffer
58 memmove(bp->bbuf, ip, i);
62 * append to buffer looking for the delim
66 ip = (char*)bp->bbuf + i;
67 while(i < bp->bsize) {
68 j = read(bp->fid, ip, bp->bsize-i);
73 * end of file but no delim. pretend we got a delim
74 * by making the delim \0 and smashing it with nulldelim.
80 *ep = delim; /* there will be room for this */
83 ep = memchr(ip, delim, j);
89 * copy back up and reset everything
91 ip = (char*)bp->ebuf - i;
93 memmove(ip, bp->bbuf, i);
94 bp->gbuf = (unsigned char*)ip;
96 j = (ep - (char*)bp->bbuf) + 1;
98 return badd(p, &bp->rdline, ip, j, delim, nulldelim);
104 * full buffer without finding; add to user string and continue
106 p = badd(p, &bp->rdline, (char*)bp->bbuf, bp->bsize, 0, 0);
111 return 0; /* never happens */