From 3713ea0f1a8841f3af5b628f8c14b09d04ee7c49 Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Wed, 6 Sep 2023 08:52:37 -0500 Subject: [PATCH] Add setlocale() call (fix #1740) --- configure.ac | 1 + src/main.c | 7 +++++++ tests/shtest | 13 +++++++++++++ 3 files changed, 21 insertions(+) diff --git a/configure.ac b/configure.ac index 9e8830bf41..4a72465979 100644 --- a/configure.ac +++ b/configure.ac @@ -149,6 +149,7 @@ AC_CHECK_MEMBER([struct tm.tm_gmtoff], [AC_DEFINE([HAVE_TM_TM_GMT_OFF],1,[Define [], [[#include ]]) AC_CHECK_MEMBER([struct tm.__tm_gmtoff], [AC_DEFINE([HAVE_TM___TM_GMT_OFF],1,[Define to 1 if the system has the __tm_gmt_off field in struct tm])], [], [[#include ]]) +AC_FIND_FUNC([setlocale], [c], [#include ], [0,0]) dnl Figure out if we have the pthread functions we actually need AC_FIND_FUNC_NO_LIBS([pthread_key_create], [], [#include ], [NULL, NULL]) diff --git a/src/main.c b/src/main.c index 2348b94df1..9c6b3cf75a 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,9 @@ #include #include #include +#ifdef HAVE_SETLOCALE +#include +#endif #include #include #include @@ -319,6 +322,10 @@ int main(int argc, char* argv[]) { jv ARGS = jv_array(); /* positional arguments */ jv program_arguments = jv_object(); /* named arguments */ +#ifdef HAVE_SETLOCALE + (void) setlocale(LC_ALL, ""); +#endif + #ifdef WIN32 jv_tsd_dtoa_ctx_init(); fflush(stdout); diff --git a/tests/shtest b/tests/shtest index 52a82190d0..ebba413e9e 100755 --- a/tests/shtest +++ b/tests/shtest @@ -561,4 +561,17 @@ if ! $VALGRIND $Q $JQ -n -f "$JQTESTDIR/yes-main-program.jq" > $d/out 2>&1; then exit 1 fi +if locale -a > /dev/null; then + locale=$(locale | egrep -v '^(C|POSIX|en)' | egrep -i 'utf8|utf-8' | head) + if [[ -z $locale ]]; then + echo "WARNING: Not testing localization" + else + date=$(LC_ALL="$locale" date +"%a %d %b %Y at %H:%M:%S") + if ! LC_ALL="$locale" ./jq -nRe --arg date "$date" '$date|strptime("%a %d %b %Y at %H:%M:%S")? // false'; then + echo "jq does not honor LC_ALL environment variable" + exit 1; + fi + fi +fi + exit 0