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