Blame lib/hard-locale.c

Packit 8f70b4
/* hard-locale.c -- Determine whether a locale is hard.
Packit 8f70b4
Packit 8f70b4
   Copyright (C) 1997-1999, 2002-2004, 2006-2007, 2009-2018 Free Software
Packit 8f70b4
   Foundation, Inc.
Packit 8f70b4
Packit 8f70b4
   This program is free software: you can redistribute it and/or modify
Packit 8f70b4
   it under the terms of the GNU General Public License as published by
Packit 8f70b4
   the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
   (at your option) any later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
   GNU General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License
Packit 8f70b4
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
#include "hard-locale.h"
Packit 8f70b4
Packit 8f70b4
#include <locale.h>
Packit 8f70b4
#include <stdlib.h>
Packit 8f70b4
#include <string.h>
Packit 8f70b4
Packit 8f70b4
#ifdef __GLIBC__
Packit 8f70b4
# define GLIBC_VERSION __GLIBC__
Packit 8f70b4
#elif defined __UCLIBC__
Packit 8f70b4
# define GLIBC_VERSION 2
Packit 8f70b4
#else
Packit 8f70b4
# define GLIBC_VERSION 0
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
/* Return true if the current CATEGORY locale is hard, i.e. if you
Packit 8f70b4
   can't get away with assuming traditional C or POSIX behavior.  */
Packit 8f70b4
bool
Packit 8f70b4
hard_locale (int category)
Packit 8f70b4
{
Packit 8f70b4
  bool hard = true;
Packit 8f70b4
  char const *p = setlocale (category, NULL);
Packit 8f70b4
Packit 8f70b4
  if (p)
Packit 8f70b4
    {
Packit 8f70b4
      if (2 <= GLIBC_VERSION)
Packit 8f70b4
        {
Packit 8f70b4
          if (strcmp (p, "C") == 0 || strcmp (p, "POSIX") == 0)
Packit 8f70b4
            hard = false;
Packit 8f70b4
        }
Packit 8f70b4
      else
Packit 8f70b4
        {
Packit 8f70b4
          char *locale = strdup (p);
Packit 8f70b4
          if (locale)
Packit 8f70b4
            {
Packit 8f70b4
              /* Temporarily set the locale to the "C" and "POSIX" locales
Packit 8f70b4
                 to find their names, so that we can determine whether one
Packit 8f70b4
                 or the other is the caller's locale.  */
Packit 8f70b4
              if (((p = setlocale (category, "C"))
Packit 8f70b4
                   && strcmp (p, locale) == 0)
Packit 8f70b4
                  || ((p = setlocale (category, "POSIX"))
Packit 8f70b4
                      && strcmp (p, locale) == 0))
Packit 8f70b4
                hard = false;
Packit 8f70b4
Packit 8f70b4
              /* Restore the caller's locale.  */
Packit 8f70b4
              setlocale (category, locale);
Packit 8f70b4
              free (locale);
Packit 8f70b4
            }
Packit 8f70b4
        }
Packit 8f70b4
    }
Packit 8f70b4
Packit 8f70b4
  return hard;
Packit 8f70b4
}