update from main archive 960927
[platform/upstream/glibc.git] / catgets / catgets.c
1 /* Copyright (C) 1996 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
17 not, 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 <nl_types.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26
27 #include "catgetsinfo.h"
28
29
30 /* Open the catalog and return a descriptor for the catalog.  */
31 nl_catd
32 catopen (const char *cat_name, int flag)
33 {
34   __nl_catd result;
35   const char *env_var;
36
37   result = (__nl_catd) malloc (sizeof (*result));
38   if (result == NULL)
39     /* We cannot get enough memory.  */
40     return (nl_catd) -1;
41
42   result->status = closed;
43
44   result->cat_name = __strdup (cat_name);
45   if (result->cat_name == NULL)
46     {
47       free (result);
48       return (nl_catd) -1;
49     }
50
51   if (strchr (cat_name, '/') == NULL)
52     {
53       if (flag == NL_CAT_LOCALE)
54         {
55           env_var = getenv ("LC_ALL");
56           if (env_var == NULL)
57             {
58               env_var = getenv ("LC_MESSAGES");
59               if (env_var == NULL)
60                 {
61                   env_var = getenv ("LANG");
62                   if (env_var == NULL)
63                     env_var = "C";
64                 }
65             }
66         }
67       else
68         {
69           env_var = getenv ("LANG");
70           if (env_var == NULL)
71             env_var = "C";
72         }
73
74       result->env_var = __strdup (env_var);
75       if (result->env_var == NULL)
76         {
77           free ((void *) result->cat_name);
78           free ((void *) result);
79           return (nl_catd) -1;
80         }
81
82       if (__secure_getenv ("NLSPATH") != NULL)
83         result->nlspath = __strdup (getenv ("NLSPATH"));
84       else
85         result->nlspath = __strdup (NLSPATH);
86
87       if (result->nlspath == NULL)
88         {
89           free ((void *) result->cat_name);
90           free ((void *) result->env_var);
91           free ((void *) result);
92           return (nl_catd) -1;
93         }
94     }
95   else
96     {
97       result->env_var = NULL;
98       result->nlspath = NULL;
99     }
100
101   return (nl_catd) result;
102 }
103
104
105 /* Return message from message catalog.  */
106 char *
107 catgets (nl_catd catalog_desc, int set, int message, const char *string)
108 {
109   __nl_catd catalog;
110   size_t idx;
111   size_t cnt;
112
113   /* Be generous if catalog which failed to be open is used.  */
114   if (catalog_desc == (nl_catd) -1 || ++set <= 0 || message < 0)
115     return (char *) string;
116
117   catalog = (__nl_catd) catalog_desc;
118
119   if (catalog->status == closed)
120     __open_catalog (catalog, 1);
121
122   if (catalog->status == nonexisting)
123     return (char *) string;
124
125   idx = ((set * message) % catalog->plane_size) * 3;
126   cnt = 0;
127   do
128     {
129       if (catalog->name_ptr[idx + 0] == (u_int32_t) set
130           && catalog->name_ptr[idx + 1] == (u_int32_t) message)
131         return (char *) &catalog->strings[catalog->name_ptr[idx + 2]];
132
133       idx += catalog->plane_size * 3;
134     }
135   while (++cnt < catalog->plane_depth);
136
137   return (char *) string;
138 }
139
140
141 /* Return resources used for loaded message catalog.  */
142 int
143 catclose (nl_catd catalog_desc)
144 {
145   __nl_catd catalog;
146
147   catalog = (__nl_catd) catalog_desc;
148
149   if (catalog->status == mmaped)
150     munmap ((void *) catalog->file_ptr, catalog->file_size);
151   else if (catalog->status == malloced)
152     free ((void *) catalog->file_ptr);
153   else if (catalog->status != closed && catalog->status != nonexisting)
154     return -1;
155
156   if (catalog->nlspath)
157     free ((void *) catalog->nlspath);
158   if (catalog->env_var)
159     free ((void *) catalog->env_var);
160   free ((void *) catalog);
161
162   return 0;
163 }