]> git.ipfire.org Git - thirdparty/glibc.git/blame - catgets/catgets.c
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / catgets / catgets.c
CommitLineData
faaa6f62 1/* Copyright (C) 1996-2000, 2001, 2002 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 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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
41bdb6e2 13 Lesser General Public License for more details.
a641835a 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
a641835a
RM
18
19#include <alloca.h>
2c6fe0bd 20#include <errno.h>
b6aa34eb 21#include <locale.h>
a641835a
RM
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. */
32nl_catd
33catopen (const char *cat_name, int flag)
34{
35 __nl_catd result;
390500b1
UD
36 const char *env_var = NULL;
37 const char *nlspath = NULL;
a641835a
RM
38
39 if (strchr (cat_name, '/') == NULL)
40 {
41 if (flag == NL_CAT_LOCALE)
b6aa34eb
UD
42 /* Use the current locale setting for LC_MESSAGES. */
43 env_var = setlocale (LC_MESSAGES, NULL);
44 else
45 /* Use the LANG environment variable. */
46 env_var = getenv ("LANG");
a641835a 47
63336471
UD
48 if (env_var == NULL || *env_var == '\0'
49 || (__libc_enable_secure && strchr (env_var, '/') != NULL))
390500b1
UD
50 env_var = "C";
51
74955460 52 nlspath = getenv ("NLSPATH");
0d8733c4
UD
53 if (nlspath != NULL && *nlspath != '\0')
54 {
55 /* Append the system dependent directory. */
39e16978 56 size_t len = strlen (nlspath) + 1 + sizeof NLSPATH;
0d8733c4
UD
57 char *tmp = alloca (len);
58
59 __stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH);
60 nlspath = tmp;
61 }
62 else
ca130fe4 63 nlspath = NLSPATH;
a641835a 64 }
390500b1 65
ca130fe4 66 result = (__nl_catd) malloc (sizeof (*result));
390500b1
UD
67 if (result == NULL)
68 /* We cannot get enough memory. */
69 return (nl_catd) -1;
70
ca130fe4 71 if (__open_catalog (cat_name, nlspath, env_var, result) != 0)
faaa6f62
UD
72 {
73 /* Couldn't open the file. */
74 free ((void *) result);
75 return (nl_catd) -1;
76 }
39e16978 77
a641835a
RM
78 return (nl_catd) result;
79}
80
81
82/* Return message from message catalog. */
83char *
84catgets (nl_catd catalog_desc, int set, int message, const char *string)
85{
86 __nl_catd catalog;
87 size_t idx;
88 size_t cnt;
89
90 /* Be generous if catalog which failed to be open is used. */
91 if (catalog_desc == (nl_catd) -1 || ++set <= 0 || message < 0)
92 return (char *) string;
93
94 catalog = (__nl_catd) catalog_desc;
95
a641835a
RM
96 idx = ((set * message) % catalog->plane_size) * 3;
97 cnt = 0;
98 do
99 {
100 if (catalog->name_ptr[idx + 0] == (u_int32_t) set
101 && catalog->name_ptr[idx + 1] == (u_int32_t) message)
102 return (char *) &catalog->strings[catalog->name_ptr[idx + 2]];
103
104 idx += catalog->plane_size * 3;
105 }
106 while (++cnt < catalog->plane_depth);
107
2c6fe0bd 108 __set_errno (ENOMSG);
a641835a
RM
109 return (char *) string;
110}
111
112
113/* Return resources used for loaded message catalog. */
114int
115catclose (nl_catd catalog_desc)
116{
117 __nl_catd catalog;
118
30d13751
UD
119 /* Be generous if catalog which failed to be open is used. */
120 if (catalog_desc == (nl_catd) -1)
121 {
122 __set_errno (EBADF);
123 return -1;
124 }
125
a641835a
RM
126 catalog = (__nl_catd) catalog_desc;
127
7cabd57c 128#ifdef _POSIX_MAPPED_FILES
6d52618b 129 if (catalog->status == mmapped)
40a55d20 130 __munmap ((void *) catalog->file_ptr, catalog->file_size);
7cabd57c
UD
131 else
132#endif /* _POSIX_MAPPED_FILES */
133 if (catalog->status == malloced)
134 free ((void *) catalog->file_ptr);
ca130fe4 135 else
7cabd57c
UD
136 {
137 __set_errno (EBADF);
138 return -1;
139 }
a641835a 140
a641835a
RM
141 free ((void *) catalog);
142
143 return 0;
144}