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