Skip to content

Commit

Permalink
Merge pull request #1643 from jimklimov/Windows-v2.8.0-3
Browse files Browse the repository at this point in the history
Revisit NUT for Windows v2.8.0 branch after upslog update
  • Loading branch information
jimklimov authored Sep 2, 2022
2 parents 295f3b7 + 7530455 commit ef527f6
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
5 changes: 4 additions & 1 deletion clients/upslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions common/strsep.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <string.h>

/* 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;
}

13 changes: 13 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 0 additions & 6 deletions include/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions include/str.h
Original file line number Diff line number Diff line change
Expand Up @@ -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* */
}
Expand Down

0 comments on commit ef527f6

Please sign in to comment.