]>
Commit | Line | Data |
---|---|---|
688903eb | 1 | /* Copyright (C) 1996-2018 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 | 18 | |
2c6fe0bd | 19 | #include <errno.h> |
b6aa34eb | 20 | #include <locale.h> |
a641835a RM |
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; | |
390500b1 UD |
35 | const char *env_var = NULL; |
36 | const char *nlspath = NULL; | |
0f585390 | 37 | char *tmp = 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; |
0f585390 PP |
57 | tmp = malloc (len); |
58 | ||
59 | if (__glibc_unlikely (tmp == NULL)) | |
60 | return (nl_catd) -1; | |
0d8733c4 UD |
61 | |
62 | __stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH); | |
63 | nlspath = tmp; | |
64 | } | |
65 | else | |
ca130fe4 | 66 | nlspath = NLSPATH; |
a641835a | 67 | } |
390500b1 | 68 | |
ca130fe4 | 69 | result = (__nl_catd) malloc (sizeof (*result)); |
390500b1 | 70 | if (result == NULL) |
0f585390 PP |
71 | { |
72 | /* We cannot get enough memory. */ | |
73 | result = (nl_catd) -1; | |
74 | } | |
75 | else if (__open_catalog (cat_name, nlspath, env_var, result) != 0) | |
faaa6f62 UD |
76 | { |
77 | /* Couldn't open the file. */ | |
78 | free ((void *) result); | |
0f585390 | 79 | result = (nl_catd) -1; |
faaa6f62 | 80 | } |
39e16978 | 81 | |
0f585390 | 82 | free (tmp); |
a641835a RM |
83 | return (nl_catd) result; |
84 | } | |
85 | ||
86 | ||
87 | /* Return message from message catalog. */ | |
88 | char * | |
89 | catgets (nl_catd catalog_desc, int set, int message, const char *string) | |
90 | { | |
91 | __nl_catd catalog; | |
92 | size_t idx; | |
93 | size_t cnt; | |
94 | ||
95 | /* Be generous if catalog which failed to be open is used. */ | |
96 | if (catalog_desc == (nl_catd) -1 || ++set <= 0 || message < 0) | |
97 | return (char *) string; | |
98 | ||
99 | catalog = (__nl_catd) catalog_desc; | |
100 | ||
a641835a RM |
101 | idx = ((set * message) % catalog->plane_size) * 3; |
102 | cnt = 0; | |
103 | do | |
104 | { | |
d9fee042 JM |
105 | if (catalog->name_ptr[idx + 0] == (uint32_t) set |
106 | && catalog->name_ptr[idx + 1] == (uint32_t) message) | |
a641835a RM |
107 | return (char *) &catalog->strings[catalog->name_ptr[idx + 2]]; |
108 | ||
109 | idx += catalog->plane_size * 3; | |
110 | } | |
111 | while (++cnt < catalog->plane_depth); | |
112 | ||
2c6fe0bd | 113 | __set_errno (ENOMSG); |
a641835a RM |
114 | return (char *) string; |
115 | } | |
116 | ||
117 | ||
118 | /* Return resources used for loaded message catalog. */ | |
119 | int | |
120 | catclose (nl_catd catalog_desc) | |
121 | { | |
122 | __nl_catd catalog; | |
123 | ||
30d13751 UD |
124 | /* Be generous if catalog which failed to be open is used. */ |
125 | if (catalog_desc == (nl_catd) -1) | |
126 | { | |
127 | __set_errno (EBADF); | |
128 | return -1; | |
129 | } | |
130 | ||
a641835a RM |
131 | catalog = (__nl_catd) catalog_desc; |
132 | ||
7cabd57c | 133 | #ifdef _POSIX_MAPPED_FILES |
6d52618b | 134 | if (catalog->status == mmapped) |
40a55d20 | 135 | __munmap ((void *) catalog->file_ptr, catalog->file_size); |
7cabd57c UD |
136 | else |
137 | #endif /* _POSIX_MAPPED_FILES */ | |
138 | if (catalog->status == malloced) | |
139 | free ((void *) catalog->file_ptr); | |
ca130fe4 | 140 | else |
7cabd57c UD |
141 | { |
142 | __set_errno (EBADF); | |
143 | return -1; | |
144 | } | |
a641835a | 145 | |
a641835a RM |
146 | free ((void *) catalog); |
147 | ||
148 | return 0; | |
149 | } |