immutable: Add tests.
[gnulib.git] / m4 / duplocale.m4
blob8fcd155090ec9431752d9aeff9f2f1aad863c4fd
1 # duplocale.m4 serial 12
2 dnl Copyright (C) 2009-2021 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 AC_DEFUN([gl_FUNC_DUPLOCALE],
9   AC_REQUIRE([gl_LOCALE_H_DEFAULTS])
10   AC_REQUIRE([AC_CANONICAL_HOST])
11   AC_REQUIRE([gl_FUNC_SETLOCALE_NULL])
12   AC_CHECK_FUNCS_ONCE([duplocale])
13   if test $ac_cv_func_duplocale = yes; then
14     dnl Check against glibc bug where duplocale crashes.
15     dnl See <https://sourceware.org/bugzilla/show_bug.cgi?id=10969>.
16     dnl Also, on AIX 7.1, duplocale(LC_GLOBAL_LOCALE) returns (locale_t)0 with
17     dnl errno set to EINVAL.
18     dnl Also, on NetBSD 7.0, duplocale(LC_GLOBAL_LOCALE) returns a locale that
19     dnl corresponds to the C locale.
20     AC_REQUIRE([gl_LOCALE_H])
21     if test $HAVE_LOCALE_T = 1; then
22       AC_CHECK_FUNCS_ONCE([snprintf_l nl_langinfo_l])
23       AC_CACHE_CHECK([whether duplocale(LC_GLOBAL_LOCALE) works],
24         [gl_cv_func_duplocale_works],
25         [AC_RUN_IFELSE(
26            [AC_LANG_SOURCE([[
27 #include <locale.h>
28 #if HAVE_XLOCALE_H
29 # include <xlocale.h>
30 #endif
31 #if HAVE_SNPRINTF_L
32 # include <stdio.h>
33 #endif
34 #if HAVE_NL_LANGINFO_L
35 # include <langinfo.h>
36 #endif
37 #include <string.h>
38 struct locale_dependent_values
40   char numeric[100];
41   char time[100];
43 int main ()
45   struct locale_dependent_values expected_result;
46   struct locale_dependent_values result;
47   locale_t loc;
48   setlocale (LC_ALL, "en_US.UTF-8");
49   setlocale (LC_NUMERIC, "de_DE.UTF-8");
50   setlocale (LC_TIME, "fr_FR.UTF-8");
51 #if HAVE_SNPRINTF_L
52   snprintf (expected_result.numeric, sizeof (expected_result.numeric), "%g", 3.5);
53 #endif
54 #if HAVE_NL_LANGINFO_L
55   strcpy (expected_result.time, nl_langinfo (MON_1));
56 #endif
57   loc = duplocale (LC_GLOBAL_LOCALE);
58   if (!loc)
59     return 1;
60 #if HAVE_SNPRINTF_L
61   snprintf_l (result.numeric, sizeof (result.numeric), loc, "%g", 3.5);
62 #endif
63 #if HAVE_NL_LANGINFO_L
64   strcpy (result.time, nl_langinfo_l (MON_1, loc));
65 #endif
66 #if HAVE_SNPRINTF_L
67   if (strcmp (result.numeric, expected_result.numeric) != 0)
68     return 2;
69 #endif
70 #if HAVE_NL_LANGINFO_L
71   if (strcmp (result.time, expected_result.time) != 0)
72     return 3;
73 #endif
74   freelocale (loc);
75   return 0;
76 }]])],
77            [gl_cv_func_duplocale_works=yes],
78            [gl_cv_func_duplocale_works=no],
79            [dnl Guess it works except on glibc < 2.12, uClibc, AIX, and NetBSD.
80             case "$host_os" in
81               aix* | netbsd*) gl_cv_func_duplocale_works="guessing no";;
82               *-gnu* | gnu*)
83                 AC_EGREP_CPP([Unlucky], [
84 #include <features.h>
85 #ifdef __GNU_LIBRARY__
86  #if (__GLIBC__ == 2 && __GLIBC_MINOR__ < 12)
87   Unlucky GNU user
88  #endif
89 #endif
90 #ifdef __UCLIBC__
91  Unlucky user
92 #endif
93                   ],
94                   [gl_cv_func_duplocale_works="guessing no"],
95                   [gl_cv_func_duplocale_works="guessing yes"])
96                 ;;
97               *) gl_cv_func_duplocale_works="guessing yes";;
98             esac
99            ])
100         ])
101       case "$gl_cv_func_duplocale_works" in
102         *no) REPLACE_DUPLOCALE=1 ;;
103       esac
104     else
105       dnl In 2019, some versions of z/OS lack the locale_t type and have broken
106       dnl newlocale, duplocale, freelocale functions. In this situation, we
107       dnl cannot use nor override duplocale.
108       HAVE_DUPLOCALE=0
109     fi
110   else
111     HAVE_DUPLOCALE=0
112   fi
113   if test $REPLACE_DUPLOCALE = 1; then
114     LIB_DUPLOCALE="$LIB_SETLOCALE_NULL"
115   else
116     LIB_DUPLOCALE=
117   fi
118   dnl LIB_DUPLOCALE is expected to be '-pthread' or '-lpthread' on AIX
119   dnl with gcc or xlc, and empty otherwise.
120   AC_SUBST([LIB_DUPLOCALE])
123 # Prerequisites of lib/duplocale.c.
124 AC_DEFUN([gl_PREREQ_DUPLOCALE],
126   :