/* This file is in the public domain. */ #include #include #include #include #include #include "stdio-util.h" typedef struct allocblk ALLOCBLK; typedef struct rstrblk RSTRBLK; struct allocblk { char **bufp; int *lenp; char *buf; int len; } ; struct rstrblk { const char *buf; int len; off_t ptr; }; static int alloc_w(void *av, const char *b, int l) { ALLOCBLK *a; a = av; if (a->bufp) { a->buf = realloc(a->buf,a->len+l); bcopy(b,a->buf+a->len,l); } a->len += l; return(l); } static int alloc_c(void *av) { ALLOCBLK *a; a = av; if (a->bufp) *a->bufp = a->buf; if (a->lenp) *a->lenp = a->len; free(a); return(0); } FILE *fopen_alloc(char **bufp, int *lenp) { FILE *f; ALLOCBLK *a; a = malloc(sizeof(ALLOCBLK)); if (a == 0) return(0); a->bufp = bufp; a->lenp = lenp; a->buf = 0; a->len = 0; f = funopen(a,0,&alloc_w,0,&alloc_c); if (f == 0) free(a); return(f); } static int rstr_r(void *bv, char *buf, int len) { RSTRBLK *b; int n; b = bv; if (b->ptr < 0) { errno = EINVAL; return(-1); } else if (b->ptr >= b->len) { return(0); } n = b->len - b->ptr; if (n > len) n = len; bcopy(b->buf+b->ptr,buf,n); b->ptr += n; return(n); } static int rstr_c(void *bv) { free(bv); return(0); } FILE *fopen_rstr(const char *s, int l) { RSTRBLK *b; FILE *f; b = malloc(sizeof(RSTRBLK)); if (b == 0) return(0); if (l < 0) l = strlen(s); b->buf = s; b->len = l; b->ptr = 0; f = funopen(b,&rstr_r,0,0,&rstr_c); if (f == 0) free(b); return(f); } static int null_w(void *cookie __attribute__((__unused__)), const char *buf __attribute__((__unused__)), int len) { return(len); } FILE *fopen_null(void) { return(funopen(0,0,&null_w,0,0)); } void print_lower(FILE *to, const char *s) { for (;*s;s++) putc(tolower((unsigned char)*s),to); } int peekc(FILE *f) { int c; c = getc(f); if (c != EOF) ungetc(c,f); return(c); }