From ba13535b9e9cd5f56b60533d9d8191a2bec05917 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Fri, 2 Sep 2022 00:14:56 +0200 Subject: [PATCH 1/3] include/Makefile.am: fix merge error (duplicate code) --- include/Makefile.am | 6 ------ 1 file changed, 6 deletions(-) diff --git a/include/Makefile.am b/include/Makefile.am index 1a5a644a82..f1ac7be3b2 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -2,12 +2,6 @@ dist_noinst_HEADERS = attribute.h common.h extstate.h proto.h \ state.h str.h timehead.h upsconf.h nut_float.h nut_stdint.h nut_platform.h \ wincompat.h -if WITH_DEV -include_HEADERS = parseconf.h -else -dist_noinst_HEADERS += parseconf.h -endif - # Optionally deliverable as part of NUT public API: if WITH_DEV include_HEADERS = parseconf.h From 9b79b66d8f3d510dc567755a78fa6bf00099f2ff Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Thu, 1 Sep 2022 23:37:19 +0200 Subject: [PATCH 2/3] clients/upslog.c: adjust to Windows branch after PR #1639 --- clients/upslog.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/clients/upslog.c b/clients/upslog.c index 35f03d2062..24f96dcad9 100644 --- a/clients/upslog.c +++ b/clients/upslog.c @@ -48,7 +48,6 @@ static char *upsname; static UPSCONN_t *ups; - static FILE *logfile; static char *logfn, *monhost; #ifndef WIN32 static sigset_t nut_upslog_sigmask; @@ -464,7 +463,11 @@ int main(int argc, char **argv) monhost_ups_current->monhost = xstrdup(strsep(&m_arg, ",")); if (!m_arg) fatalx(EXIT_FAILURE, "Argument '-m upsspec,logfile' requires exactly 2 components in the tuple"); +#ifndef WIN32 monhost_ups_current->logfn = xstrdup(strsep(&m_arg, ",")); +#else + monhost_ups_current->logfn = xstrdup(filter_path(strsep(&m_arg, ","))); +#endif if (m_arg) /* Had a third comma - also unexpected! */ fatalx(EXIT_FAILURE, "Argument '-m upsspec,logfile' requires exactly 2 components in the tuple"); if (upscli_splitname(monhost_ups_current->monhost, &(monhost_ups_current->upsname), &(monhost_ups_current->hostname), &(monhost_ups_current->port)) != 0) { From 7530455575f26c430de2dc23353e775968e5f2e3 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Fri, 2 Sep 2022 00:00:16 +0200 Subject: [PATCH 3/3] Add fallback strsep() implementation (for Windows mingw builds) after PR #1639 --- common/Makefile.am | 8 ++++++++ common/strsep.c | 19 +++++++++++++++++++ configure.ac | 13 +++++++++++++ include/str.h | 6 ++++++ 4 files changed, 46 insertions(+) create mode 100644 common/strsep.c diff --git a/common/Makefile.am b/common/Makefile.am index d987ece5a3..eb7fe53d4b 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -46,6 +46,14 @@ libcommon_la_SOURCES += strptime.c libcommonclient_la_SOURCES += strptime.c endif +if HAVE_STRSEP +EXTRA_DIST += strsep.c +else +# fall back to simple implem +libcommon_la_SOURCES += strsep.c +libcommonclient_la_SOURCES += strsep.c +endif + # TODO: libnutwincompat.la? if HAVE_WINDOWS libcommon_la_SOURCES += wincompat.c $(top_srcdir)/include/wincompat.h diff --git a/common/strsep.c b/common/strsep.c new file mode 100644 index 0000000000..6b9b503740 --- /dev/null +++ b/common/strsep.c @@ -0,0 +1,19 @@ +#include + +/* Simple implem courtesy of https://stackoverflow.com/a/58244503 + * Note: like the (BSD) standard implem, this changes the original stringp! + * Result is undefined (segfault here) if stringp==NULL + * (it is supposed to be address of string after all) + */ +char *strsep(char **stringp, const char *delim) { + char *rv = *stringp; + if (rv) { + *stringp += strcspn(*stringp, delim); + if (**stringp) + *(*stringp)++ = '\0'; + else + *stringp = NULL; + } + return rv; +} + diff --git a/configure.ac b/configure.ac index b5d80fac3a..db1f33c90e 100644 --- a/configure.ac +++ b/configure.ac @@ -335,6 +335,19 @@ AS_IF([test x"${ac_cv_func_strlwr}" = xyes], [AC_MSG_WARN([Optional C library routine strlwr not found])] ) +AC_CACHE_CHECK([for strsep(s1,s2)], + [ac_cv_func_strsep], + [AX_RUN_OR_LINK_IFELSE( + [AC_LANG_PROGRAM([$CODE_STRINGINCL], + [char arr@<:@64@:>@ = {"Some,tuple,text"} ; char *s = arr; if (strsep(&s, ",") == NULL) return 1])], + [ac_cv_func_strsep=yes], [ac_cv_func_strsep=no] + )]) +AS_IF([test x"${ac_cv_func_strsep}" = xyes], + [AC_DEFINE([HAVE_STRSEP], 1, [defined if standard library has, and C standard allows, the strsep(s1,s2) method])], + [AC_MSG_WARN([Optional C library routine strsep not found])] + ) +AM_CONDITIONAL([HAVE_STRSEP], [test x"${ac_cv_func_strsep}" = "xyes"]) + AC_CACHE_CHECK([for strstr(s1,s2)], [ac_cv_func_strstr], [AX_RUN_OR_LINK_IFELSE( diff --git a/include/str.h b/include/str.h index 05b7cb976c..8654fb98c2 100644 --- a/include/str.h +++ b/include/str.h @@ -134,6 +134,12 @@ int str_to_double_strict(const char *string, double *number, const int base); */ int str_ends_with(const char *s, const char *suff); +#ifndef HAVE_STRSEP +/* Makefile should add the implem to libcommon(client).la */ +char *strsep(char **stringp, const char *delim); +#define HAVE_STRSEP 1 +#endif + #ifdef __cplusplus /* *INDENT-OFF* */ }