hard-locale.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* hard-locale.c -- Determine whether a locale is hard.
  2. Copyright (C) 1997-1999, 2002-2004, 2006-2007, 2009-2017 Free Software
  3. Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include "hard-locale.h"
  16. #include <locale.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #ifdef __GLIBC__
  20. # define GLIBC_VERSION __GLIBC__
  21. #elif defined __UCLIBC__
  22. # define GLIBC_VERSION 2
  23. #else
  24. # define GLIBC_VERSION 0
  25. #endif
  26. /* Return true if the current CATEGORY locale is hard, i.e. if you
  27. can't get away with assuming traditional C or POSIX behavior. */
  28. bool
  29. hard_locale (int category)
  30. {
  31. bool hard = true;
  32. char const *p = setlocale (category, NULL);
  33. if (p)
  34. {
  35. if (2 <= GLIBC_VERSION)
  36. {
  37. if (strcmp (p, "C") == 0 || strcmp (p, "POSIX") == 0)
  38. hard = false;
  39. }
  40. else
  41. {
  42. char *locale = strdup (p);
  43. if (locale)
  44. {
  45. /* Temporarily set the locale to the "C" and "POSIX" locales
  46. to find their names, so that we can determine whether one
  47. or the other is the caller's locale. */
  48. if (((p = setlocale (category, "C"))
  49. && strcmp (p, locale) == 0)
  50. || ((p = setlocale (category, "POSIX"))
  51. && strcmp (p, locale) == 0))
  52. hard = false;
  53. /* Restore the caller's locale. */
  54. setlocale (category, locale);
  55. free (locale);
  56. }
  57. }
  58. }
  59. return hard;
  60. }