/* This file is in the public domain. */ #include #include #include #include #include "util.h" typedef struct wrap_w_priv WRAP_W_PRIV; struct wrap_w_priv { FILE *wrapped; void *cookie; void (*proc)(FILE *, void *, const char *, int); unsigned int flags; } ; static int wrap_w_w(void *pv, const char *buf, int len) { WRAP_W_PRIV *p; p = pv; (*p->proc)(p->wrapped,p->cookie,buf,len); return(len); } static int wrap_w_c(void *pv) { WRAP_W_PRIV *p; p = pv; if (p->flags & SWW_CLOSE_WRAPPED) fclose(p->wrapped); free(pv); return(0); } FILE *stdio_wrap_w(FILE *wrapped, void *cookie, void (*proc)(FILE *, void *, const char *, int), unsigned int flags) { WRAP_W_PRIV *p; FILE *f; int e; p = malloc(sizeof(WRAP_W_PRIV)); if (! p) return(0); p->wrapped = wrapped; p->cookie = cookie; p->proc = proc; p->flags = flags; f = funopen(p,0,&wrap_w_w,0,&wrap_w_c); if (! f) { e = errno; free(p); errno = e; } return(f); } char *blk_to_nulterm(const void *buf, int len) { char *s; s = malloc(len+1); if (s) { if (len > 0) bcopy(buf,s,len); s[len] = '\0'; } return(s); }