using strerror_l()

This commit is contained in:
Devaev Maxim
2021-04-01 10:21:12 +03:00
parent 24f7fb797b
commit cbc6145977

View File

@@ -28,6 +28,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <locale.h>
#include <errno.h> #include <errno.h>
#include <math.h> #include <math.h>
#include <time.h> #include <time.h>
@@ -146,10 +147,14 @@ INLINE int flock_timedwait_monotonic(int fd, long double timeout) {
} }
INLINE char *errno_to_string(int error, char *buf, size_t size) { INLINE char *errno_to_string(int error, char *buf, size_t size) {
# if defined(__GLIBC__) && defined(_GNU_SOURCE) assert(buf);
return strerror_r(error, buf, size); assert(size > 0);
# else locale_t locale = newlocale(LC_MESSAGES_MASK, "C", NULL);
strerror_r(error, buf, size); char *str = "!!! newlocale() error !!!";
strncpy(buf, (locale ? strerror_l(error, locale) : str), size - 1);
buf[size - 1] = '\0';
if (locale) {
freelocale(locale);
}
return buf; return buf;
# endif
} }