From 3f9160b7e659279f4e743c6074bbb05fa63ed7db Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 27 Nov 2025 18:14:15 -0800 Subject: [PATCH] cbortojson: don't hardcode OS support for fopencookie() Instead of saying Linux (a.k.a. glibc) has it and Apple has funopen(), use the fact that we've just detected them and inform the .c source which one it was. Fixes #306 Signed-off-by: Thiago Macieira --- CMakeLists.txt | 9 +++++++-- src/open_memstream.c | 8 ++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b5f7f55b..a54a9493 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -131,8 +131,13 @@ check_symbol_exists(funopen stdio.h HAVE_OPEN_FUNOPEN) check_symbol_exists(fopencookie stdio.h HAVE_OPEN_FOPENCOOKIE) if(NOT HAVE_OPEN_MEMSTREAM) - if (HAVE_OPEN_FUNOPEN AND HAVE_OPEN_FOPENCOOKIE) - message(STATUS "using open_memstream implementation") + if (HAVE_OPEN_FUNOPEN) + message(STATUS "implementing open_memstream using funopen()") + target_compile_definitions(tinycbor PRIVATE HAVE_OPEN_FUNOPEN) + target_sources(tinycbor PRIVATE src/open_memstream.c) + elseif (HAVE_OPEN_FOPENCOOKIE) + message(STATUS "implementing open_memstream using fopencookie()") + target_compile_definitions(tinycbor PRIVATE HAVE_OPEN_FOPENCOOKIE) target_sources(tinycbor PRIVATE src/open_memstream.c) else() target_compile_definitions(tinycbor PRIVATE WITHOUT_OPEN_MEMSTREAM) diff --git a/src/open_memstream.c b/src/open_memstream.c index 33653784..ed94e898 100644 --- a/src/open_memstream.c +++ b/src/open_memstream.c @@ -35,10 +35,10 @@ #if defined(__unix__) || defined(__APPLE__) # include #endif -#ifdef __APPLE__ +#if defined(HAVE_OPEN_FUNOPEN) typedef int RetType; typedef int LenType; -#elif __linux__ +#elif defined(HAVE_OPEN_FOPENCOOKIE) typedef ssize_t RetType; typedef size_t LenType; #else @@ -99,9 +99,9 @@ FILE *open_memstream(char **bufptr, size_t *lenptr) *bufptr = NULL; *lenptr = 0; -#ifdef __APPLE__ +#if defined(HAVE_OPEN_FUNOPEN) return funopen(b, NULL, write_to_buffer, NULL, close_buffer); -#elif __linux__ +#elif defined(HAVE_OPEN_FOPENCOOKIE) static const cookie_io_functions_t vtable = { NULL, write_to_buffer,