]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - catgets/open_catalog.c
Use glibc_likely instead __builtin_expect.
[thirdparty/glibc.git] / catgets / open_catalog.c
index aefa34f3fa4fb90719cf550c16e0e06d0a2deca9..d582270601174de7c7f55a327634dd3782b27842 100644 (file)
@@ -1,21 +1,20 @@
-/* Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
+/* Copyright (C) 1996-2014 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper, <drepper@gnu.org>.
 
    The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
    The GNU C Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Library General Public License for more details.
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with the GNU C Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
 #include <byteswap.h>
 #include <endian.h>
 #include <sys/stat.h>
 
 #include "catgetsinfo.h"
+#include <not-cancel.h>
 
 
 #define SWAPU32(w) bswap_32 (w)
 
 
-void
-__open_catalog (__nl_catd catalog)
+int
+__open_catalog (const char *cat_name, const char *nlspath, const char *env_var,
+               __nl_catd catalog)
 {
   int fd = -1;
-  struct stat st;
+  struct stat64 st;
   int swapping;
   size_t cnt;
   size_t max_offset;
   size_t tab_size;
   const char *lastp;
+  int result = -1;
 
-  /* Make sure we are alone.  */
-  __libc_lock_lock (catalog->lock);
-
-  /* Check whether there was no other thread faster.  */
-  if (catalog->status != closed)
-    /* While we waited some other thread tried to open the catalog.  */
-    goto unlock_return;
-
-  if (strchr (catalog->cat_name, '/') != NULL || catalog->nlspath == NULL)
-    fd = __open (catalog->cat_name, O_RDONLY);
+  if (strchr (cat_name, '/') != NULL || nlspath == NULL)
+    fd = open_not_cancel_2 (cat_name, O_RDONLY);
   else
     {
-      const char *run_nlspath = catalog->nlspath;
+      const char *run_nlspath = nlspath;
 #define ENOUGH(n)                                                            \
-  if (bufact + (n) >=bufmax)                                                 \
+  if (__glibc_unlikely (bufact + (n) >= bufmax))                             \
     {                                                                        \
       char *old_buf = buf;                                                   \
       bufmax += 256 + (n);                                                   \
@@ -84,95 +78,106 @@ __open_catalog (__nl_catd catalog)
       while (*run_nlspath != '\0')
        {
          bufact = 0;
-         while (*run_nlspath != ':' && *run_nlspath != '\0')
-           if (*run_nlspath == '%')
-             {
-               const char *tmp;
-
-               ++run_nlspath;  /* We have seen the `%'.  */
-               switch (*run_nlspath++)
-                 {
-                 case 'N':
-                   /* Use the catalog name.  */
-                   len = strlen (catalog->cat_name);
-                   ENOUGH (len);
-                   memcpy (&buf[bufact], catalog->cat_name, len);
-                   bufact += len;
-                   break;
-                 case 'L':
-                   /* Use the current locale category value.  */
-                   len = strlen (catalog->env_var);
-                   ENOUGH (len);
-                   memcpy (&buf[bufact], catalog->env_var, len);
-                   bufact += len;
-                   break;
-                 case 'l':
-                   /* Use language element of locale category value.  */
-                   tmp = catalog->env_var;
-                   do
-                     {
-                       ENOUGH (1);
-                       buf[bufact++] = *tmp++;
-                     }
-                   while (*tmp != '\0' && *tmp != '_' && *tmp != '.');
-                   break;
-                 case 't':
-                   /* Use territory element of locale category value.  */
-                   tmp = catalog->env_var;
-                   do
-                     ++tmp;
-                   while (*tmp != '\0' && *tmp != '_' && *tmp != '.');
-                   if (*tmp == '_')
-                     {
+
+         if (*run_nlspath == ':')
+           {
+             /* Leading colon or adjacent colons - treat same as %N.  */
+             len = strlen (cat_name);
+             ENOUGH (len);
+             memcpy (&buf[bufact], cat_name, len);
+             bufact += len;
+           }
+         else
+           while (*run_nlspath != ':' && *run_nlspath != '\0')
+             if (*run_nlspath == '%')
+               {
+                 const char *tmp;
+
+                 ++run_nlspath;        /* We have seen the `%'.  */
+                 switch (*run_nlspath++)
+                   {
+                   case 'N':
+                     /* Use the catalog name.  */
+                     len = strlen (cat_name);
+                     ENOUGH (len);
+                     memcpy (&buf[bufact], cat_name, len);
+                     bufact += len;
+                     break;
+                   case 'L':
+                     /* Use the current locale category value.  */
+                     len = strlen (env_var);
+                     ENOUGH (len);
+                     memcpy (&buf[bufact], env_var, len);
+                     bufact += len;
+                     break;
+                   case 'l':
+                     /* Use language element of locale category value.  */
+                     tmp = env_var;
+                     do
+                       {
+                         ENOUGH (1);
+                         buf[bufact++] = *tmp++;
+                       }
+                     while (*tmp != '\0' && *tmp != '_' && *tmp != '.');
+                     break;
+                   case 't':
+                     /* Use territory element of locale category value.  */
+                     tmp = env_var;
+                     do
                        ++tmp;
-                       do
-                         {
-                           ENOUGH (1);
-                           buf[bufact++] = *tmp;
-                         }
-                       while (*tmp != '\0' && *tmp != '.');
-                     }
-                   break;
-                 case 'c':
-                   /* Use code set element of locale category value.  */
-                   tmp = catalog->env_var;
-                   do
-                     ++tmp;
-                   while (*tmp != '\0' && *tmp != '.');
-                   if (*tmp == '.')
-                     {
+                     while (*tmp != '\0' && *tmp != '_' && *tmp != '.');
+                     if (*tmp == '_')
+                       {
+                         ++tmp;
+                         do
+                           {
+                             ENOUGH (1);
+                             buf[bufact++] = *tmp++;
+                           }
+                         while (*tmp != '\0' && *tmp != '.');
+                       }
+                     break;
+                   case 'c':
+                     /* Use code set element of locale category value.  */
+                     tmp = env_var;
+                     do
                        ++tmp;
-                       do
-                         {
-                           ENOUGH (1);
-                           buf[bufact++] = *tmp;
-                         }
-                       while (*tmp != '\0');
-                     }
-                   break;
-                 case '%':
-                   ENOUGH (1);
-                   buf[bufact++] = '%';
-                   break;
-                 default:
-                   /* Unknown variable: ignore this path element.  */
-                   bufact = 0;
-                   while (*run_nlspath != '\0' && *run_nlspath != ':')
-                     ++run_nlspath;
-                   break;
-                 }
-             }
-           else
-             {
-               ENOUGH (1);
-               buf[bufact++] = *run_nlspath++;
-             }
+                     while (*tmp != '\0' && *tmp != '.');
+                     if (*tmp == '.')
+                       {
+                         ++tmp;
+                         do
+                           {
+                             ENOUGH (1);
+                             buf[bufact++] = *tmp++;
+                           }
+                         while (*tmp != '\0');
+                       }
+                     break;
+                   case '%':
+                     ENOUGH (1);
+                     buf[bufact++] = '%';
+                     break;
+                   default:
+                     /* Unknown variable: ignore this path element.  */
+                     bufact = 0;
+                     while (*run_nlspath != '\0' && *run_nlspath != ':')
+                       ++run_nlspath;
+                     break;
+                   }
+               }
+             else
+               {
+                 ENOUGH (1);
+                 buf[bufact++] = *run_nlspath++;
+               }
+
          ENOUGH (1);
          buf[bufact] = '\0';
 
          if (bufact != 0)
            {
-             fd = __open (buf, O_RDONLY);
+             fd = open_not_cancel_2 (buf, O_RDONLY);
              if (fd >= 0)
                break;
            }
@@ -182,23 +187,18 @@ __open_catalog (__nl_catd catalog)
     }
 
   /* Avoid dealing with directories and block devices */
-  if (fd < 0)
-    {
-      catalog->status = nonexisting;
-      goto unlock_return;
-    }
+  if (__builtin_expect (fd, 0) < 0)
+    return -1;
 
-  if (__fxstat (_STAT_VER, fd, &st) < 0)
-    {
-      catalog->status = nonexisting;
-      goto close_unlock_return;
-    }
-  if (!S_ISREG (st.st_mode) || st.st_size < sizeof (struct catalog_obj))
+  if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
+    goto close_unlock_return;
+
+  if (__builtin_expect (!S_ISREG (st.st_mode), 0)
+      || (size_t) st.st_size < sizeof (struct catalog_obj))
     {
       /* `errno' is not set correctly but the file is not usable.
         Use an reasonable error value.  */
       __set_errno (EINVAL);
-      catalog->status = nonexisting;
       goto close_unlock_return;
     }
 
@@ -211,15 +211,12 @@ __open_catalog (__nl_catd catalog)
 # ifndef MAP_FILE
     /* Some systems do not have this flag; it is superfluous.  */
 #  define MAP_FILE 0
-# endif
-# ifndef MAP_INHERIT
-    /* Some systems might lack this; they lose.  */
-#  define MAP_INHERIT 0
 # endif
   catalog->file_ptr =
     (struct catalog_obj *) __mmap (NULL, st.st_size, PROT_READ,
-                                  MAP_FILE|MAP_COPY|MAP_INHERIT, fd, 0);
-  if (catalog->file_ptr != (struct catalog_obj *) MAP_FAILED)
+                                  MAP_FILE|MAP_COPY, fd, 0);
+  if (__builtin_expect (catalog->file_ptr != (struct catalog_obj *) MAP_FAILED,
+                       1))
     /* Tell the world we managed to mmap the file.  */
     catalog->status = mmapped;
   else
@@ -230,20 +227,21 @@ __open_catalog (__nl_catd catalog)
       size_t todo;
       catalog->file_ptr = malloc (st.st_size);
       if (catalog->file_ptr == NULL)
-       {
-         catalog->status = nonexisting;
-         goto close_unlock_return;
-       }
+       goto close_unlock_return;
+
       todo = st.st_size;
       /* Save read, handle partial reads.  */
       do
        {
-         size_t now = __read (fd, (((char *) &catalog->file_ptr)
-                                   + (st.st_size - todo)), todo);
-         if (now == 0)
+         size_t now = read_not_cancel (fd, (((char *) catalog->file_ptr)
+                                            + (st.st_size - todo)), todo);
+         if (now == 0 || now == (size_t) -1)
            {
+#ifdef EINTR
+             if (now == (size_t) -1 && errno == EINTR)
+               continue;
+#endif
              free ((void *) catalog->file_ptr);
-             catalog->status = nonexisting;
              goto close_unlock_return;
            }
          todo -= now;
@@ -255,7 +253,7 @@ __open_catalog (__nl_catd catalog)
   /* Determine whether the file is a catalog file and if yes whether
      it is written using the correct byte order.  Else we have to swap
      the values.  */
-  if (catalog->file_ptr->magic == CATGETS_MAGIC)
+  if (__glibc_likely (catalog->file_ptr->magic == CATGETS_MAGIC))
     swapping = 0;
   else if (catalog->file_ptr->magic == SWAPU32 (CATGETS_MAGIC))
     swapping = 1;
@@ -270,7 +268,6 @@ __open_catalog (__nl_catd catalog)
       else
 #endif /* _POSIX_MAPPED_FILES */
        free (catalog->file_ptr);
-      catalog->status = nonexisting;
       goto close_unlock_return;
     }
 
@@ -307,7 +304,8 @@ __open_catalog (__nl_catd catalog)
 
   /* Now we can check whether the file is large enough to contain the
      tables it says it contains.  */
-  if (st.st_size <= (sizeof (struct catalog_obj) + 2 * tab_size + max_offset))
+  if ((size_t) st.st_size
+      <= (sizeof (struct catalog_obj) + 2 * tab_size + max_offset))
     /* The last string is not contained in the file.  */
     goto invalid_file;
 
@@ -321,9 +319,13 @@ __open_catalog (__nl_catd catalog)
       ++lastp;
     }
 
+  /* We succeeded.  */
+  result = 0;
+
   /* Release the lock again.  */
  close_unlock_return:
-  __close (fd);
- unlock_return:
-  __libc_lock_unlock (catalog->lock);
+  close_not_cancel_no_status (fd);
+
+  return result;
 }
+libc_hidden_def (__open_catalog)