summary refs log tree commit diff
path: root/catgets
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2001-08-08 21:25:01 +0000
committerUlrich Drepper <drepper@redhat.com>2001-08-08 21:25:01 +0000
commitca130fe4651a2ca547b05187bdc2804defc8a66b (patch)
tree9e743e6df3161a03fda43de4ff71f3b52f21c4fa /catgets
parent443470c3360d5d4ffb8f3078f47cda55fd5cffbd (diff)
downloadglibc-ca130fe4651a2ca547b05187bdc2804defc8a66b.tar.gz
glibc-ca130fe4651a2ca547b05187bdc2804defc8a66b.tar.xz
glibc-ca130fe4651a2ca547b05187bdc2804defc8a66b.zip
Update.
	* catgets/open_catalog.c: Rewrite code to assume that the function
	is called at catopen time and not delayed in catgets.
	* catgets/catgets.c (catopen): Call __open_catalog and fail if that
	function failed.
	(catgets): Remove code for delayed opening of catalog.
	* catgets/catgetsinfo.h: Remove now unnecessary information from
	struct catalog_info.  Change __open_catalog prototype.
	* catgets/gencat.c: Adjust __open_catalog call.
	* catgets/test-gencat.c: Stop program if catopen failed.
Diffstat (limited to 'catgets')
-rw-r--r--catgets/catgets.c44
-rw-r--r--catgets/catgetsinfo.h14
-rw-r--r--catgets/gencat.c12
-rw-r--r--catgets/open_catalog.c67
-rw-r--r--catgets/test-gencat.c6
5 files changed, 47 insertions, 96 deletions
diff --git a/catgets/catgets.c b/catgets/catgets.c
index c719a02934..248b552c91 100644
--- a/catgets/catgets.c
+++ b/catgets/catgets.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
+/* Copyright (C) 1996-2000, 2001 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper, <drepper@gnu.org>.
 
@@ -36,10 +36,6 @@ catopen (const char *cat_name, int flag)
   __nl_catd result;
   const char *env_var = NULL;
   const char *nlspath = NULL;
-  size_t cat_name_len = strlen (cat_name) + 1;
-  size_t env_var_len = 0;
-  size_t nlspath_len = 0;
-  char *endp;
 
   if (strchr (cat_name, '/') == NULL)
     {
@@ -54,8 +50,6 @@ catopen (const char *cat_name, int flag)
 	  || (__libc_enable_secure && strchr (env_var, '/') != NULL))
 	env_var = "C";
 
-      env_var_len = strlen (env_var) + 1;
-
       nlspath = getenv ("NLSPATH");
       if (nlspath != NULL && *nlspath != '\0')
 	{
@@ -65,34 +59,19 @@ catopen (const char *cat_name, int flag)
 
 	  __stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH);
 	  nlspath = tmp;
-
-	  nlspath_len = len;
 	}
       else
-	{
-	  nlspath = NLSPATH;
-
-	  nlspath_len = sizeof NLSPATH;
-	}
+	nlspath = NLSPATH;
     }
 
-  result = (__nl_catd) malloc (sizeof (*result) + cat_name_len
-			       + env_var_len + nlspath_len);
+  result = (__nl_catd) malloc (sizeof (*result));
   if (result == NULL)
     /* We cannot get enough memory.  */
     return (nl_catd) -1;
 
-  result->status = closed;
-  result->cat_name = endp = (char *) (result + 1);
-  endp = __mempcpy (endp, cat_name, cat_name_len);
-
-  result->env_var = cat_name_len != 0 ? endp : NULL;
-  endp = __mempcpy (endp, env_var, env_var_len);
-
-  result->nlspath = nlspath_len != 0 ? endp : NULL;
-  memcpy (endp, nlspath, nlspath_len);
-
-  __libc_lock_init (result->lock);
+  if (__open_catalog (cat_name, nlspath, env_var, result) != 0)
+    /* Couldn't open the file.  */
+    return (nl_catd) -1;
 
   return (nl_catd) result;
 }
@@ -112,15 +91,6 @@ catgets (nl_catd catalog_desc, int set, int message, const char *string)
 
   catalog = (__nl_catd) catalog_desc;
 
