Back to index

glibc  2.9
catgets.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996-2000, 2001, 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper, <drepper@gnu.org>.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <alloca.h>
00021 #include <errno.h>
00022 #include <locale.h>
00023 #include <nl_types.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <unistd.h>
00027 #include <sys/mman.h>
00028 
00029 #include "catgetsinfo.h"
00030 
00031 
00032 /* Open the catalog and return a descriptor for the catalog.  */
00033 nl_catd
00034 catopen (const char *cat_name, int flag)
00035 {
00036   __nl_catd result;
00037   const char *env_var = NULL;
00038   const char *nlspath = NULL;
00039 
00040   if (strchr (cat_name, '/') == NULL)
00041     {
00042       if (flag == NL_CAT_LOCALE)
00043        /* Use the current locale setting for LC_MESSAGES.  */
00044        env_var = setlocale (LC_MESSAGES, NULL);
00045       else
00046        /* Use the LANG environment variable.  */
00047        env_var = getenv ("LANG");
00048 
00049       if (env_var == NULL || *env_var == '\0'
00050          || (__libc_enable_secure && strchr (env_var, '/') != NULL))
00051        env_var = "C";
00052 
00053       nlspath = getenv ("NLSPATH");
00054       if (nlspath != NULL && *nlspath != '\0')
00055        {
00056          /* Append the system dependent directory.  */
00057          size_t len = strlen (nlspath) + 1 + sizeof NLSPATH;
00058          char *tmp = alloca (len);
00059 
00060          __stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH);
00061          nlspath = tmp;
00062        }
00063       else
00064        nlspath = NLSPATH;
00065     }
00066 
00067   result = (__nl_catd) malloc (sizeof (*result));
00068   if (result == NULL)
00069     /* We cannot get enough memory.  */
00070     return (nl_catd) -1;
00071 
00072   if (__open_catalog (cat_name, nlspath, env_var, result) != 0)
00073     {
00074       /* Couldn't open the file.  */
00075       free ((void *) result);
00076       return (nl_catd) -1;
00077     }
00078 
00079   return (nl_catd) result;
00080 }
00081 
00082 
00083 /* Return message from message catalog.  */
00084 char *
00085 catgets (nl_catd catalog_desc, int set, int message, const char *string)
00086 {
00087   __nl_catd catalog;
00088   size_t idx;
00089   size_t cnt;
00090 
00091   /* Be generous if catalog which failed to be open is used.  */
00092   if (catalog_desc == (nl_catd) -1 || ++set <= 0 || message < 0)
00093     return (char *) string;
00094 
00095   catalog = (__nl_catd) catalog_desc;
00096 
00097   idx = ((set * message) % catalog->plane_size) * 3;
00098   cnt = 0;
00099   do
00100     {
00101       if (catalog->name_ptr[idx + 0] == (u_int32_t) set
00102          && catalog->name_ptr[idx + 1] == (u_int32_t) message)
00103        return (char *) &catalog->strings[catalog->name_ptr[idx + 2]];
00104 
00105       idx += catalog->plane_size * 3;
00106     }
00107   while (++cnt < catalog->plane_depth);
00108 
00109   __set_errno (ENOMSG);
00110   return (char *) string;
00111 }
00112 
00113 
00114 /* Return resources used for loaded message catalog.  */
00115 int
00116 catclose (nl_catd catalog_desc)
00117 {
00118   __nl_catd catalog;
00119 
00120   /* Be generous if catalog which failed to be open is used.  */
00121   if (catalog_desc == (nl_catd) -1)
00122     {
00123       __set_errno (EBADF);
00124       return -1;
00125     }
00126 
00127   catalog = (__nl_catd) catalog_desc;
00128 
00129 #ifdef _POSIX_MAPPED_FILES
00130   if (catalog->status == mmapped)
00131     __munmap ((void *) catalog->file_ptr, catalog->file_size);
00132   else
00133 #endif /* _POSIX_MAPPED_FILES */
00134     if (catalog->status == malloced)
00135       free ((void *) catalog->file_ptr);
00136     else
00137       {
00138        __set_errno (EBADF);
00139        return -1;
00140       }
00141 
00142   free ((void *) catalog);
00143 
00144   return 0;
00145 }