Skip to content

Commit e558b0f

Browse files
committed
Issue #264: Bring back BSD compatibility in strerror hacks
1 parent b578414 commit e558b0f

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/libs/tools.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <string.h>
2828
#include <unistd.h>
2929
#include <limits.h>
30+
#include <locale.h> // Make C locale for strerror_l()
3031
#include <errno.h>
3132
#include <math.h>
3233
#include <time.h>
@@ -175,14 +176,26 @@ INLINE int us_flock_timedwait_monotonic(int fd, ldf timeout) {
175176
}
176177

177178
INLINE char *us_errno_to_string(int error) {
179+
# if (_POSIX_C_SOURCE >= 200112L) && !defined(_GNU_SOURCE) // XSI
178180
char buf[2048];
179181
const uz max_len = sizeof(buf) - 1;
180-
# if (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE
181182
if (strerror_r(error, buf, max_len) != 0) {
182183
US_SNPRINTF(buf, max_len, "Errno = %d", error);
183184
}
184185
return us_strdup(buf);
185-
# else
186+
187+
# elif defined(__GLIBC__) && defined(_GNU_SOURCE) // GNU
188+
char buf[2048];
189+
const uz max_len = sizeof(buf) - 1;
186190
return us_strdup(strerror_r(error, buf, max_len));
191+
192+
# else // BSD
193+
locale_t locale = newlocale(LC_MESSAGES_MASK, "C", NULL);
194+
if (locale) {
195+
char *ptr = us_strdup(strerror_l(error, locale));
196+
freelocale(locale);
197+
return ptr;
198+
}
199+
return us_strdup("!!! newlocale() error !!!");
187200
# endif
188201
}

0 commit comments

Comments
 (0)