Merge from vendor branch BIND:
[dragonfly.git] / contrib / diffutils-2.8.1 / lib / hard-locale.c
1 /* hard-locale.c -- Determine whether a locale is hard.
2
3    Copyright (C) 1997, 1998, 1999, 2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #if HAVE_LOCALE_H
24 # include <locale.h>
25 #endif
26
27 #if HAVE_STDLIB_H
28 # include <stdlib.h>
29 #endif
30
31 #if HAVE_STRING_H
32 # include <string.h>
33 #endif
34
35 #include "hard-locale.h"
36
37 /* Return nonzero if the current CATEGORY locale is hard, i.e. if you
38    can't get away with assuming traditional C or POSIX behavior.  */
39 int
40 hard_locale (int category)
41 {
42 #if ! HAVE_SETLOCALE
43   return 0;
44 #else
45
46   int hard = 1;
47   char const *p = setlocale (category, 0);
48
49   if (p)
50     {
51 # if defined __GLIBC__ && 2 <= __GLIBC__
52       if (strcmp (p, "C") == 0 || strcmp (p, "POSIX") == 0)
53         hard = 0;
54 # else
55       char *locale = malloc (strlen (p) + 1);
56       if (locale)
57         {
58           strcpy (locale, p);
59
60           /* Temporarily set the locale to the "C" and "POSIX" locales
61              to find their names, so that we can determine whether one
62              or the other is the caller's locale.  */
63           if (((p = setlocale (category, "C"))
64                && strcmp (p, locale) == 0)
65               || ((p = setlocale (category, "POSIX"))
66                   && strcmp (p, locale) == 0))
67             hard = 0;
68
69           /* Restore the caller's locale.  */
70           setlocale (category, locale);
71           free (locale);
72         }
73 # endif
74     }
75
76   return hard;
77
78 #endif
79 }