]> git.ipfire.org Git - thirdparty/glibc.git/blame - catgets/catgets.c
Update.
[thirdparty/glibc.git] / catgets / catgets.c
CommitLineData
b6aa34eb 1/* Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
2c6fe0bd 2 This file is part of the GNU C Library.
ed3b44d3 3 Contributed by Ulrich Drepper, <drepper@gnu.org>.
a641835a 4
2c6fe0bd
UD
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.
a641835a 9
2c6fe0bd
UD
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.
a641835a 14
2c6fe0bd
UD
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. */
a641835a
RM
19
20#include <alloca.h>
2c6fe0bd 21#include <errno.h>
b6aa34eb 22#include <locale.h>
a641835a
RM
23#include <nl_types.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <sys/mman.h>
28
29#include "catgetsinfo.h"
30
31
32/* Open the catalog and return a descriptor for the catalog. */
33nl_catd
34catopen (const char *cat_name, int flag)
35{
36 __nl_catd result;
390500b1
UD
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;
a641835a
RM
43
44 if (strchr (cat_name, '/') == NULL)
45 {
46 if (flag == NL_CAT_LOCALE)
b6aa34eb
UD
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");
a641835a 52
63336471
UD
53 if (env_var == NULL || *env_var == '\0'
54 || (__libc_enable_secure && strchr (env_var, '/') != NULL))
390500b1
UD
55 env_var = "C";
56
390500b1
UD
57 env_var_len = strlen (env_var) + 1;
58
74955460 59 nlspath = getenv ("NLSPATH");
0d8733c4
UD
60 if (nlspath != NULL && *nlspath != '\0')
61 {
62 /* Append the system dependent directory. */
39e16978 63 size_t len = strlen (nlspath) + 1 + sizeof NLSPATH;
0d8733c4
UD
64 char *tmp = alloca (len);
65
66 __stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH);
67 nlspath = tmp;
390500b1
UD
68
69 nlspath_len = len;
0d8733c4
UD
70 }
71 else
a641835a 72 {
390500b1
UD
73 nlspath = NLSPATH;
74
75 nlspath_len = sizeof NLSPATH;
a641835a
RM
76 }
77 }
390500b1
UD
78
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;
84
85 result->status = closed;
86 result->cat_name = endp = (char *) (result + 1);
87 endp = __mempcpy (endp, cat_name, cat_name_len);
88
89 result->env_var = cat_name_len != 0 ? endp : NULL;
90 endp = __mempcpy (endp, env_var, env_var_len);
91
92 result->nlspath = nlspath_len != 0 ? endp : NULL;
93 memcpy (endp, nlspath, nlspath_len);
a641835a 94
39e16978
UD
95 __libc_lock_init (result->lock);
96
a641835a
RM
97 return (nl_catd) result;
98}
99
100
101/* Return message from message catalog. */
102char *
103catgets (nl_catd catalog_desc, int set, int message, const char *string)
104{
105 __nl_catd catalog;
106 size_t idx;
107 size_t cnt;
108
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;
112
113 catalog = (__nl_catd) catalog_desc;
114
115 if (catalog->status == closed)
39e16978 116 __open_catalog (catalog);
a641835a
RM
117
118 if (catalog->status == nonexisting)
2c6fe0bd
UD
119 {
120 __set_errno (EBADF);
121 return (char *) string;
122 }
a641835a
RM
123
124 idx = ((set * message) % catalog->plane_size) * 3;
125 cnt = 0;
126 do
127 {
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]];
131
132 idx += catalog->plane_size * 3;
133 }
134 while (++cnt < catalog->plane_depth);
135
2c6fe0bd 136 __set_errno (ENOMSG);
a641835a
RM
137 return (char *) string;
138}
139
140
141/* Return resources used for loaded message catalog. */
142int
143catclose (nl_catd catalog_desc)
144{
145 __nl_catd catalog;
146
147 catalog = (__nl_catd) catalog_desc;
148
7cabd57c 149#ifdef _POSIX_MAPPED_FILES
6d52618b 150 if (catalog->status == mmapped)
40a55d20 151 __munmap ((void *) catalog->file_ptr, catalog->file_size);
7cabd57c
UD
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)
157 {
158 __set_errno (EBADF);
159 return -1;
160 }
a641835a 161
a641835a
RM
162 free ((void *) catalog);
163
164 return 0;
165}