#ifndef _UTIL_H_56cd0328_ #define _UTIL_H_56cd0328_ /* This file is in the public domain. */ #include #include /* * Assorted utility functions. */ /* * stdio output wrapper. When passed a stdio stream A, this creates * and returns another stream, B; output to B gets passed to the * callback function, which is expected to massage it and then write * the massaged version to A. * * Argument pattern: stdio_wrap_w(A,cookie,cb,flags) * * cb is called as (*cb)(A,cookie,buf,len). cookie is not used in any * other way. * * flags is a bitmask of flags. Only one flag is defined: * * SWW_CLOSE_WRAPPED * When set, this causes closing B to also close A. When * clear, closing B doesn't do anything to A. */ extern FILE *stdio_wrap_w(FILE *, void *, void (*)(FILE *, void *, const char *, int), unsigned int); #define SWW_CLOSE_WRAPPED 0x00000001 /* * Accumulator. This is like asprintf, except it returns a FILE * and * thus can accumulate output over multiple calls, and can be passed * to other routines that want a FILE *, and such. * * open_accum(strptr,lenptr) returns a FILE * open for write. When * this stream is finally closed, anything written to it is packaged * up into a string, which is stored through strptr; the number of * bytes written is stored through lenptr. If either strptr or lenptr * is nil, the corresponding thing is not stored (and, for strptr, * care is taken to not leak memory as a result). Passing nil for * both arguments is not normally useful, but may be convenient to * produce a discard-output stream. * * A \0 is always appended to the data written, and is included in the * string stored thorugh strptr, but it is not counted in the length * stored through lenptr. (This is done for the convenience of uses * which want to treat the resulting data block as a C string; uses * that treat it as a binary blob can ignore the extra \0, since it's * not included in the length.) */ extern FILE *open_accum(char **, int *); /* * Take a pointer-and-length describing a string and make a mallocked * copy of it as a \0-terminated C string. */ extern char *blk_to_nulterm(const void *, int); /* * Turn on O_NONBLOCK on a descriptor. */ extern void set_nonblock(int); /* * Easy support for non-blocking "would block" errors. This is * intended to be used as in * * switch (errno) * { case NONBLOCKING: * ... * } */ #if defined(EAGAIN) # if defined(EWOULDBLOCK) # if (EAGAIN == EWOULDBLOCK) # define NONBLOCKING EAGAIN # else # define NONBLOCKING EAGAIN: case EWOULDBLOCK # endif # else # define NONBLOCKING EAGAIN # endif #else # if defined(EWOULDBLOCK) # define NONBLOCKING EWOULDBLOCK # else # error "Neither EAGAIN nor EWOULDBLOCK is defined" # endif #endif #endif