Blame lib/hard-locale.c

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