xref: /netbsd-src/external/gpl3/gdb/dist/gnulib/import/setlocale_null.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
18dffb485Schristos /* Query the name of the current global locale.
2*4b169a6bSchristos    Copyright (C) 2019-2022 Free Software Foundation, Inc.
38dffb485Schristos 
4*4b169a6bSchristos    This file is free software: you can redistribute it and/or modify
5*4b169a6bSchristos    it under the terms of the GNU Lesser General Public License as
6*4b169a6bSchristos    published by the Free Software Foundation; either version 2.1 of the
7*4b169a6bSchristos    License, or (at your option) any later version.
88dffb485Schristos 
9*4b169a6bSchristos    This file is distributed in the hope that it will be useful,
108dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
118dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*4b169a6bSchristos    GNU Lesser General Public License for more details.
138dffb485Schristos 
14*4b169a6bSchristos    You should have received a copy of the GNU Lesser General Public License
158dffb485Schristos    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
168dffb485Schristos 
178dffb485Schristos /* Written by Bruno Haible <bruno@clisp.org>, 2019.  */
188dffb485Schristos 
198dffb485Schristos #include <config.h>
208dffb485Schristos 
218dffb485Schristos /* Specification.  */
228dffb485Schristos #include "setlocale_null.h"
238dffb485Schristos 
248dffb485Schristos #include <errno.h>
258dffb485Schristos #include <locale.h>
268dffb485Schristos #include <stdlib.h>
278dffb485Schristos #include <string.h>
288dffb485Schristos #if defined _WIN32 && !defined __CYGWIN__
298dffb485Schristos # include <wchar.h>
308dffb485Schristos #endif
318dffb485Schristos 
328dffb485Schristos #if !(SETLOCALE_NULL_ALL_MTSAFE && SETLOCALE_NULL_ONE_MTSAFE)
338dffb485Schristos # if defined _WIN32 && !defined __CYGWIN__
348dffb485Schristos 
358dffb485Schristos #  define WIN32_LEAN_AND_MEAN  /* avoid including junk */
368dffb485Schristos #  include <windows.h>
378dffb485Schristos 
388dffb485Schristos # elif HAVE_PTHREAD_API
398dffb485Schristos 
408dffb485Schristos #  include <pthread.h>
418dffb485Schristos #  if HAVE_THREADS_H && HAVE_WEAK_SYMBOLS
428dffb485Schristos #   include <threads.h>
438dffb485Schristos #   pragma weak thrd_exit
448dffb485Schristos #   define c11_threads_in_use() (thrd_exit != NULL)
458dffb485Schristos #  else
468dffb485Schristos #   define c11_threads_in_use() 0
478dffb485Schristos #  endif
488dffb485Schristos 
498dffb485Schristos # elif HAVE_THREADS_H
508dffb485Schristos 
518dffb485Schristos #  include <threads.h>
528dffb485Schristos 
538dffb485Schristos # endif
548dffb485Schristos #endif
558dffb485Schristos 
568dffb485Schristos /* Use the system's setlocale() function, not the gnulib override, here.  */
578dffb485Schristos #undef setlocale
588dffb485Schristos 
598dffb485Schristos static const char *
setlocale_null_androidfix(int category)608dffb485Schristos setlocale_null_androidfix (int category)
618dffb485Schristos {
628dffb485Schristos   const char *result = setlocale (category, NULL);
638dffb485Schristos 
648dffb485Schristos #ifdef __ANDROID__
658dffb485Schristos   if (result == NULL)
668dffb485Schristos     switch (category)
678dffb485Schristos       {
688dffb485Schristos       case LC_CTYPE:
698dffb485Schristos       case LC_NUMERIC:
708dffb485Schristos       case LC_TIME:
718dffb485Schristos       case LC_COLLATE:
728dffb485Schristos       case LC_MONETARY:
738dffb485Schristos       case LC_MESSAGES:
748dffb485Schristos       case LC_ALL:
758dffb485Schristos       case LC_PAPER:
768dffb485Schristos       case LC_NAME:
778dffb485Schristos       case LC_ADDRESS:
788dffb485Schristos       case LC_TELEPHONE:
798dffb485Schristos       case LC_MEASUREMENT:
808dffb485Schristos         result = "C";
818dffb485Schristos         break;
828dffb485Schristos       default:
838dffb485Schristos         break;
848dffb485Schristos       }
858dffb485Schristos #endif
868dffb485Schristos 
878dffb485Schristos   return result;
888dffb485Schristos }
898dffb485Schristos 
908dffb485Schristos static int
setlocale_null_unlocked(int category,char * buf,size_t bufsize)918dffb485Schristos setlocale_null_unlocked (int category, char *buf, size_t bufsize)
928dffb485Schristos {
938dffb485Schristos #if defined _WIN32 && !defined __CYGWIN__ && defined _MSC_VER
948dffb485Schristos   /* On native Windows, nowadays, the setlocale() implementation is based
958dffb485Schristos      on _wsetlocale() and uses malloc() for the result.  We are better off
968dffb485Schristos      using _wsetlocale() directly.  */
978dffb485Schristos   const wchar_t *result = _wsetlocale (category, NULL);
988dffb485Schristos 
998dffb485Schristos   if (result == NULL)
1008dffb485Schristos     {
1018dffb485Schristos       /* CATEGORY is invalid.  */
1028dffb485Schristos       if (bufsize > 0)
1038dffb485Schristos         /* Return an empty string in BUF.
1048dffb485Schristos            This is a convenience for callers that don't want to write explicit
1058dffb485Schristos            code for handling EINVAL.  */
1068dffb485Schristos         buf[0] = '\0';
1078dffb485Schristos       return EINVAL;
1088dffb485Schristos     }
1098dffb485Schristos   else
1108dffb485Schristos     {
1118dffb485Schristos       size_t length = wcslen (result);
1128dffb485Schristos       if (length < bufsize)
1138dffb485Schristos         {
1148dffb485Schristos           size_t i;
1158dffb485Schristos 
1168dffb485Schristos           /* Convert wchar_t[] -> char[], assuming plain ASCII.  */
1178dffb485Schristos           for (i = 0; i <= length; i++)
1188dffb485Schristos             buf[i] = result[i];
1198dffb485Schristos 
1208dffb485Schristos           return 0;
1218dffb485Schristos         }
1228dffb485Schristos       else
1238dffb485Schristos         {
1248dffb485Schristos           if (bufsize > 0)
1258dffb485Schristos             {
1268dffb485Schristos               /* Return a truncated result in BUF.
1278dffb485Schristos                  This is a convenience for callers that don't want to write
1288dffb485Schristos                  explicit code for handling ERANGE.  */
1298dffb485Schristos               size_t i;
1308dffb485Schristos 
1318dffb485Schristos               /* Convert wchar_t[] -> char[], assuming plain ASCII.  */
1328dffb485Schristos               for (i = 0; i < bufsize; i++)
1338dffb485Schristos                 buf[i] = result[i];
1348dffb485Schristos               buf[bufsize - 1] = '\0';
1358dffb485Schristos             }
1368dffb485Schristos           return ERANGE;
1378dffb485Schristos         }
1388dffb485Schristos     }
1398dffb485Schristos #else
1408dffb485Schristos   const char *result = setlocale_null_androidfix (category);
1418dffb485Schristos 
1428dffb485Schristos   if (result == NULL)
1438dffb485Schristos     {
1448dffb485Schristos       /* CATEGORY is invalid.  */
1458dffb485Schristos       if (bufsize > 0)
1468dffb485Schristos         /* Return an empty string in BUF.
1478dffb485Schristos            This is a convenience for callers that don't want to write explicit
1488dffb485Schristos            code for handling EINVAL.  */
1498dffb485Schristos         buf[0] = '\0';
1508dffb485Schristos       return EINVAL;
1518dffb485Schristos     }
1528dffb485Schristos   else
1538dffb485Schristos     {
1548dffb485Schristos       size_t length = strlen (result);
1558dffb485Schristos       if (length < bufsize)
1568dffb485Schristos         {
1578dffb485Schristos           memcpy (buf, result, length + 1);
1588dffb485Schristos           return 0;
1598dffb485Schristos         }
1608dffb485Schristos       else
1618dffb485Schristos         {
1628dffb485Schristos           if (bufsize > 0)
1638dffb485Schristos             {
1648dffb485Schristos               /* Return a truncated result in BUF.
1658dffb485Schristos                  This is a convenience for callers that don't want to write
1668dffb485Schristos                  explicit code for handling ERANGE.  */
1678dffb485Schristos               memcpy (buf, result, bufsize - 1);
1688dffb485Schristos               buf[bufsize - 1] = '\0';
1698dffb485Schristos             }
1708dffb485Schristos           return ERANGE;
1718dffb485Schristos         }
1728dffb485Schristos     }
1738dffb485Schristos #endif
1748dffb485Schristos }
1758dffb485Schristos 
1768dffb485Schristos #if !(SETLOCALE_NULL_ALL_MTSAFE && SETLOCALE_NULL_ONE_MTSAFE) /* musl libc, macOS, FreeBSD, NetBSD, OpenBSD, AIX, Haiku, Cygwin */
1778dffb485Schristos 
1788dffb485Schristos /* Use a lock, so that no two threads can invoke setlocale_null_unlocked
1798dffb485Schristos    at the same time.  */
1808dffb485Schristos 
1818dffb485Schristos /* Prohibit renaming this symbol.  */
1828dffb485Schristos # undef gl_get_setlocale_null_lock
1838dffb485Schristos 
1848dffb485Schristos # if defined _WIN32 && !defined __CYGWIN__
1858dffb485Schristos 
1868dffb485Schristos extern __declspec(dllimport) CRITICAL_SECTION *gl_get_setlocale_null_lock (void);
1878dffb485Schristos 
1888dffb485Schristos static int
setlocale_null_with_lock(int category,char * buf,size_t bufsize)1898dffb485Schristos setlocale_null_with_lock (int category, char *buf, size_t bufsize)
1908dffb485Schristos {
1918dffb485Schristos   CRITICAL_SECTION *lock = gl_get_setlocale_null_lock ();
1928dffb485Schristos   int ret;
1938dffb485Schristos 
1948dffb485Schristos   EnterCriticalSection (lock);
1958dffb485Schristos   ret = setlocale_null_unlocked (category, buf, bufsize);
1968dffb485Schristos   LeaveCriticalSection (lock);
1978dffb485Schristos 
1988dffb485Schristos   return ret;
1998dffb485Schristos }
2008dffb485Schristos 
2018dffb485Schristos # elif HAVE_PTHREAD_API /* musl libc, macOS, FreeBSD, NetBSD, OpenBSD, AIX, Haiku, Cygwin */
2028dffb485Schristos 
2038dffb485Schristos extern
2048dffb485Schristos #  if defined _WIN32 || defined __CYGWIN__
2058dffb485Schristos   __declspec(dllimport)
2068dffb485Schristos #  endif
2078dffb485Schristos   pthread_mutex_t *gl_get_setlocale_null_lock (void);
2088dffb485Schristos 
2098dffb485Schristos #  if HAVE_WEAK_SYMBOLS /* musl libc, FreeBSD, NetBSD, OpenBSD, Haiku */
2108dffb485Schristos 
2118dffb485Schristos     /* Avoid the need to link with '-lpthread'.  */
2128dffb485Schristos #   pragma weak pthread_mutex_lock
2138dffb485Schristos #   pragma weak pthread_mutex_unlock
2148dffb485Schristos 
2158dffb485Schristos     /* Determine whether libpthread is in use.  */
2168dffb485Schristos #   pragma weak pthread_mutexattr_gettype
2178dffb485Schristos     /* See the comments in lock.h.  */
2188dffb485Schristos #   define pthread_in_use() \
2198dffb485Schristos       (pthread_mutexattr_gettype != NULL || c11_threads_in_use ())
2208dffb485Schristos 
2218dffb485Schristos #  else
2228dffb485Schristos #   define pthread_in_use() 1
2238dffb485Schristos #  endif
2248dffb485Schristos 
2258dffb485Schristos static int
setlocale_null_with_lock(int category,char * buf,size_t bufsize)2268dffb485Schristos setlocale_null_with_lock (int category, char *buf, size_t bufsize)
2278dffb485Schristos {
2288dffb485Schristos   if (pthread_in_use())
2298dffb485Schristos     {
2308dffb485Schristos       pthread_mutex_t *lock = gl_get_setlocale_null_lock ();
2318dffb485Schristos       int ret;
2328dffb485Schristos 
2338dffb485Schristos       if (pthread_mutex_lock (lock))
2348dffb485Schristos         abort ();
2358dffb485Schristos       ret = setlocale_null_unlocked (category, buf, bufsize);
2368dffb485Schristos       if (pthread_mutex_unlock (lock))
2378dffb485Schristos         abort ();
2388dffb485Schristos 
2398dffb485Schristos       return ret;
2408dffb485Schristos     }
2418dffb485Schristos   else
2428dffb485Schristos     return setlocale_null_unlocked (category, buf, bufsize);
2438dffb485Schristos }
2448dffb485Schristos 
2458dffb485Schristos # elif HAVE_THREADS_H
2468dffb485Schristos 
2478dffb485Schristos extern mtx_t *gl_get_setlocale_null_lock (void);
2488dffb485Schristos 
2498dffb485Schristos static int
setlocale_null_with_lock(int category,char * buf,size_t bufsize)2508dffb485Schristos setlocale_null_with_lock (int category, char *buf, size_t bufsize)
2518dffb485Schristos {
2528dffb485Schristos   mtx_t *lock = gl_get_setlocale_null_lock ();
2538dffb485Schristos   int ret;
2548dffb485Schristos 
2558dffb485Schristos   if (mtx_lock (lock) != thrd_success)
2568dffb485Schristos     abort ();
2578dffb485Schristos   ret = setlocale_null_unlocked (category, buf, bufsize);
2588dffb485Schristos   if (mtx_unlock (lock) != thrd_success)
2598dffb485Schristos     abort ();
2608dffb485Schristos 
2618dffb485Schristos   return ret;
2628dffb485Schristos }
2638dffb485Schristos 
2648dffb485Schristos # endif
2658dffb485Schristos 
2668dffb485Schristos #endif
2678dffb485Schristos 
2688dffb485Schristos int
setlocale_null_r(int category,char * buf,size_t bufsize)2698dffb485Schristos setlocale_null_r (int category, char *buf, size_t bufsize)
2708dffb485Schristos {
2718dffb485Schristos #if SETLOCALE_NULL_ALL_MTSAFE
2728dffb485Schristos # if SETLOCALE_NULL_ONE_MTSAFE
2738dffb485Schristos 
2748dffb485Schristos   return setlocale_null_unlocked (category, buf, bufsize);
2758dffb485Schristos 
2768dffb485Schristos # else
2778dffb485Schristos 
2788dffb485Schristos   if (category == LC_ALL)
2798dffb485Schristos     return setlocale_null_unlocked (category, buf, bufsize);
2808dffb485Schristos   else
2818dffb485Schristos     return setlocale_null_with_lock (category, buf, bufsize);
2828dffb485Schristos 
2838dffb485Schristos # endif
2848dffb485Schristos #else
2858dffb485Schristos # if SETLOCALE_NULL_ONE_MTSAFE
2868dffb485Schristos 
2878dffb485Schristos   if (category == LC_ALL)
2888dffb485Schristos     return setlocale_null_with_lock (category, buf, bufsize);
2898dffb485Schristos   else
2908dffb485Schristos     return setlocale_null_unlocked (category, buf, bufsize);
2918dffb485Schristos 
2928dffb485Schristos # else
2938dffb485Schristos 
2948dffb485Schristos   return setlocale_null_with_lock (category, buf, bufsize);
2958dffb485Schristos 
2968dffb485Schristos # endif
2978dffb485Schristos #endif
2988dffb485Schristos }
2998dffb485Schristos 
3008dffb485Schristos const char *
setlocale_null(int category)3018dffb485Schristos setlocale_null (int category)
3028dffb485Schristos {
3038dffb485Schristos #if SETLOCALE_NULL_ALL_MTSAFE && SETLOCALE_NULL_ONE_MTSAFE
3048dffb485Schristos   return setlocale_null_androidfix (category);
3058dffb485Schristos #else
3068dffb485Schristos 
3078dffb485Schristos   /* This call must be multithread-safe.  To achieve this without using
3088dffb485Schristos      thread-local storage:
3098dffb485Schristos        1. We use a specific static buffer for each possible CATEGORY
3108dffb485Schristos           argument.  So that different threads can call setlocale_mtsafe
3118dffb485Schristos           with different CATEGORY arguments, without interfering.
3128dffb485Schristos        2. We use a simple strcpy or memcpy to fill this static buffer.
3138dffb485Schristos           Filling it through, for example, strcpy + strcat would not be
3148dffb485Schristos           guaranteed to leave the buffer's contents intact if another thread
3158dffb485Schristos           is currently accessing it.  If necessary, the contents is first
3168dffb485Schristos           assembled in a stack-allocated buffer.  */
3178dffb485Schristos   if (category == LC_ALL)
3188dffb485Schristos     {
3198dffb485Schristos # if SETLOCALE_NULL_ALL_MTSAFE
3208dffb485Schristos       return setlocale_null_androidfix (LC_ALL);
3218dffb485Schristos # else
3228dffb485Schristos       char buf[SETLOCALE_NULL_ALL_MAX];
3238dffb485Schristos       static char resultbuf[SETLOCALE_NULL_ALL_MAX];
3248dffb485Schristos 
3258dffb485Schristos       if (setlocale_null_r (LC_ALL, buf, sizeof (buf)))
3268dffb485Schristos         return "C";
3278dffb485Schristos       strcpy (resultbuf, buf);
3288dffb485Schristos       return resultbuf;
3298dffb485Schristos # endif
3308dffb485Schristos     }
3318dffb485Schristos   else
3328dffb485Schristos     {
3338dffb485Schristos # if SETLOCALE_NULL_ONE_MTSAFE
3348dffb485Schristos       return setlocale_null_androidfix (category);
3358dffb485Schristos # else
3368dffb485Schristos       enum
3378dffb485Schristos         {
3388dffb485Schristos           LC_CTYPE_INDEX,
3398dffb485Schristos           LC_NUMERIC_INDEX,
3408dffb485Schristos           LC_TIME_INDEX,
3418dffb485Schristos           LC_COLLATE_INDEX,
3428dffb485Schristos           LC_MONETARY_INDEX,
3438dffb485Schristos           LC_MESSAGES_INDEX,
3448dffb485Schristos #  ifdef LC_PAPER
3458dffb485Schristos           LC_PAPER_INDEX,
3468dffb485Schristos #  endif
3478dffb485Schristos #  ifdef LC_NAME
3488dffb485Schristos           LC_NAME_INDEX,
3498dffb485Schristos #  endif
3508dffb485Schristos #  ifdef LC_ADDRESS
3518dffb485Schristos           LC_ADDRESS_INDEX,
3528dffb485Schristos #  endif
3538dffb485Schristos #  ifdef LC_TELEPHONE
3548dffb485Schristos           LC_TELEPHONE_INDEX,
3558dffb485Schristos #  endif
3568dffb485Schristos #  ifdef LC_MEASUREMENT
3578dffb485Schristos           LC_MEASUREMENT_INDEX,
3588dffb485Schristos #  endif
3598dffb485Schristos #  ifdef LC_IDENTIFICATION
3608dffb485Schristos           LC_IDENTIFICATION_INDEX,
3618dffb485Schristos #  endif
3628dffb485Schristos           LC_INDICES_COUNT
3638dffb485Schristos         }
3648dffb485Schristos         i;
3658dffb485Schristos       char buf[SETLOCALE_NULL_MAX];
3668dffb485Schristos       static char resultbuf[LC_INDICES_COUNT][SETLOCALE_NULL_MAX];
3678dffb485Schristos       int err;
3688dffb485Schristos 
3698dffb485Schristos       err = setlocale_null_r (category, buf, sizeof (buf));
3708dffb485Schristos       if (err == EINVAL)
3718dffb485Schristos         return NULL;
3728dffb485Schristos       if (err)
3738dffb485Schristos         return "C";
3748dffb485Schristos 
3758dffb485Schristos       switch (category)
3768dffb485Schristos         {
3778dffb485Schristos         case LC_CTYPE:          i = LC_CTYPE_INDEX;          break;
3788dffb485Schristos         case LC_NUMERIC:        i = LC_NUMERIC_INDEX;        break;
3798dffb485Schristos         case LC_TIME:           i = LC_TIME_INDEX;           break;
3808dffb485Schristos         case LC_COLLATE:        i = LC_COLLATE_INDEX;        break;
3818dffb485Schristos         case LC_MONETARY:       i = LC_MONETARY_INDEX;       break;
3828dffb485Schristos         case LC_MESSAGES:       i = LC_MESSAGES_INDEX;       break;
3838dffb485Schristos #  ifdef LC_PAPER
3848dffb485Schristos         case LC_PAPER:          i = LC_PAPER_INDEX;          break;
3858dffb485Schristos #  endif
3868dffb485Schristos #  ifdef LC_NAME
3878dffb485Schristos         case LC_NAME:           i = LC_NAME_INDEX;           break;
3888dffb485Schristos #  endif
3898dffb485Schristos #  ifdef LC_ADDRESS
3908dffb485Schristos         case LC_ADDRESS:        i = LC_ADDRESS_INDEX;        break;
3918dffb485Schristos #  endif
3928dffb485Schristos #  ifdef LC_TELEPHONE
3938dffb485Schristos         case LC_TELEPHONE:      i = LC_TELEPHONE_INDEX;      break;
3948dffb485Schristos #  endif
3958dffb485Schristos #  ifdef LC_MEASUREMENT
3968dffb485Schristos         case LC_MEASUREMENT:    i = LC_MEASUREMENT_INDEX;    break;
3978dffb485Schristos #  endif
3988dffb485Schristos #  ifdef LC_IDENTIFICATION
3998dffb485Schristos         case LC_IDENTIFICATION: i = LC_IDENTIFICATION_INDEX; break;
4008dffb485Schristos #  endif
4018dffb485Schristos         default:
4028dffb485Schristos           /* If you get here, a #ifdef LC_xxx is missing.  */
4038dffb485Schristos           abort ();
4048dffb485Schristos         }
4058dffb485Schristos 
4068dffb485Schristos       strcpy (resultbuf[i], buf);
4078dffb485Schristos       return resultbuf[i];
4088dffb485Schristos # endif
4098dffb485Schristos     }
4108dffb485Schristos #endif
4118dffb485Schristos }
412