-  if (catalog->status == closed)
-    __open_catalog (catalog);
-
-  if (catalog->status == nonexisting)
-    {
-      __set_errno (EBADF);
-      return (char *) string;
-    }
-
   idx = ((set * message) % catalog->plane_size) * 3;
   cnt = 0;
   do
@@ -153,7 +123,7 @@ catclose (nl_catd catalog_desc)
 #endif	/* _POSIX_MAPPED_FILES */
     if (catalog->status == malloced)
       free ((void *) catalog->file_ptr);
-    else if (catalog->status != closed && catalog->status != nonexisting)
+    else
       {
 	__set_errno (EBADF);
 	return -1;
diff --git a/catgets/catgetsinfo.h b/catgets/catgetsinfo.h
index d40b4193f3..d4aa003b34 100644
--- a/catgets/catgetsinfo.h
+++ b/catgets/catgetsinfo.h
@@ -1,4 +1,4 @@
-/* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
+/* Copyright (C) 1996, 1997, 2001 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper, <drepper@gnu.ai.mit.edu>.
 
@@ -18,7 +18,6 @@
    02111-1307 USA.  */
 
 #include <sys/types.h>
-#include <bits/libc-lock.h>
 
 
 struct catalog_obj
@@ -35,11 +34,7 @@ struct catalog_obj
 /* This structure will be filled after loading the catalog.  */
 typedef struct catalog_info
 {
-  enum { closed, nonexisting, mmapped, malloced } status;
-
-  const char *cat_name;
-  const char *env_var;
-  const char *nlspath;
+  enum { mmapped, malloced } status;
 
   size_t plane_size;
   size_t plane_depth;
@@ -48,8 +43,6 @@ typedef struct catalog_info
 
   struct catalog_obj *file_ptr;
   size_t file_size;
-
-  __libc_lock_define (,lock);
 } *__nl_catd;
 
 
@@ -59,4 +52,5 @@ typedef struct catalog_info
 
 
 /* Prototypes for helper functions.  */
-void __open_catalog (__nl_catd __catalog);
+extern int __open_catalog (const char *cat_name, const char *nlspath,
+			   const char *env_var, __nl_catd __catalog);
diff --git a/catgets/gencat.c b/catgets/gencat.c
index 103e8324bb..ce5c4dc50e 100644
--- a/catgets/gencat.c
+++ b/catgets/gencat.c
@@ -1187,21 +1187,15 @@ read_old (struct catalog *catalog, const char *file_name)
   int last_set = -1;
   size_t cnt;
 
-  old_cat_obj.status = closed;
-  old_cat_obj.cat_name = file_name;
-  old_cat_obj.nlspath = NULL;
-  __libc_lock_init (old_cat_obj.lock);
-
   /* Try to open catalog, but don't look through the NLSPATH.  */
-  __open_catalog (&old_cat_obj);
-
-  if (old_cat_obj.status != mmapped && old_cat_obj.status != malloced)
+  if (__open_catalog (file_name, NULL, NULL, &old_cat_obj) != 0)
     {
       if (errno == ENOENT)
 	/* No problem, the catalog simply does not exist.  */
 	return;
       else
-	error (EXIT_FAILURE, errno, gettext ("while opening old catalog file"));
+	error (EXIT_FAILURE, errno,
+	       gettext ("while opening old catalog file"));
     }
 
   /* OK, we have the catalog loaded.  Now read all messages and merge
diff --git a/catgets/open_catalog.c b/catgets/open_catalog.c
index 52ff8a9c83..c38719edb9 100644
--- a/catgets/open_catalog.c
+++ b/catgets/open_catalog.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
+/* Copyright (C) 1996-2000, 2001 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper, <drepper@gnu.org>.
 
@@ -35,8 +35,9 @@
 #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 stat64 st;
@@ -45,20 +46,13 @@ __open_catalog (__nl_catd catalog)
   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 (__builtin_expect (catalog->status != closed, 0))
-    /* 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 (cat_name, O_RDONLY);
   else
     {
-      const char *run_nlspath = catalog->nlspath;
+      const char *run_nlspath = nlspath;
 #define ENOUGH(n)							      \
   if (__builtin_expect (bufact + (n) >= bufmax, 0))			      \
     {									      \
@@ -88,9 +82,9 @@ __open_catalog (__nl_catd catalog)
 	  if (*run_nlspath == ':')
 	    {
 	      /* Leading colon or adjacent colons - treat same as %N.  */
-	      len = strlen (catalog->cat_name);
+	      len = strlen (cat_name);
 	      ENOUGH (len);
-	      memcpy (&buf[bufact], catalog->cat_name, len);
+	      memcpy (&buf[bufact], cat_name, len);
 	      bufact += len;
 	    }
 	  else
