From 855f5ee0f79b702f83d4476edb6351bd16bc23c8 Mon Sep 17 00:00:00 2001 From: thor Date: Sat, 4 Jan 2025 09:07:14 +0000 Subject: [PATCH] compat, libout123, mpg123: detect strtok_r or strtok_s (MSVC) to fix bug 376 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We prefer strtok_r if available, but fall back to strtok_s on MS platforms or plain strtok if no other is there. There is INT123_compat_strtok now to use as mediator name — which is a macro, currently, so doesn't even change the libout123 binary. git-svn-id: svn://scm.orgis.org/mpg123/trunk@5474 35dc7657-300d-0410-a2e5-dc2837fedb53 --- NEWS | 3 +++ configure.ac | 3 ++- ports/cmake/src/CMakeLists.txt | 2 ++ ports/cmake/src/config.cmake.h.in | 2 ++ src/compat/compat.h | 14 ++++++++++++++ src/control_generic.c | 5 +++-- src/libout123/libout123.c | 4 ++-- 7 files changed, 28 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index 8e1a633b..10f8d9d0 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,8 @@ 1.32.11 ------ +- compat: Map strtok use to strtok_r or strtok_s (MS platforms), if possible. + users only in control_generic and libout123 so far. Out123 itself uses mytok. + Shall fix bug 376 (build with MSVC again). - mpg123: Fix printout of filenames at end (convert/limit text encoding). - libout123: -- modules/win32: Align waveOutGetDevCapsA to WAVEOUTCAPSA, in anticipation diff --git a/configure.ac b/configure.ac index 173881e1..98fac8ba 100644 --- a/configure.ac +++ b/configure.ac @@ -1491,7 +1491,8 @@ AC_CHECK_FUNCS( sched_setscheduler setuid getuid) # Check for setpriority AC_CHECK_FUNCS( setpriority ) -AC_CHECK_FUNCS( strerror strerror_l uselocale ) +# strtok_s for MSVCRT, not the C11 variant +AC_CHECK_FUNCS( strerror strerror_l uselocale strtok_r strtok_s) AC_CHECK_FUNCS( setlocale nl_langinfo mbstowcs wcstombs wcswidth iswprint ) diff --git a/ports/cmake/src/CMakeLists.txt b/ports/cmake/src/CMakeLists.txt index 93ef004f..265623a6 100644 --- a/ports/cmake/src/CMakeLists.txt +++ b/ports/cmake/src/CMakeLists.txt @@ -67,6 +67,8 @@ check_function_exists(shmdt HAVE_SHMDT) check_function_exists(shmctl HAVE_SHMCTL) check_function_exists(strerror HAVE_STRERROR) check_function_exists(strerror_l HAVE_STRERROR_L) +check_function_exists(strtok_r HAVE_STRTOK_R) +check_function_exists(strtok_s HAVE_STRTOK_S) check_function_exists(fork HAVE_FORK) check_function_exists(execvp HAVE_EXECVP) check_function_exists(ctermid HAVE_CTERMID) diff --git a/ports/cmake/src/config.cmake.h.in b/ports/cmake/src/config.cmake.h.in index 39aac4c9..e2a44327 100644 --- a/ports/cmake/src/config.cmake.h.in +++ b/ports/cmake/src/config.cmake.h.in @@ -43,6 +43,8 @@ #cmakedefine HAVE_SIGNAL_H 1 #cmakedefine HAVE_STRERROR 1 #cmakedefine HAVE_STRERROR_L 1 +#cmakedefine HAVE_STRTOK_R 1 +#cmakedefine HAVE_STRTOK_S 1 #cmakedefine HAVE_FORK 1 #cmakedefine HAVE_EXECVP 1 #cmakedefine HAVE_CTERMID 1 diff --git a/src/compat/compat.h b/src/compat/compat.h index 23df921b..0a16fd32 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -110,8 +110,17 @@ typedef unsigned char byte; +// Annoying hackery to select a safe strtok variant. MS decided to call their strtok_r strtok_s, while +// C11 declares another strtok_s with different prototype. Thanks to you all. +#ifdef HAVE_STRTOK_R +#define INT123_compat_strtok(a, b, c) strtok_r((a), (b), (c)) +#endif + #if (defined(_UCRT) || defined(_MSC_VER) || (defined(__MINGW32__) || defined(__MINGW64__)) || (defined(__WATCOMC__) && defined(__NT__))) && !defined(__CYGWIN__) #define MPG123_COMPAT_MSVCRT_IO +#ifndef INT123_compat_strtok +#define INT123_compat_strtok(a, b, c) strtok_s((a), (b), (c)) +#endif #endif #if defined(MPG123_COMPAT_MSVCRT_IO) @@ -150,6 +159,11 @@ typedef unsigned char byte; #include #endif +#ifndef INT123_compat_strtok +#warning "no safe strtok found" +#define INT123_compat_strtok(a, b, c) strtok((a), (b)) +#endif + /* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */ void *INT123_safe_realloc(void *ptr, size_t size); // Also freeing ptr if result is NULL. You can do diff --git a/src/control_generic.c b/src/control_generic.c index 2e3f6376..3604c4a3 100644 --- a/src/control_generic.c +++ b/src/control_generic.c @@ -780,8 +780,9 @@ int control_generic (mpg123_handle *fr) /* commands with arguments */ cmd = NULL; arg = NULL; - cmd = strtok(comstr," \t"); /* get the main command */ - arg = strtok(NULL,""); /* get the args */ + char *toksave = NULL; + cmd = INT123_compat_strtok(comstr, " \t", &toksave); /* get the main command */ + arg = INT123_compat_strtok(NULL, "", &toksave); /* get the args */ if (cmd && strlen(cmd) && arg && strlen(arg)) { diff --git a/src/libout123/libout123.c b/src/libout123/libout123.c index f3155da1..7baaa37b 100644 --- a/src/libout123/libout123.c +++ b/src/libout123/libout123.c @@ -456,11 +456,11 @@ out123_open(out123_handle *ao, const char* driver, const char* device) /* Now loop over the list of possible modules to find one that works. */ char *toksave = NULL; - nextname = strtok_r(modnames, ",", &toksave); + nextname = INT123_compat_strtok(modnames, ",", &toksave); while(!ao->open && nextname) { char *curname = nextname; - nextname = strtok_r(NULL, ",", &toksave); + nextname = INT123_compat_strtok(NULL, ",", &toksave); check_output_module(ao, curname, device, !nextname); if(ao->open) {