I'm fixing some Linux code which used strerror
(not thread-safe) for multi-threading. I found that strerror_r
and strerror_l
are both thread-safe. Due to different definitions for strerror_r
(depending on _GNU_SOURCE
it is differently defined) I'd like to use the newer strerror_l
function, but how am I supposed to obtain a locale_t
object for the current locale? I'm not using iconv
or anything, just plain libc, and I don't see how I can obtain a "default locale" object (I don't care in what language the error is printed, I just want a human readable string.)
You could use POSIX uselocale
:
strerror_l(errno, uselocale((locate_t)0));
If you pass "" to the locale parameter newlocale will allocate a locale object set to the current native locale[1]
[1]http://pubs.opengroup.org/onlinepubs/9699919799/functions/newlocale.html
static locale_t locale;
bool MyStrerrorInit(void)
{
locale = newlocale(LC_CTYPE_MASK|LC_NUMERIC_MASK|LC_TIME_MASK|LC_COLLATE_MASK|
LC_MONETARY_MASK|LC_MESSAGES_MASK,"",(locale_t)0);
if (locale == (locale_t)0) {
return false;
}
return true;
}
char * MyStrerror(int error)
{
return strerror_l(error, locale);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With