xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/libgrep/hard-locale.c (revision 901e7e84758515fbf39dfc064cb0b45ab146d8b0)
1 /* hard-locale.c -- Determine whether a locale is hard.
2 
3    Copyright (C) 1997-1999, 2002-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18 
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 
23 #include "hard-locale.h"
24 
25 #include <locale.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "strdup.h"
30 
31 #ifdef __GLIBC__
32 # define GLIBC_VERSION __GLIBC__
33 #else
34 # define GLIBC_VERSION 0
35 #endif
36 
37 /* Return true if the current CATEGORY locale is hard, i.e. if you
38    can't get away with assuming traditional C or POSIX behavior.  */
39 bool
40 hard_locale (int category)
41 {
42   bool hard = true;
43   char const *p = setlocale (category, NULL);
44 
45   if (p)
46     {
47       if (2 <= GLIBC_VERSION)
48 	{
49 	  if (strcmp (p, "C") == 0 || strcmp (p, "POSIX") == 0)
50 	    hard = false;
51 	}
52       else
53 	{
54 	  char *locale = strdup (p);
55 	  if (locale)
56 	    {
57 	      /* Temporarily set the locale to the "C" and "POSIX" locales
58 		 to find their names, so that we can determine whether one
59 		 or the other is the caller's locale.  */
60 	      if (((p = setlocale (category, "C"))
61 		   && strcmp (p, locale) == 0)
62 		  || ((p = setlocale (category, "POSIX"))
63 		      && strcmp (p, locale) == 0))
64 		hard = false;
65 
66 	      /* Restore the caller's locale.  */
67 	      setlocale (category, locale);
68 	      free (locale);
69 	    }
70 	}
71     }
72 
73   return hard;
74 }
75