Update.
[platform/upstream/glibc.git] / catgets / catgets.c
1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper, <drepper@gnu.ai.mit.edu>.
4
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.
9
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.
14
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.  */
19
20 #include <alloca.h>
21 #include <errno.h>
22 #include <nl_types.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27
28 #include "catgetsinfo.h"
29
30
31 /* Open the catalog and return a descriptor for the catalog.  */
32 nl_catd
33 catopen (const char *cat_name, int flag)
34 {
35   __nl_catd result;
36   const char *env_var;
37   const char *nlspath;
38
39   result = (__nl_catd) malloc (sizeof (*result));
40   if (result == NULL)
41     /* We cannot get enough memory.  */
42     return (nl_catd) -1;
43
44   result->status = closed;
45
46   result->cat_name = __strdup (cat_name);
47   if (result->cat_name == NULL)
48     {
49       free (result);
50       __set_errno (ENOMEM);
51       return (nl_catd) -1;
52     }
53
54   if (strchr (cat_name, '/') == NULL)
55     {
56       if (flag == NL_CAT_LOCALE)
57         {
58           env_var = getenv ("LC_ALL");
59           if (env_var == NULL)
60             {
61               env_var = getenv ("LC_MESSAGES");
62               if (env_var == NULL)
63                 {
64                   env_var = getenv ("LANG");
65                   if (env_var == NULL)
66                     env_var = "C";
67                 }
68             }
69         }
70       else
71         {
72           env_var = getenv ("LANG");
73           if (env_var == NULL)
74             env_var = "C";
75         }
76
77       result->env_var = __strdup (env_var);
78       if (result->env_var == NULL)
79         {
80           free ((void *) result->cat_name);
81           free ((void *) result);
82           __set_errno (ENOMEM);
83           return (nl_catd) -1;
84         }
85
86       nlspath = __secure_getenv ("NLSPATH");
87       if (nlspath != NULL && *nlspath != '\0')
88         {
89           /* Append the system dependent directory.  */
90           size_t len = strlen (nlspath) + 1 + sizeof NLSPATH;
91           char *tmp = alloca (len);
92
93           __stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH);
94           nlspath = tmp;
95         }
96       else
97         nlspath = NLSPATH;
98
99       result->nlspath = __strdup (NLSPATH);
100       if (result->nlspath == NULL)
101         {
102           free ((void *) result->cat_name);
103           free ((void *) result->env_var);
104           free ((void *) result);
105           __set_errno (ENOMEM);
106           return (nl_catd) -1;
107         }
108     }
109   else
110     {
111       result->env_var = NULL;
112       result->nlspath = NULL;
113     }
114
115   __libc_lock_init (result->lock);
116
117   return (nl_catd) result;
118 }
119
120
121 /* Return message from message catalog.  */
122 char *
123 catgets (nl_catd catalog_desc, int set, int message, const char *string)
124 {
125   __nl_catd catalog;
126   size_t idx;
127   size_t cnt;
128
129   /* Be generous if catalog which failed to be open is used.  */
130   if (catalog_desc == (nl_catd) -1 || ++set <= 0 || message < 0)
131     return (char *) string;
132
133   catalog = (__nl_catd) catalog_desc;
134
135   if (catalog->status == closed)
136     __open_catalog (catalog);
137
138   if (catalog->status == nonexisting)
139     {
140       __set_errno (EBADF);
141       return (char *) string;
142     }
143
144   idx = ((set * message) % catalog->plane_size) * 3;
145   cnt = 0;
146   do
147     {
148       if (catalog->name_ptr[idx + 0] == (u_int32_t) set
149           && catalog->name_ptr[idx + 1] == (u_int32_t) message)
150         return (char *) &catalog->strings[catalog->name_ptr[idx + 2]];
151
152       idx += catalog->plane_size * 3;
153     }
154   while (++cnt < catalog->plane_depth);
155
156   __set_errno (ENOMSG);
157   return (char *) string;
158 }
159
160
161 /* Return resources used for loaded message catalog.  */
162 int
163 catclose (nl_catd catalog_desc)
164 {
165   __nl_catd catalog;
166
167   catalog = (__nl_catd) catalog_desc;
168
169   if (catalog->status == mmapped)
170     __munmap ((void *) catalog->file_ptr, catalog->file_size);
171   else if (catalog->status == malloced)
172     free ((void *) catalog->file_ptr);
173   else if (catalog->status != closed && catalog->status != nonexisting)
174     {
175       __set_errno (EBADF);
176       return -1;
177     }
178
179   if (catalog->nlspath)
180     free ((void *) catalog->nlspath);
181   if (catalog->env_var)
182     free ((void *) catalog->env_var);
183   free ((void *) catalog);
184
185   return 0;
186 }