]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - gnu/usr.bin/grep/hard-locale.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / gnu / usr.bin / grep / hard-locale.c
1 /* hard-locale.c -- Determine whether a locale is hard.
2    Copyright 1997, 1998, 1999 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* $FreeBSD$ */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #ifndef __GNUC__
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 /* Return nonzero if the current CATEGORY locale is hard, i.e. if you
52    can't get away with assuming traditional C or POSIX behavior.  */
53 int
54 hard_locale (int category)
55 {
56 #if ! HAVE_SETLOCALE
57   return 0;
58 #else
59
60   int hard = 1;
61   char const *p = setlocale (category, 0);
62
63   if (p)
64     {
65 # if defined(__FreeBSD__) || (defined __GLIBC__ && __GLIBC__ >= 2)
66       if (strcmp (p, "C") == 0 || strcmp (p, "POSIX") == 0)
67         hard = 0;
68 # else
69       char *locale = alloca (strlen (p) + 1);
70       strcpy (locale, p);
71
72       /* Temporarily set the locale to the "C" and "POSIX" locales to
73          find their names, so that we can determine whether one or the
74          other is the caller's locale.  */
75       if (((p = setlocale (category, "C")) && strcmp (p, locale) == 0)
76           || ((p = setlocale (category, "POSIX")) && strcmp (p, locale) == 0))
77         hard = 0;
78
79       /* Restore the caller's locale.  */
80       setlocale (category, locale);
81 # endif
82     }
83
84   return hard;
85
86 #endif
87 }