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 bfl_config(struct fastbuf *f UNUSED, uns item, int value UNUSED)
47 case BCONFIG_CAN_OVERWRITE:
55 bopen_limited_fd(int fd, uns buflen, uns limit)
57 struct fb_limfd *F = xmalloc(sizeof(struct fb_limfd) + buflen);
58 struct fastbuf *f = &F->fb;
61 f->buffer = (char *)(F+1);
62 f->bptr = f->bstop = f->buffer;
63 f->bufend = f->buffer + buflen;
64 f->name = "limited-fd";
67 f->refill = bfl_refill;
69 f->config = bfl_config;
75 int main(int argc, char **argv)
77 struct fastbuf *f = bopen_limited_fd(0, 3, 13);
78 struct fastbuf *o = bfdopen_shared(1, 16);
80 while ((c = bgetc(f)) >= 0)