+2019-12-18 Bruno Haible <bruno@clisp.org>
+
+ hard-locale: Make multithread-safe.
+ * lib/hard-locale.h (hard_locale): Move documentation to here.
+ * lib/hard-locale.c: Don't include <stdlib.h>.
+ (GLIBC_VERSION): Remove macro.
+ (hard_locale): Assume that all systems name the "C" and "POSIX" locales
+ "C" or "POSIX". Invoke setlocale_null instead of setlocale.
+ * modules/hard-locale (Depends-on): Remove strdup. Add setlocale-null.
+ (configure.ac): Require gl_FUNC_SETLOCALE_NULL. Set LIB_HARD_LOCALE.
+ (Link): New section.
+ * modules/hard-locale-tests (Makefile.am): Link test-hard-locale against
+ $(LIB_HARD_LOCALE).
+
2019-12-18 Bruno Haible <bruno@clisp.org>
hard-locale: Avoid test failure on Haiku.
#include "hard-locale.h"
#include <locale.h>
-#include <stdlib.h>
#include <string.h>
-#ifdef __GLIBC__
-# define GLIBC_VERSION __GLIBC__
-#elif defined __UCLIBC__
-# define GLIBC_VERSION 2
-#else
-# define GLIBC_VERSION 0
-#endif
-
-/* Return true if the current CATEGORY locale is hard, i.e. if you
- can't get away with assuming traditional C or POSIX behavior. */
bool
hard_locale (int category)
{
- bool hard = true;
- char const *p = setlocale (category, NULL);
-
- if (p)
- {
- if (2 <= GLIBC_VERSION)
- {
- if (strcmp (p, "C") == 0 || strcmp (p, "POSIX") == 0)
- hard = false;
- }
- else
- {
- char *locale = strdup (p);
- if (locale)
- {
- /* Temporarily set the locale to the "C" and "POSIX" locales
- to find their names, so that we can determine whether one
- or the other is the caller's locale. */
- if (((p = setlocale (category, "C"))
- && strcmp (p, locale) == 0)
- || ((p = setlocale (category, "POSIX"))
- && strcmp (p, locale) == 0))
- hard = false;
+ char locale[SETLOCALE_NULL_MAX];
- /* Restore the caller's locale. */
- setlocale (category, locale);
- free (locale);
- }
- }
- }
+ if (setlocale_null (category, locale, sizeof (locale)))
+ return false;
- return hard;
+ return !(strcmp (locale, "C") == 0 || strcmp (locale, "POSIX") == 0);
}
# include <stdbool.h>
-bool hard_locale (int);
+/* Return true if the specified CATEGORY of the current locale is hard, i.e.
+ different from the C or POSIX locale that has a fixed behavior.
+ CATEGORY must be one of the LC_* values, but not LC_ALL. */
+extern bool hard_locale (int category);
#endif /* HARD_LOCALE_H_ */