From dfc06e70f1f12a871bb2300642e0231a0a6cbcda Mon Sep 17 00:00:00 2001 From: Daniel Markstedt Date: Mon, 14 Jul 2025 20:14:11 +0200 Subject: [PATCH] Compatibility with Solaris standard C library The Solaris standard C library defines a bgets symbol in libgen.h which conflicts with this library https://docs.oracle.com/cd/E36784_01/html/E36877/bgets-3gen.html This changeset introduces a build system check for this symbol and wraps a workaround in preprocessor macros You can also force the workaround with -Dforce-bgets-workaround=true --- bstring/bstraux.c | 2 +- bstring/bstraux.h | 4 ++++ bstring/bstrlib.c | 6 +++++- bstring/bstrlib.h | 8 ++++++++ meson.build | 26 ++++++++++++++++++++++++++ meson_options.txt | 6 ++++++ tests/bstest.c | 12 ++++++++++++ tests/testaux.c | 4 ++++ 8 files changed, 66 insertions(+), 2 deletions(-) diff --git a/bstring/bstraux.c b/bstring/bstraux.c index 1726d3e..583ef77 100644 --- a/bstring/bstraux.c +++ b/bstring/bstraux.c @@ -32,7 +32,7 @@ */ #ifdef HAVE_CONFIG_H -#include "config.h" +#include #endif #ifdef _MSC_VER diff --git a/bstring/bstraux.h b/bstring/bstraux.h index 525b5da..d83fdee 100644 --- a/bstring/bstraux.h +++ b/bstring/bstraux.h @@ -42,6 +42,10 @@ #ifndef BSTRAUX_H #define BSTRAUX_H +#ifdef HAVE_CONFIG_H +#include +#endif + #include #include diff --git a/bstring/bstrlib.c b/bstring/bstrlib.c index 8cc2437..afd07be 100644 --- a/bstring/bstrlib.c +++ b/bstring/bstrlib.c @@ -36,7 +36,7 @@ */ #ifdef HAVE_CONFIG_H -#include "config.h" +#include #endif #if defined (_MSC_VER) @@ -1943,7 +1943,11 @@ bgetsa(bstring b, bNgetc getcPtr, void *parm, char terminator) } bstring +#if defined(HAVE_LIBGEN_H_BGETS) +bgetstream(bNgetc getcPtr, void *parm, char terminator) +#else bgets(bNgetc getcPtr, void *parm, char terminator) +#endif { bstring buff; if (0 > bgetsa(buff = bfromcstr (""), getcPtr, parm, terminator) || diff --git a/bstring/bstrlib.h b/bstring/bstrlib.h index 3519c47..58ae1e5 100644 --- a/bstring/bstrlib.h +++ b/bstring/bstrlib.h @@ -66,6 +66,10 @@ extern "C" { #endif +#ifdef HAVE_CONFIG_H +#include +#endif + #include #include #include @@ -1150,7 +1154,11 @@ typedef size_t (*bNread)(void *buff, size_t elsize, size_t nelem, void *parm); * character. This is consistent with the semantics of fgets.) */ BSTR_PUBLIC bstring +#if defined(HAVE_LIBGEN_H_BGETS) +bgetstream(bNgetc getcPtr, void *parm, char terminator); +#else bgets(bNgetc getcPtr, void *parm, char terminator); +#endif /** * Read an entire stream into a bstring, verbatum. diff --git a/meson.build b/meson.build index 6bce313..2512f69 100644 --- a/meson.build +++ b/meson.build @@ -11,6 +11,32 @@ add_project_arguments('-fvisibility=hidden', language: 'c') bstring_inc = include_directories(['.', 'bstring']) +bgets_test_code = ''' +#include +#include +int main() { + char buffer[256]; + char delim = '\n'; + FILE *fp = stdin; + char *result = bgets(buffer, sizeof(buffer), fp, &delim); + return 0; +} +''' + +has_bgets = cc.compiles(bgets_test_code, name: 'bgets defined in standard C library') +conf_data = configuration_data() + +if has_bgets or get_option('force-bgets-workaround') + conf_data.set('HAVE_LIBGEN_H_BGETS', '1') +else + conf_data.set('HAVE_LIBGEN_H_BGETS', '0') +endif + +configure_file( + output: 'config.h', + configuration: conf_data +) + subdir('bstring') pkg.generate( diff --git a/meson_options.txt b/meson_options.txt index 2ee1fbc..eeeb8ea 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1,3 +1,9 @@ +option( + 'force-bgets-workaround', + type: 'boolean', + value: true, + description: 'Always use workaround that renames bgets function to bgetstring', +) option( 'enable-docs', type: 'boolean', diff --git a/tests/bstest.c b/tests/bstest.c index 201d3d6..91d2245 100644 --- a/tests/bstest.c +++ b/tests/bstest.c @@ -35,6 +35,10 @@ * This file is the C unit test for Bstrlib. */ +#ifdef HAVE_CONFIG_H +#include +#endif + #include "bstraux.h" #include "bstrlib.h" #include @@ -2466,11 +2470,19 @@ START_TEST(core_038) int ret = test38_aux_bnopen(&f, &shortBstring); ck_assert_int_eq(ret, 0); /* Creation/reads */ +#if defined(HAVE_LIBGEN_H_BGETS) + b0 = bgetstream((bNgetc)test38_aux_bngetc, &f, 'b'); +#else b0 = bgets((bNgetc)test38_aux_bngetc, &f, 'b'); +#endif ck_assert(b0 != NULL); b1 = bread((bNread)test38_aux_bnread, &f); ck_assert(b1 != NULL); +#if defined(HAVE_LIBGEN_H_BGETS) + b2 = bgetstream((bNgetc)test38_aux_bngetc, &f, '\0'); +#else b2 = bgets((bNgetc)test38_aux_bngetc, &f, '\0'); +#endif ck_assert(b2 == NULL); b3 = bread((bNread)test38_aux_bnread, &f); ck_assert(b3 != NULL); diff --git a/tests/testaux.c b/tests/testaux.c index cb090fd..0e28ffd 100644 --- a/tests/testaux.c +++ b/tests/testaux.c @@ -35,6 +35,10 @@ * This file is the C unit test for the bstraux module of Bstrlib. */ +#ifdef HAVE_CONFIG_H +#include +#endif + #include "bstraux.h" #include "bstrlib.h" #include