#include #include #include #include #include "stdio-util.h" typedef struct allocpriv ALLOCPRIV; struct allocpriv { char *b; int l; char **sp; int *lp; } ; static int alloc_w(void *pv, const char *data, int len) { ALLOCPRIV *p; char *new; p = pv; if (p->sp) { new = realloc(p->b,p->l+len); if (! new) return(-1); p->b = new; bcopy(data,p->b+p->l,len); } p->l += len; return(len); } static int alloc_c(void *pv) { ALLOCPRIV *p; p = pv; if (p->lp) *p->lp = p->l; alloc_w(pv,"",1); if (p->sp) *p->sp = p->b; free(p); return(0); } FILE *fopenalloc(char **sp, int *lp) { ALLOCPRIV *p; FILE *f; int e; p = malloc(sizeof(ALLOCPRIV)); if (! p) return(0); p->b = 0; p->l = 0; p->sp = sp; p->lp = lp; f = funopen(p,0,&alloc_w,0,&alloc_c); if (! f) { e = errno; free(p); errno = e; return(0); } return(f); }