1170754Sdelphij/* hard-locale.c -- Determine whether a locale is hard.
2170754Sdelphij
3170754Sdelphij   Copyright (C) 1997, 1998, 1999, 2002, 2003 Free Software Foundation, Inc.
4170754Sdelphij
5170754Sdelphij   This program is free software; you can redistribute it and/or modify
6170754Sdelphij   it under the terms of the GNU General Public License as published by
7170754Sdelphij   the Free Software Foundation; either version 2, or (at your option)
8170754Sdelphij   any later version.
9170754Sdelphij
10170754Sdelphij   This program is distributed in the hope that it will be useful,
11170754Sdelphij   but WITHOUT ANY WARRANTY; without even the implied warranty of
12170754Sdelphij   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13170754Sdelphij   GNU General Public License for more details.
14170754Sdelphij
15170754Sdelphij   You should have received a copy of the GNU General Public License
16170754Sdelphij   along with this program; if not, write to the Free Software Foundation,
17170754Sdelphij   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18170754Sdelphij
19170754Sdelphij#if HAVE_CONFIG_H
20170754Sdelphij# include <config.h>
21170754Sdelphij#endif
22170754Sdelphij
23170754Sdelphij#include "hard-locale.h"
24170754Sdelphij
25170754Sdelphij#if HAVE_LOCALE_H
26170754Sdelphij# include <locale.h>
27170754Sdelphij#endif
28170754Sdelphij
29170754Sdelphij#include <stdlib.h>
30170754Sdelphij#include <string.h>
31170754Sdelphij
32170754Sdelphij/* Return nonzero if the current CATEGORY locale is hard, i.e. if you
33170754Sdelphij   can't get away with assuming traditional C or POSIX behavior.  */
34170754Sdelphijint
35170754Sdelphijhard_locale (int category)
36170754Sdelphij{
37170754Sdelphij#if ! HAVE_SETLOCALE
38170754Sdelphij  return 0;
39170754Sdelphij#else
40170754Sdelphij
41170754Sdelphij  int hard = 1;
42170754Sdelphij  char const *p = setlocale (category, 0);
43170754Sdelphij
44170754Sdelphij  if (p)
45170754Sdelphij    {
46170754Sdelphij# if defined __GLIBC__ && 2 <= __GLIBC__
47170754Sdelphij      if (strcmp (p, "C") == 0 || strcmp (p, "POSIX") == 0)
48170754Sdelphij	hard = 0;
49170754Sdelphij# else
50170754Sdelphij      char *locale = malloc (strlen (p) + 1);
51170754Sdelphij      if (locale)
52170754Sdelphij	{
53170754Sdelphij	  strcpy (locale, p);
54170754Sdelphij
55170754Sdelphij	  /* Temporarily set the locale to the "C" and "POSIX" locales
56170754Sdelphij	     to find their names, so that we can determine whether one
57170754Sdelphij	     or the other is the caller's locale.  */
58170754Sdelphij	  if (((p = setlocale (category, "C"))
59170754Sdelphij	       && strcmp (p, locale) == 0)
60170754Sdelphij	      || ((p = setlocale (category, "POSIX"))
61170754Sdelphij		  && strcmp (p, locale) == 0))
62170754Sdelphij	    hard = 0;
63170754Sdelphij
64170754Sdelphij	  /* Restore the caller's locale.  */
65170754Sdelphij	  setlocale (category, locale);
66170754Sdelphij	  free (locale);
67170754Sdelphij	}
68170754Sdelphij# endif
69170754Sdelphij    }
70170754Sdelphij
71170754Sdelphij  return hard;
72170754Sdelphij
73170754Sdelphij#endif
74170754Sdelphij}
75