Update.
[glibc.git] / catgets / catgets.c
blob86ff0ff831d39efaf68da54c10539614db30c63c
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)
54 env_var = "C";
56 env_var_len = strlen (env_var) + 1;
58 nlspath = __secure_getenv ("NLSPATH");
59 if (nlspath != NULL && *nlspath != '\0')
61 /* Append the system dependent directory. */
62 size_t len = strlen (nlspath) + 1 + sizeof NLSPATH;
63 char *tmp = alloca (len);
65 __stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH);
66 nlspath = tmp;
68 nlspath_len = len;
70 else
72 nlspath = NLSPATH;
74 nlspath_len = sizeof NLSPATH;
78 result = (__nl_catd) malloc (sizeof (*result) + cat_name_len
79 + env_var_len + nlspath_len);
80 if (result == NULL)
81 /* We cannot get enough memory. */
82 return (nl_catd) -1;
84 result->status = closed;
85 result->cat_name = endp = (char *) (result + 1);
86 endp = __mempcpy (endp, cat_name, cat_name_len);
88 result->env_var = cat_name_len != 0 ? endp : NULL;
89 endp = __mempcpy (endp, env_var, env_var_len);
91 result->nlspath = nlspath_len != 0 ? endp : NULL;
92 memcpy (endp, nlspath, nlspath_len);
94 __libc_lock_init (result->lock);
96 return (nl_catd) result;
100 /* Return message from message catalog. */
101 char *
102 catgets (nl_catd catalog_desc, int set, int message, const char *string)
104 __nl_catd catalog;
105 size_t idx;
106 size_t cnt;
108 /* Be generous if catalog which failed to be open is used. */
109 if (catalog_desc == (nl_catd) -1 || ++set <= 0 || message < 0)
110 return (char *) string;
112 catalog = (__nl_catd) catalog_desc;
114 if (catalog->status == closed)
115 __open_catalog (catalog);
117 if (catalog->status == nonexisting)
119 __set_errno (EBADF);
120 return (char *) string;
123 idx = ((set * message) % catalog->plane_size) * 3;
124 cnt = 0;
127 if (catalog->name_ptr[idx + 0] == (u_int32_t) set
128 && catalog->name_ptr[idx + 1] == (u_int32_t) message)
129 return (char *) &catalog->strings[catalog->name_ptr[idx + 2]];
131 idx += catalog->plane_size * 3;
133 while (++cnt < catalog->plane_depth);
135 __set_errno (ENOMSG);
136 return (char *) string;
140 /* Return resources used for loaded message catalog. */
142 catclose (nl_catd catalog_desc)
144 __nl_catd catalog;
146 catalog = (__nl_catd) catalog_desc;
148 #ifdef _POSIX_MAPPED_FILES
149 if (catalog->status == mmapped)
150 __munmap ((void *) catalog->file_ptr, catalog->file_size);
151 else
152 #endif /* _POSIX_MAPPED_FILES */
153 if (catalog->status == malloced)
154 free ((void *) catalog->file_ptr);
155 else if (catalog->status != closed && catalog->status != nonexisting)
157 __set_errno (EBADF);
158 return -1;
161 free ((void *) catalog);
163 return 0;