2 * Sherlock Library -- Fast Buffered Input on Limited File Descriptors
4 * (c) 2003 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/fastbuf.h"
18 int fd; /* File descriptor */
21 #define FB_LIMFD(f) ((struct fb_limfd *)(f)->is_fastbuf)
24 bfl_refill(struct fastbuf *f)
26 int max = MIN(FB_LIMFD(f)->limit - f->pos, f->bufend - f->buffer);
27 int l = read(FB_LIMFD(f)->fd, f->buffer, max);
29 die("Error reading %s: %m", f->name);
31 f->bstop = f->buffer + l;
37 bfl_close(struct fastbuf *f)
43 bopen_limited_fd(int fd, uns buflen, uns limit)
45 struct fb_limfd *F = xmalloc(sizeof(struct fb_limfd) + buflen);
46 struct fastbuf *f = &F->fb;
49 f->buffer = (char *)(F+1);
50 f->bptr = f->bstop = f->buffer;
51 f->bufend = f->buffer + buflen;
52 f->name = "limited-fd";
55 f->refill = bfl_refill;
57 f->can_overwrite_buffer = 2;
63 int main(int argc, char **argv)
65 struct fastbuf *f = bopen_limited_fd(0, 3, 13);
66 struct fastbuf *o = bfdopen_shared(1, 16);
68 while ((c = bgetc(f)) >= 0)