From 13af11a3a64cf9f08014e89f35e52e001fb10cad Mon Sep 17 00:00:00 2001 From: Maxim Devaev Date: Sun, 18 Feb 2024 18:41:29 +0200 Subject: [PATCH] Using strerror_r() instead of strerror_l() for better compatibility --- src/libs/tools.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libs/tools.h b/src/libs/tools.h index 1452707..664856a 100644 --- a/src/libs/tools.h +++ b/src/libs/tools.h @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -177,15 +176,16 @@ INLINE int us_flock_timedwait_monotonic(int fd, long double timeout) { } INLINE char *us_errno_to_string(int error) { - locale_t locale = newlocale(LC_MESSAGES_MASK, "C", NULL); - char *buf; - if (locale) { - buf = us_strdup(strerror_l(error, locale)); - freelocale(locale); - } else { - buf = us_strdup("!!! newlocale() error !!!"); + char buf[2048]; + const size_t max_len = sizeof(buf) - 1; +# if (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE + if (strerror_r(error, buf, max_len) != 0) { + assert(snprintf(buf, max_len, "Errno = %d", error) > 0); } - return buf; + return us_strdup(buf); +# else + return us_strdup(strerror_r(error, buf, max_len)); +# endif } INLINE char *us_signum_to_string(int signum) {