Update.
[glibc.git] / catgets / catgets.c
blob5014f86b012ab3e806d100ad800ee01d25a95b36
1 /* Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper, <drepper@gnu.org>.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <alloca.h>
21 #include <errno.h>
22 #include <locale.h>
23 #include <nl_types.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
29 #include "catgetsinfo.h"
32 /* Open the catalog and return a descriptor for the catalog. */
33 nl_catd
34 catopen (const char *cat_name, int flag)
36 __nl_catd result;
37 const char *env_var = NULL;
38 const char *nlspath = NULL;
39 size_t cat_name_len = strlen (cat_name) + 1;
40 size_t env_var_len = 0;
41 size_t nlspath_len = 0;
42 char *endp;
44 if (strchr (cat_name, '/') == NULL)
46 if (flag == NL_CAT_LOCALE)
47 /* Use the current locale setting for LC_MESSAGES. */
48 env_var = setlocale (LC_MESSAGES, NULL);
49 else
50 /* Use the LANG environment variable. */
51 env_var = getenv ("LANG");
53 if (env_var == NULL || *env_var == '\0'
54 || (__libc_enable_secure && strchr (env_var, '/') != NULL))
55 env_var = "C";
57 env_var_len = strlen (env_var) + 1;
59 nlspath = getenv ("NLSPATH");
60 if (nlspath != NULL && *nlspath != '\0')
62 /* Append the system dependent directory. */
63 size_t len = strlen (nlspath) + 1 + sizeof NLSPATH;
64 char *tmp = alloca (len);
66 __stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH);
67 nlspath = tmp;
69 nlspath_len = len;
71 else
73 nlspath = NLSPATH;
75 nlspath_len = sizeof NLSPATH;
79 result = (__nl_catd) malloc (sizeof (*result) + cat_name_len
80 + env_var_len + nlspath_len);
81 if (result == NULL)
82 /* We cannot get enough memory. */
83 return (nl_catd) -1;
85 result->status = closed;
86 result->cat_name = endp = (char *) (result + 1);
87 endp = __mempcpy (endp, cat_name, cat_name_len);
89 result->env_var = cat_name_len != 0 ? endp : NULL;
90 endp = __mempcpy (endp, env_var, env_var_len);
92 result->nlspath = nlspath_len != 0 ? endp : NULL;
93 memcpy (endp, nlspath, nlspath_len);
95 __libc_lock_init (result->lock);
97 return (nl_catd) result;
101 /* Return message from message catalog. */
102 char *
103 catgets (nl_catd catalog_desc, int set, int message, const char *string)
105 __nl_catd catalog;
106 size_t idx;
107 size_t cnt;
109 /* Be generous if catalog which failed to be open is used. */
110 if (catalog_desc == (nl_catd) -1 || ++set <= 0 || message < 0)
111 return (char *) string;
113 catalog = (__nl_catd) catalog_desc;
115 if (catalog->status == closed)
116 __open_catalog (catalog);
118 if (catalog->status == nonexisting)
120 __set_errno (EBADF);
121 return (char *) string;
124 idx = ((set * message) % catalog->plane_size) * 3;
125 cnt = 0;
128 if (catalog->name_ptr[idx + 0] == (u_int32_t) set
129 && catalog->name_ptr[idx + 1] == (u_int32_t) message)
130 return (char *) &catalog->strings[catalog->name_ptr[idx + 2]];
132 idx += catalog->plane_size * 3;
134 while (++cnt < catalog->plane_depth);
136 __set_errno (ENOMSG);
137 return (char *) string;
141 /* Return resources used for loaded message catalog. */
143 catclose (nl_catd catalog_desc)
145 __nl_catd catalog;
147 catalog = (__nl_catd) catalog_desc;
149 #ifdef _POSIX_MAPPED_FILES
150 if (catalog->status == mmapped)
151 __munmap ((void *) catalog->file_ptr, catalog->file_size);
152 else
153 #endif /* _POSIX_MAPPED_FILES */
154 if (catalog->status == malloced)
155 free ((void *) catalog->file_ptr);
156 else if (catalog->status != closed && catalog->status != nonexisting)
158 __set_errno (EBADF);
159 return -1;
162 free ((void *) catalog);
164 return 0;