1131554Stjr/* hard-locale.c -- Determine whether a locale is hard.
2131554Stjr   Copyright 1997, 1998, 1999 Free Software Foundation, Inc.
3131554Stjr
4131554Stjr   This program is free software; you can redistribute it and/or modify
5131554Stjr   it under the terms of the GNU General Public License as published by
6131554Stjr   the Free Software Foundation; either version 2, or (at your option)
7131554Stjr   any later version.
8131554Stjr
9131554Stjr   This program is distributed in the hope that it will be useful,
10131554Stjr   but WITHOUT ANY WARRANTY; without even the implied warranty of
11131554Stjr   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12131554Stjr   GNU General Public License for more details.
13131554Stjr
14131554Stjr   You should have received a copy of the GNU General Public License
15131554Stjr   along with this program; if not, write to the Free Software Foundation,
16131554Stjr   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17131554Stjr
18131560Stjr/* $FreeBSD$ */
19131560Stjr
20131554Stjr#if HAVE_CONFIG_H
21131554Stjr# include <config.h>
22131554Stjr#endif
23131554Stjr
24131554Stjr#ifndef __GNUC__
25131554Stjr# ifdef HAVE_ALLOCA_H
26131554Stjr#  include <alloca.h>
27131554Stjr# else
28131554Stjr#  ifdef _AIX
29131554Stjr #  pragma alloca
30131554Stjr#  else
31131554Stjr#   ifdef _WIN32
32131554Stjr#    include <malloc.h>
33131554Stjr#    include <io.h>
34131554Stjr#   else
35131554Stjr#    ifndef alloca
36131554Stjrchar *alloca ();
37131554Stjr#    endif
38131554Stjr#   endif
39131554Stjr#  endif
40131554Stjr# endif
41131554Stjr#endif
42131554Stjr
43131554Stjr#if HAVE_LOCALE_H
44131554Stjr# include <locale.h>
45131554Stjr#endif
46131554Stjr
47131554Stjr#if HAVE_STRING_H
48131554Stjr# include <string.h>
49131554Stjr#endif
50131554Stjr
51131554Stjr/* Return nonzero if the current CATEGORY locale is hard, i.e. if you
52131554Stjr   can't get away with assuming traditional C or POSIX behavior.  */
53131554Stjrint
54131554Stjrhard_locale (int category)
55131554Stjr{
56131560Stjr#if ! HAVE_SETLOCALE
57131554Stjr  return 0;
58131554Stjr#else
59131554Stjr
60131554Stjr  int hard = 1;
61131554Stjr  char const *p = setlocale (category, 0);
62131554Stjr
63131554Stjr  if (p)
64131554Stjr    {
65131560Stjr# if defined(__FreeBSD__) || (defined __GLIBC__ && __GLIBC__ >= 2)
66131554Stjr      if (strcmp (p, "C") == 0 || strcmp (p, "POSIX") == 0)
67131554Stjr	hard = 0;
68131554Stjr# else
69131554Stjr      char *locale = alloca (strlen (p) + 1);
70131554Stjr      strcpy (locale, p);
71131554Stjr
72131554Stjr      /* Temporarily set the locale to the "C" and "POSIX" locales to
73131554Stjr	 find their names, so that we can determine whether one or the
74131554Stjr	 other is the caller's locale.  */
75131554Stjr      if (((p = setlocale (category, "C")) && strcmp (p, locale) == 0)
76131554Stjr	  || ((p = setlocale (category, "POSIX")) && strcmp (p, locale) == 0))
77131554Stjr	hard = 0;
78131554Stjr
79131554Stjr      /* Restore the caller's locale.  */
80131554Stjr      setlocale (category, locale);
81131554Stjr# endif
82131554Stjr    }
83131554Stjr
84131554Stjr  return hard;
85131554Stjr
86131554Stjr#endif
87131554Stjr}
88