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