Blame lib/hard-locale.c

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