How to use strerror_l with current locale?

I am fixing Linux code that used strerror (not thread safe) for multithreading. I found that strerror_r and strerror_l are thread safe. Due to different definitions for strerror_r (depending on _GNU_SOURCE this is differently defined) I would like to use the new strerror_l function, but how should I get the locale_t object for the current locale? I don’t use iconv or anything else, just libc, and I don’t see how I can get the “default locale” object (I don’t care what language the error is printed in, I just need a person to read the line.)

+6
source share
1 answer

If you pass "" to the locale parameter, newlocale will highlight the 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); } 
+1
source

Source: https://habr.com/ru/post/1210721/


All Articles