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