@@ -104,21 +98,21 @@ __open_catalog (__nl_catd catalog)
 		    {
 		    case 'N':
 		      /* Use the catalog name.  */
-		      len = strlen (catalog->cat_name);
+		      len = strlen (cat_name);
 		      ENOUGH (len);
-		      memcpy (&buf[bufact], catalog->cat_name, len);
+		      memcpy (&buf[bufact], cat_name, len);
 		      bufact += len;
 		      break;
 		    case 'L':
 		      /* Use the current locale category value.  */
-		      len = strlen (catalog->env_var);
+		      len = strlen (env_var);
 		      ENOUGH (len);
-		      memcpy (&buf[bufact], catalog->env_var, len);
+		      memcpy (&buf[bufact], env_var, len);
 		      bufact += len;
 		      break;
 		    case 'l':
 		      /* Use language element of locale category value.  */
-		      tmp = catalog->env_var;
+		      tmp = env_var;
 		      do
 			{
 			  ENOUGH (1);
@@ -128,7 +122,7 @@ __open_catalog (__nl_catd catalog)
 		      break;
 		    case 't':
 		      /* Use territory element of locale category value.  */
-		      tmp = catalog->env_var;
+		      tmp = env_var;
 		      do
 			++tmp;
 		      while (*tmp != '\0' && *tmp != '_' && *tmp != '.');
@@ -145,7 +139,7 @@ __open_catalog (__nl_catd catalog)
 		      break;
 		    case 'c':
 		      /* Use code set element of locale category value.  */
-		      tmp = catalog->env_var;
+		      tmp = env_var;
 		      do
 			++tmp;
 		      while (*tmp != '\0' && *tmp != '.');
@@ -194,23 +188,17 @@ __open_catalog (__nl_catd catalog)
 
   /* Avoid dealing with directories and block devices */
   if (__builtin_expect (fd, 0) < 0)
-    {
-      catalog->status = nonexisting;
-      goto unlock_return;
-    }
+    return -1;
 
   if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
-    {
-      catalog->status = nonexisting;
-      goto close_unlock_return;
-    }
+    goto close_unlock_return;
+
   if (__builtin_expect (!S_ISREG (st.st_mode), 0)
       || 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;
     }
 
@@ -243,10 +231,8 @@ __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
@@ -260,7 +246,6 @@ __open_catalog (__nl_catd catalog)
 		continue;
 #endif
 	      free ((void *) catalog->file_ptr);
-	      catalog->status = nonexisting;
 	      goto close_unlock_return;
 	    }
 	  todo -= now;
@@ -288,7 +273,6 @@ __open_catalog (__nl_catd catalog)
       else
 #endif	/* _POSIX_MAPPED_FILES */
 	free (catalog->file_ptr);
-      catalog->status = nonexisting;
       goto close_unlock_return;
     }
 
@@ -339,9 +323,12 @@ __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);
+
+  return result;
 }
diff --git a/catgets/test-gencat.c b/catgets/test-gencat.c
index fe5d0916b7..cb026fdc60 100644
--- a/catgets/test-gencat.c
+++ b/catgets/test-gencat.c
@@ -12,6 +12,12 @@ main (void)
   printf ("LC_MESSAGES = %s\n", setlocale (LC_MESSAGES, NULL));
 
   catalog = catopen ("sample", NL_CAT_LOCALE);
+  if (catalog == (nl_catd) -1)
+    {
+      printf ("no catalog: %m\n");
+      exit (1);
+    }
+
   printf ("%s\n", catgets(catalog, 1, 1, "sample 1"));
   printf ("%s\n", catgets(catalog, 1, 2, "sample 2"));
   printf ("%s\n", catgets(catalog, 1, 3, "sample 3"));