From e4499269be39b8c72f7ec33ccfc6a8b5db7830d4 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 26 Nov 2019 22:16:22 +0000 Subject: [PATCH] portability: Provide implementation of fmemopen We are going to want one of these. I have tested it as follows: 1. In configure.ac just before AC_CHECK_FUNCS, add: LIBS+=-lbsd 2. In osdep.c, add: #include 3. Change all fmemopen to Yfmemopen, with git-ls-files | perl -lne 'print if lstat and -f _' | xargs perl -i~ -pe 's/fmemopen/Y$&/gi' The result is that we do not find Yfmemopen. The tests still pass and I have verified that my stunt implementation is called. FTR, this rune undoes the Y: git-ls-files | perl -lne 'print if lstat and -f _' | xargs perl -i~ -pe 's/Y(fmemopen)/$1/gi' Signed-off-by: Ian Jackson --- config.h.in | 6 ++++++ configure | 13 +++++++++++++ configure.ac | 2 ++ osdep.c | 38 ++++++++++++++++++++++++++++++++++++++ osdep.h | 6 ++++++ 5 files changed, 65 insertions(+) diff --git a/config.h.in b/config.h.in index 8cce8b0..a99e83f 100644 --- a/config.h.in +++ b/config.h.in @@ -11,6 +11,12 @@ /* Define to 1 to use IPv6 support in system and adns */ #undef CONFIG_IPV6 +/* Define to 1 if you have the `fmemopen' function. */ +#undef HAVE_FMEMOPEN + +/* Define to 1 if you have the `funopen' function. */ +#undef HAVE_FUNOPEN + /* Define to 1 if you have the `adns' library (-ladns). */ #undef HAVE_LIBADNS diff --git a/configure b/configure index b9cf39a..43de16e 100755 --- a/configure +++ b/configure @@ -4119,6 +4119,19 @@ fi +for ac_func in fmemopen funopen +do : + as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` +ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" +if eval test \"x\$"$as_ac_var"\" = x"yes"; then : + cat >>confdefs.h <<_ACEOF +#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1 +_ACEOF + +fi +done + + { $as_echo "$as_me:${as_lineno-$LINENO}: Checking requirements for IPv6 support..." >&5 $as_echo "$as_me: Checking requirements for IPv6 support..." >&6;} enable_ipv6=true diff --git a/configure.ac b/configure.ac index 7b06f09..674ed36 100644 --- a/configure.ac +++ b/configure.ac @@ -100,6 +100,8 @@ SECNET_C_GETFUNC(inet_aton,resolv) AC_CHECK_LIB(adns,adns_init) REQUIRE_HEADER([adns.h]) +AC_CHECK_FUNCS([fmemopen funopen]) + AC_MSG_NOTICE([Checking requirements for IPv6 support...]) enable_ipv6=true m4_define(NO_IPV6,[enable_ipv6=false]) diff --git a/osdep.c b/osdep.c index 7bb7aaa..2407698 100644 --- a/osdep.c +++ b/osdep.c @@ -25,3 +25,41 @@ #include "osdep.h" #include "secnet.h" #include "util.h" + +#ifndef HAVE_FMEMOPEN +# ifdef HAVE_FUNOPEN + +struct fmemopen_state { + const char *bufp; + size_t remain; +}; + +static int fmemopen_readfn(void *sst, char *out, int sz) +{ + struct fmemopen_state *st=sst; + assert(sz>=0); + int now=MIN((size_t)sz,st->remain); + memcpy(out,st->bufp,now); + st->remain-=now; + return now; +} +static int fmemopen_close(void *sst) { free(sst); return 0; } + +FILE *fmemopen(void *buf, size_t size, const char *mode) +{ + /* this is just a fake plastic imitation */ + assert(!strcmp(mode,"r")); + struct fmemopen_state *st; + NEW(st); + st->bufp=buf; + st->remain=size; + FILE *f=funopen(st,fmemopen_readfn,0,0,fmemopen_close); + if (!f) free(st); + return f; +} + +# else /* HAVE_FUNOPEN */ +# error no fmemopen, no funopen, cannot proceed +# endif + +#endif /* HAVE_FMEMOPEN */ diff --git a/osdep.h b/osdep.h index a89958d..46c532b 100644 --- a/osdep.h +++ b/osdep.h @@ -26,4 +26,10 @@ #include "config.h" +#include + +#ifndef HAVE_FMEMOPEN +extern FILE *fmemopen(void *buf, size_t size, const char *mode); +#endif + #endif /* osdep_h */ -- 2.30.2