Rocco Rutte:
[apps/madmutt.git] / intl / localealias.c
index f57d764..7a092a0 100644 (file)
@@ -1,5 +1,5 @@
 /* Handle aliases for locale names.
-   Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
+   Copyright (C) 1995-1999, 2000-2001, 2003 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
@@ -13,7 +13,7 @@
 
    You should have received a copy of the GNU Library General Public
    License along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    USA.  */
 
 /* Tell glibc's <string.h> to provide a prototype for mempcpy().
 
 #include <ctype.h>
 #include <stdio.h>
+#if defined _LIBC || defined HAVE___FSETLOCKING
+# include <stdio_ext.h>
+#endif
 #include <sys/types.h>
 
 #ifdef __GNUC__
+# undef alloca
 # define alloca __builtin_alloca
 # define HAVE_ALLOCA 1
 #else
-# if defined HAVE_ALLOCA_H || defined _LIBC
-#  include <alloca.h>
+# ifdef _MSC_VER
+#  include <malloc.h>
+#  define alloca _alloca
 # else
-#  ifdef _AIX
- #pragma alloca
+#  if defined HAVE_ALLOCA_H || defined _LIBC
+#   include <alloca.h>
 #  else
-#   ifndef alloca
+#   ifdef _AIX
+ #pragma alloca
+#   else
+#    ifndef alloca
 char *alloca ();
+#    endif
 #   endif
 #  endif
 # endif
 #endif
 
 #include <stdlib.h>
-
 #include <string.h>
-#if !HAVE_STRCHR && !defined _LIBC
-# ifndef strchr
-#  define strchr index
-# endif
-#endif
 
 #include "gettextP.h"
 
+#if ENABLE_RELOCATABLE
+# include "relocatable.h"
+#else
+# define relocate(pathname) (pathname)
+#endif
+
 /* @@ end of prolog @@ */
 
 #ifdef _LIBC
@@ -71,6 +80,7 @@ char *alloca ();
 #  define mempcpy __mempcpy
 # endif
 # define HAVE_MEMPCPY  1
+# define HAVE___FSETLOCKING    1
 
 /* We need locking here since we can be called from different places.  */
 # include <bits/libc-lock.h>
@@ -82,6 +92,15 @@ __libc_lock_define_initialized (static, lock);
 # define internal_function
 #endif
 
+/* Some optimizations for glibc.  */
+#ifdef _LIBC
+# define FEOF(fp)              feof_unlocked (fp)
+# define FGETS(buf, n, fp)     fgets_unlocked (buf, n, fp)
+#else
+# define FEOF(fp)              feof (fp)
+# define FGETS(buf, n, fp)     fgets (buf, n, fp)
+#endif
+
 /* For those losing systems which don't have `alloca' we have to add
    some additional code emulating it.  */
 #ifdef HAVE_ALLOCA
@@ -91,11 +110,11 @@ __libc_lock_define_initialized (static, lock);
 # define freea(p) free (p)
 #endif
 
-#if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
+#if defined _LIBC_REENTRANT || HAVE_DECL_FGETS_UNLOCKED
 # undef fgets
 # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
 #endif
-#if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
+#if defined _LIBC_REENTRANT || HAVE_DECL_FEOF_UNLOCKED
 # undef feof
 # define feof(s) feof_unlocked (s)
 #endif
@@ -108,27 +127,30 @@ struct alias_map
 };
 
 
-static char *string_space;
+#ifndef _LIBC
+# define libc_freeres_ptr(decl) decl
+#endif
+
+libc_freeres_ptr (static char *string_space);
 static size_t string_space_act;
 static size_t string_space_max;
-static struct alias_map *map;
+libc_freeres_ptr (static struct alias_map *map);
 static size_t nmap;
 static size_t maxmap;
 
 
 /* Prototypes for local functions.  */
-static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
+static size_t read_alias_file (const char *fname, int fname_len)
      internal_function;
-static int extend_alias_table PARAMS ((void));
-static int alias_compare PARAMS ((const struct alias_map *map1,
-                                 const struct alias_map *map2));
+static int extend_alias_table (void);
+static int alias_compare (const struct alias_map *map1,
+                         const struct alias_map *map2);
 
 
 const char *
