fdl.texi from gnustandards
[gnulib.git] / lib / localename.c
index 03328b9..ea646c5 100644 (file)
@@ -1,5 +1,5 @@
 /* Determine name of the currently selected locale.
-   Copyright (C) 1995-1999, 2000-2009 Free Software Foundation, Inc.
+   Copyright (C) 1995-2010 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published
@@ -2592,20 +2592,22 @@ struniq (const char *string)
 #endif
 
 
+#if defined IN_LIBINTL || HAVE_USELOCALE
+
 /* Like gl_locale_name_thread, except that the result is not in storage of
    indefinite extent.  */
-#if !defined IN_LIBINTL
+# if !defined IN_LIBINTL
 static
-#endif
+# endif
 const char *
 gl_locale_name_thread_unsafe (int category, const char *categoryname)
 {
-#if HAVE_USELOCALE
+# if HAVE_USELOCALE
   {
     locale_t thread_locale = uselocale (NULL);
     if (thread_locale != LC_GLOBAL_LOCALE)
       {
-# if __GLIBC__ >= 2
+#  if __GLIBC__ >= 2
         /* Work around an incorrect definition of the _NL_LOCALE_NAME macro in
            glibc < 2.12.
            See <http://sourceware.org/bugzilla/show_bug.cgi?id=10968>.  */
@@ -2616,18 +2618,30 @@ gl_locale_name_thread_unsafe (int category, const char *categoryname)
              nl_langinfo (_NL_LOCALE_NAME (category)).  */
           name = thread_locale->__names[category];
         return name;
-# endif
-# if defined __APPLE__ && defined __MACH__ /* MacOS X */
+#  endif
+#  if defined __APPLE__ && defined __MACH__ /* MacOS X */
         /* The locale name is found deep in an undocumented data structure.
            Since it's stored in a buffer of size 32 and newlocale() rejects
            locale names of length > 31, we can assume that it is NUL terminated
            in this buffer. But we need to make a copy of the locale name, of
            indefinite extent.  */
-        struct _xlocale
+        struct _xlocale_part1_v0 /* used in MacOS X 10.5 */
+          {
+            int32_t __refcount;
+            void (*__free_extra)(void *);
+            __darwin_mbstate_t __mbs[10];
+            int64_t __magic;
+          };
+        struct _xlocale_part1_v1 /* used in MacOS X >= 10.6.0 */
           {
             int32_t __refcount;
             void (*__free_extra)(void *);
             __darwin_mbstate_t __mbs[10];
+            /*pthread_lock_t*/ int __lock;
+            int64_t __magic;
+          };
+        struct _xlocale_part2
+          {
             int64_t __magic;
             unsigned char __collate_load_error;
             unsigned char __collate_substitute_nontrivial;
@@ -2687,7 +2701,23 @@ gl_locale_name_thread_unsafe (int category, const char *categoryname)
             char *_time_locale_buf;
             /* more */
           };
-        struct _xlocale *tlp = (struct _xlocale *) thread_locale;
+        struct _xlocale_part2 *tlp;
+        if (((struct _xlocale_part1_v0 *) thread_locale)->__magic
+            == 0x786C6F63616C6530LL)
+          /* MacOS X 10.5 */
+          tlp =
+            (struct _xlocale_part2 *)
+            &((struct _xlocale_part1_v0 *) thread_locale)->__magic;
+        else if (((struct _xlocale_part1_v1 *) thread_locale)->__magic
+                 == 0x786C6F63616C6530LL)
+          /* MacOS X >= 10.6.0 */
+          tlp =
+            (struct _xlocale_part2 *)
+            &((struct _xlocale_part1_v1 *) thread_locale)->__magic;
+        else
+          /* Unsupported version of MacOS X: The internals of 'struct _xlocale'
+             have changed again.  */
+          return "";
         switch (category)
           {
           case LC_CTYPE:
@@ -2715,13 +2745,15 @@ gl_locale_name_thread_unsafe (int category, const char *categoryname)
           default: /* We shouldn't get here.  */
             return "";
           }
-# endif
+#  endif
       }
   }
-#endif
+# endif
   return NULL;
 }
 
+#endif
+
 const char *
 gl_locale_name_thread (int category, const char *categoryname)
 {
@@ -2862,7 +2894,7 @@ gl_locale_name_default (void)
         CFLocaleRef locale = CFLocaleCopyCurrent ();
         CFStringRef name = CFLocaleGetIdentifier (locale);
 
-        if (CFStringGetCString (name, namebuf, sizeof(namebuf),
+        if (CFStringGetCString (name, namebuf, sizeof (namebuf),
                                 kCFStringEncodingASCII))
           {
             gl_locale_name_canonicalize (namebuf);
@@ -2875,7 +2907,8 @@ gl_locale_name_default (void)
                                      kCFPreferencesCurrentApplication);
         if (value != NULL
             && CFGetTypeID (value) == CFStringGetTypeID ()
-            && CFStringGetCString ((CFStringRef)value, namebuf, sizeof(namebuf),
+            && CFStringGetCString ((CFStringRef)value,
+                                   namebuf, sizeof (namebuf),
                                    kCFStringEncodingASCII))
           {
             gl_locale_name_canonicalize (namebuf);