update from main archive 961105
[glibc.git] / catgets / catgets.c
blobab7d28a68212d23f8d646bac52cf6e36db2b839b
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>.
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 <nl_types.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
28 #include "catgetsinfo.h"
31 /* Open the catalog and return a descriptor for the catalog. */
32 nl_catd
33 catopen (const char *cat_name, int flag)
35 __nl_catd result;
36 const char *env_var;
38 result = (__nl_catd) malloc (sizeof (*result));
39 if (result == NULL)
40 /* We cannot get enough memory. */
41 return (nl_catd) -1;
43 result->status = closed;
45 result->cat_name = __strdup (cat_name);
46 if (result->cat_name == NULL)
48 free (result);
49 __set_errno (ENOMEM);
50 return (nl_catd) -1;
53 if (strchr (cat_name, '/') == NULL)
55 if (flag == NL_CAT_LOCALE)
57 env_var = getenv ("LC_ALL");
58 if (env_var == NULL)
60 env_var = getenv ("LC_MESSAGES");
61 if (env_var == NULL)
63 env_var = getenv ("LANG");
64 if (env_var == NULL)
65 env_var = "C";
69 else
71 env_var = getenv ("LANG");
72 if (env_var == NULL)
73 env_var = "C";
76 result->env_var = __strdup (env_var);
77 if (result->env_var == NULL)
79 free ((void *) result->cat_name);
80 free ((void *) result);
81 __set_errno (ENOMEM);
82 return (nl_catd) -1;
85 if (__secure_getenv ("NLSPATH") != NULL)
86 result->nlspath = __strdup (getenv ("NLSPATH"));
87 else
88 result->nlspath = __strdup (NLSPATH);
90 if (result->nlspath == NULL)
92 free ((void *) result->cat_name);
93 free ((void *) result->env_var);
94 free ((void *) result);
95 __set_errno (ENOMEM);
96 return (nl_catd) -1;
99 else
101 result->env_var = NULL;
102 result->nlspath = NULL;
105 return (nl_catd) result;
109 /* Return message from message catalog. */
110 char *
111 catgets (nl_catd catalog_desc, int set, int message, const char *string)
113 __nl_catd catalog;
114 size_t idx;
115 size_t cnt;
117 /* Be generous if catalog which failed to be open is used. */
118 if (catalog_desc == (nl_catd) -1 || ++set <= 0 || message < 0)
119 return (char *) string;
121 catalog = (__nl_catd) catalog_desc;
123 if (catalog->status == closed)
124 __open_catalog (catalog, 1);
126 if (catalog->status == nonexisting)
128 __set_errno (EBADF);
129 return (char *) string;
132 idx = ((set * message) % catalog->plane_size) * 3;
133 cnt = 0;
136 if (catalog->name_ptr[idx + 0] == (u_int32_t) set
137 && catalog->name_ptr[idx + 1] == (u_int32_t) message)
138 return (char *) &catalog->strings[catalog->name_ptr[idx + 2]];
140 idx += catalog->plane_size * 3;
142 while (++cnt < catalog->plane_depth);
144 __set_errno (ENOMSG);
145 return (char *) string;
149 /* Return resources used for loaded message catalog. */
151 catclose (nl_catd catalog_desc)
153 __nl_catd catalog;
155 catalog = (__nl_catd) catalog_desc;
157 if (catalog->status == mmaped)
158 munmap ((void *) catalog->file_ptr, catalog->file_size);
159 else if (catalog->status == malloced)
160 free ((void *) catalog->file_ptr);
161 else if (catalog->status != closed && catalog->status != nonexisting)
163 __set_errno (EBADF);
164 return -1;
167 if (catalog->nlspath)
168 free ((void *) catalog->nlspath);
169 if (catalog->env_var)
170 free ((void *) catalog->env_var);
171 free ((void *) catalog);
173 return 0;