-_nl_expand_alias (name)
-    const char *name;
+_nl_expand_alias (const char *name)
 {
-  static const char *locale_alias_path = LOCALE_ALIAS_PATH;
+  static const char *locale_alias_path;
   struct alias_map *retval;
   const char *result = NULL;
   size_t added;
@@ -137,6 +159,9 @@ _nl_expand_alias (name)
   __libc_lock_lock (lock);
 #endif
 
+  if (locale_alias_path == NULL)
+    locale_alias_path = LOCALE_ALIAS_PATH;
+
   do
     {
       struct alias_map item;
@@ -146,8 +171,8 @@ _nl_expand_alias (name)
       if (nmap > 0)
        retval = (struct alias_map *) bsearch (&item, map, nmap,
                                               sizeof (struct alias_map),
-                                              (int (*) PARAMS ((const void *,
-                                                                const void *))
+                                              (int (*) (const void *,
+                                                        const void *)
                                                ) alias_compare);
       else
        retval = NULL;
@@ -189,9 +214,7 @@ _nl_expand_alias (name)
 
 static size_t
 internal_function
-read_alias_file (fname, fname_len)
-     const char *fname;
-     int fname_len;
+read_alias_file (const char *fname, int fname_len)
 {
   FILE *fp;
   char *full_fname;
@@ -207,41 +230,35 @@ read_alias_file (fname, fname_len)
   memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
 #endif
 
-  fp = fopen (full_fname, "r");
+  fp = fopen (relocate (full_fname), "r");
   freea (full_fname);
   if (fp == NULL)
     return 0;
 
+#ifdef HAVE___FSETLOCKING
+  /* No threads present.  */
+  __fsetlocking (fp, FSETLOCKING_BYCALLER);
+#endif
+
   added = 0;
-  while (!feof (fp))
+  while (!FEOF (fp))
     {
       /* It is a reasonable approach to use a fix buffer here because
         a) we are only interested in the first two fields
         b) these fields must be usable as file names and so must not
            be that long
-       */
-      char buf[BUFSIZ];
+        We avoid a multi-kilobyte buffer here since this would use up
+        stack space which we might not have if the program ran out of
+        memory.  */
+      char buf[400];
       char *alias;
       char *value;
       char *cp;
 
-      if (fgets (buf, sizeof buf, fp) == NULL)
+      if (FGETS (buf, sizeof buf, fp) == NULL)
        /* EOF reached.  */
        break;
 
-      /* Possibly not the whole line fits into the buffer.  Ignore
-        the rest of the line.  */
-      if (strchr (buf, '\n') == NULL)
-       {
-         char altbuf[BUFSIZ];
-         do
-           if (fgets (altbuf, sizeof altbuf, fp) == NULL)
-             /* Make sure the inner loop will be left.  The outer loop
-                will exit at the `feof' test.  */
-             break;
-         while (strchr (altbuf, '\n') == NULL);
-       }
-
       cp = buf;
       /* Ignore leading white space.  */
       while (isspace ((unsigned char) cp[0]))
@@ -325,6 +342,14 @@ read_alias_file (fname, fname_len)
              ++added;
            }
        }
+
+      /* Possibly not the whole line fits into the buffer.  Ignore
+        the rest of the line.  */
+      while (strchr (buf, '\n') == NULL)
+       if (FGETS (buf, sizeof buf, fp) == NULL)
+         /* Make sure the inner loop will be left.  The outer loop
+            will exit at the `feof' test.  */
+         break;
     }
 
   /* Should we test for ferror()?  I think we have to silently ignore
@@ -333,7 +358,7 @@ read_alias_file (fname, fname_len)
 
   if (added > 0)
     qsort (map, nmap, sizeof (struct alias_map),
-          (int (*) PARAMS ((const void *, const void *))) alias_compare);
+          (int (*) (const void *, const void *)) alias_compare);
 
   return added;
 }
@@ -358,23 +383,8 @@ extend_alias_table ()
 }
 
 
-#ifdef _LIBC
-static void __attribute__ ((unused))
-free_mem (void)
-{
-  if (string_space != NULL)
-    free (string_space);
-  if (map != NULL)
-    free (map);
-}
-text_set_element (__libc_subfreeres, free_mem);
-#endif
-
-
 static int
-alias_compare (map1, map2)
-     const struct alias_map *map1;
-     const struct alias_map *map2;
+alias_compare (const struct alias_map *map1, const struct alias_map *map2)
 {
 #if defined _LIBC || defined HAVE_STRCASECMP
   return strcasecmp (map1->alias, map2->alias);