Blame lib/hard-locale.c

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