1/* hard-locale.c -- Determine whether a locale is hard.
2
3   Copyright (C) 1997, 1998, 1999, 2002, 2003 Free Software Foundation, Inc.
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2, or (at your option)
8   any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software Foundation,
17   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19#if HAVE_CONFIG_H
20# include <config.h>
21#endif
22
23#include "hard-locale.h"
24
25#if HAVE_LOCALE_H
26# include <locale.h>
27#endif
28
29#include <stdlib.h>
30#include <string.h>
31
32/* Return nonzero if the current CATEGORY locale is hard, i.e. if you
33   can't get away with assuming traditional C or POSIX behavior.  */
34int
35hard_locale (int category)
36{
37#if ! HAVE_SETLOCALE
38  return 0;
39#else
40
41  int hard = 1;
42  char const *p = setlocale (category, 0);
43
44  if (p)
45    {
46# if defined __GLIBC__ && 2 <= __GLIBC__
47      if (strcmp (p, "C") == 0 || strcmp (p, "POSIX") == 0)
48	hard = 0;
49# else
50      char *locale = malloc (strlen (p) + 1);
51      if (locale)
52	{
53	  strcpy (locale, p);
54
55	  /* Temporarily set the locale to the "C" and "POSIX" locales
56	     to find their names, so that we can determine whether one
57	     or the other is the caller's locale.  */
58	  if (((p = setlocale (category, "C"))
59	       && strcmp (p, locale) == 0)
60	      || ((p = setlocale (category, "POSIX"))
61		  && strcmp (p, locale) == 0))
62	    hard = 0;
63
64	  /* Restore the caller's locale.  */
65	  setlocale (category, locale);
66	  free (locale);
67	}
68# endif
69    }
70
71  return hard;
72
73#endif
74}
75