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