From: Fredrik Tolf Date: Thu, 27 Feb 2014 08:50:06 +0000 (+0100) Subject: lib: Added a BSD mtio implementation. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=2a619a21d4af6cbc5033f987b2aa25ef2b749600;p=ashd.git lib: Added a BSD mtio implementation. --- diff --git a/configure.in b/configure.in index 1242e84..1ddf173 100644 --- a/configure.in +++ b/configure.in @@ -22,15 +22,21 @@ if test "$HAS_MAGIC" = no; then fi AH_TEMPLATE(HAVE_GLIBC_STDIO, [define to indicate system support for glibc cookie streams]) +AH_TEMPLATE(HAVE_BSD_STDIO, [define to indicate system support for BSD-style funopen streams]) HAS_FOPENCOOKIE=yes AC_CHECK_FUNC(fopencookie, [], [HAS_FOPENCOOKIE=no]) AC_CHECK_MEMBER([cookie_io_functions_t.read], [], [HAS_FOPENCOOKIE=no]) +HAS_FUNOPEN=yes +AC_CHECK_FUNC(funopen, [], [HAS_FUNOPEN=no]) + if test "$HAS_FOPENCOOKIE" = yes; then - AC_DEFINE(HAVE_GLIBC_STDIO) + AC_DEFINE(HAVE_GLIBC_STDIO) +elif test "$HAS_FUNOPEN" = yes; then + AC_DEFINE(HAVE_BSD_STDIO) else - AC_MSG_ERROR([*** libc support for custom stdio streams is required]) + AC_MSG_ERROR([*** libc support for custom stdio streams is required]) fi AH_TEMPLATE(HAVE_VALGRIND, [define to include debugging support for Valgrind]) diff --git a/lib/mtio.c b/lib/mtio.c index b994cc2..70a0949 100644 --- a/lib/mtio.c +++ b/lib/mtio.c @@ -107,7 +107,7 @@ static int mtclose(void *cookie) return(0); } -#ifdef HAVE_GLIBC_STDIO +#if defined(HAVE_GLIBC_STDIO) static cookie_io_functions_t iofuns = { .read = mtread, .write = mtwrite, @@ -130,6 +130,43 @@ FILE *mtstdopen(int fd, int issock, int timeout, char *mode) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); return(ret); } +#elif defined(HAVE_BSD_STDIO) +static int bsd2mtread(void *cookie, char *buf, int len) +{ + return(mtread(cookie, buf, len)); +} + +static int bsd2mtwrite(void *cookie, const char *buf, int len) +{ + return(mtwrite(cookie, buf, len)); +} + +FILE *mtstdopen(int fd, int issock, int timeout, char *mode) +{ + struct stdiofd *d; + FILE *ret; + int r, w; + + if(!strcmp(mode, "r")) { + r = 1; w = 0; + } else if(!strcmp(mode, "w")) { + r = 0; w = 1; + } else if(!strcmp(mode, "r+")) { + r = w = 1; + } else { + return(NULL); + } + omalloc(d); + d->fd = fd; + d->sock = issock; + d->timeout = timeout; + ret = funopen(d, r?bsd2mtread:NULL, w?bsd2mtwrite:NULL, NULL, mtclose); + if(!ret) + free(d); + else + fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); + return(ret); +} #else #error "No stdio implementation for this system" #endif