]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Assume <locale.h> exists.
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 2 Dec 2004 06:51:46 +0000 (06:51 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 2 Dec 2004 06:51:46 +0000 (06:51 +0000)
Include "strdup.h".
(GLIBC_VERSION): New macro.
(hard_locale): Assume setlocale exists.
Rewrite to avoid #ifdef.
Use strdup rather than malloc + strcpy.

lib/hard-locale.c

index 67a4144a6fec74d12a3addac772c0eb20c2c70e7..cc2c9becfab1f0238b21d042a3730f0ab64dbf87 100644 (file)
 
 #include "hard-locale.h"
 
-#if HAVE_LOCALE_H
-# include <locale.h>
-#endif
-
+#include <locale.h>
 #include <stdlib.h>
 #include <string.h>
 
+#include "strdup.h"
+
+#ifdef __GLIBC__
+# define GLIBC_VERSION __GLIBC__
+#else
+# define GLIBC_VERSION 0
+#endif
+
 /* Return true if the current CATEGORY locale is hard, i.e. if you
    can't get away with assuming traditional C or POSIX behavior.  */
 bool
 hard_locale (int category)
 {
-#if ! HAVE_SETLOCALE
-  return false;
-#else
-
   bool hard = true;
   char const *p = setlocale (category, NULL);
 
   if (p)
     {
-# if defined __GLIBC__ && 2 <= __GLIBC__
-      if (strcmp (p, "C") == 0 || strcmp (p, "POSIX") == 0)
-       hard = false;
-# else
-      char *locale = malloc (strlen (p) + 1);
-      if (locale)
+      if (2 <= GLIBC_VERSION)
        {
-         strcpy (locale, p);
-
-         /* Temporarily set the locale to the "C" and "POSIX" locales
-            to find their names, so that we can determine whether one
-            or the other is the caller's locale.  */
-         if (((p = setlocale (category, "C"))
-              && strcmp (p, locale) == 0)
-             || ((p = setlocale (category, "POSIX"))
-                 && strcmp (p, locale) == 0))
+         if (strcmp (p, "C") == 0 || strcmp (p, "POSIX") == 0)
            hard = false;
+       }
+      else
+       {
+         char *locale = strdup (p);
+         if (locale)
+           {
+             /* Temporarily set the locale to the "C" and "POSIX" locales
+                to find their names, so that we can determine whether one
+                or the other is the caller's locale.  */
+             if (((p = setlocale (category, "C"))
+                  && strcmp (p, locale) == 0)
+                 || ((p = setlocale (category, "POSIX"))
+                     && strcmp (p, locale) == 0))
+               hard = false;
 
-         /* Restore the caller's locale.  */
-         setlocale (category, locale);
-         free (locale);
+             /* Restore the caller's locale.  */
+             setlocale (category, locale);
+             free (locale);
+           }
        }
-# endif
     }
 
   return hard;
-
-#endif
 }