summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2019-12-18 11:42:21 +0100
committerBruno Haible <bruno@clisp.org>2019-12-18 11:42:21 +0100
commit25d476a2b79ab1cfc9c34e162bf5a69159ed6952 (patch)
tree6c4b05e31fae20e8f099f476d7801d7a3a7cb0ae
parent9b3418df8ef2a9ab70f8dab5790ce3ab729c2233 (diff)
downloadgnulib-25d476a2b79ab1cfc9c34e162bf5a69159ed6952.tar.gz
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).
-rw-r--r--ChangeLog14
-rw-r--r--lib/hard-locale.c45
-rw-r--r--lib/hard-locale.h5
-rw-r--r--modules/hard-locale8
-rw-r--r--modules/hard-locale-tests1
5 files changed, 30 insertions, 43 deletions
diff --git a/ChangeLog b/ChangeLog
index 2f41e5e8a7..e2c33fb163 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,19 @@
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.
* tests/test-hard-locale.c (test_one): Treat Haiku like recent OpenBSD.
diff --git a/lib/hard-locale.c b/lib/hard-locale.c
index dcfcad62ee..4a2adabb7e 100644
--- a/lib/hard-locale.c
+++ b/lib/hard-locale.c
@@ -21,52 +21,15 @@
#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);
}
diff --git a/lib/hard-locale.h b/lib/hard-locale.h
index 8f1da96e2f..160674ba7a 100644
--- a/lib/hard-locale.h
+++ b/lib/hard-locale.h
@@ -20,6 +20,9 @@
# 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_ */
diff --git a/modules/hard-locale b/modules/hard-locale
index d4463b74bd..df07d4a6ed 100644
--- a/modules/hard-locale
+++ b/modules/hard-locale
@@ -7,9 +7,12 @@ lib/hard-locale.c
Depends-on:
stdbool
-strdup
+setlocale-null
configure.ac:
+AC_REQUIRE([gl_FUNC_SETLOCALE_NULL])
+LIB_HARD_LOCALE="$LIB_SETLOCALE_NULL"
+AC_SUBST([LIB_HARD_LOCALE])
Makefile.am:
lib_SOURCES += hard-locale.c
@@ -17,6 +20,9 @@ lib_SOURCES += hard-locale.c
Include:
"hard-locale.h"
+Link:
+$(LIB_HARD_LOCALE)
+
License:
LGPLv2+
diff --git a/modules/hard-locale-tests b/modules/hard-locale-tests
index cdc084eba4..a501781d8d 100644
--- a/modules/hard-locale-tests
+++ b/modules/hard-locale-tests
@@ -16,3 +16,4 @@ Makefile.am:
TESTS += test-hard-locale
check_PROGRAMS += test-hard-locale
noinst_PROGRAMS += locale
+test_hard_locale_LDADD = $(LDADD) @LIB_HARD_LOCALE@