#include #include #include #include /* * stdio utilities. There's really not much to say here beyond what * the comments in stdio-util.h say. */ #include "stdio-util.h" typedef struct swpriv SWPRIV; typedef struct malpriv MALPRIV; struct swpriv { char *buf; int len; int at; } ; struct malpriv { char *buf; int len; char **loc; } ; static int sw_write(void *pv, const char *buf, int len) { SWPRIV *p; int n; p = pv; n = p->len - p->at; if (n > len) n = len; if (n < 1) return(0); bcopy(buf,p->buf+p->at,n); p->at += n; return(n); } static int sw_close(void *pv) { SWPRIV *p; p = pv; p->buf[p->at] = '\0'; free(pv); return(0); } FILE *fopensw(char *s, int len) { SWPRIV *p; FILE *f; p = malloc(sizeof(SWPRIV)); if (p == 0) return(0); if (len < 1) { errno = EINVAL; return(0); } f = funopen(p,0,sw_write,0,sw_close); if (f == 0) { int e; e = errno; free(p); errno = e; return(0); } p->buf = s; p->len = len - 1; p->at = 0; return(f); } static int mal_write(void *pv, const char *buf, int len) { MALPRIV *p; p = pv; p->buf = realloc(p->buf,p->len+len); bcopy(buf,p->buf+p->len,len); p->len += len; return(len); } static int mal_close(void *pv) { MALPRIV *p; mal_write(pv,"",1); p = pv; *p->loc = p->buf; free(pv); return(0); } FILE *fopenmalloc(char **sp) { MALPRIV *p; FILE *f; p = malloc(sizeof(MALPRIV)); if (p == 0) return(0); f = funopen(p,0,mal_write,0,mal_close); if (f == 0) { int e; e = errno; free(p); errno = e; return(0); } p->buf = 0; p->len = 0; p->loc = sp; return(f); }