]>
Commit | Line | Data |
---|---|---|
6d7e8eda | 1 | /* Copyright (C) 1996-2023 Free Software Foundation, Inc. |
2c6fe0bd | 2 | This file is part of the GNU C Library. |
a641835a | 3 | |
2c6fe0bd | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
a641835a | 8 | |
2c6fe0bd UD |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
a641835a | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
a641835a | 17 | |
2c6fe0bd | 18 | #include <errno.h> |
b6aa34eb | 19 | #include <locale.h> |
a641835a RM |
20 | #include <nl_types.h> |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
24 | #include <sys/mman.h> | |
25 | ||
26 | #include "catgetsinfo.h" | |
27 | ||
28 | ||
29 | /* Open the catalog and return a descriptor for the catalog. */ | |
30 | nl_catd | |
31 | catopen (const char *cat_name, int flag) | |
32 | { | |
33 | __nl_catd result; | |
390500b1 UD |
34 | const char *env_var = NULL; |
35 | const char *nlspath = NULL; | |
0f585390 | 36 | char *tmp = NULL; |
a641835a RM |
37 | |
38 | if (strchr (cat_name, '/') == NULL) | |
39 | { | |
40 | if (flag == NL_CAT_LOCALE) | |
b6aa34eb UD |
41 | /* Use the current locale setting for LC_MESSAGES. */ |
42 | env_var = setlocale (LC_MESSAGES, NULL); | |
43 | else | |
44 | /* Use the LANG environment variable. */ | |
45 | env_var = getenv ("LANG"); | |
a641835a | 46 | |
63336471 UD |
47 | if (env_var == NULL || *env_var == '\0' |
48 | || (__libc_enable_secure && strchr (env_var, '/') != NULL)) | |
390500b1 UD |
49 | env_var = "C"; |
50 | ||
74955460 | 51 | nlspath = getenv ("NLSPATH"); |
0d8733c4 UD |
52 | if (nlspath != NULL && *nlspath != '\0') |
53 | { | |
54 | /* Append the system dependent directory. */ | |
39e16978 | 55 | size_t len = strlen (nlspath) + 1 + sizeof NLSPATH; |
0f585390 PP |
56 | tmp = malloc (len); |
57 | ||
58 | if (__glibc_unlikely (tmp == NULL)) | |
59 | return (nl_catd) -1; | |
0d8733c4 UD |
60 | |
61 | __stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH); | |
62 | nlspath = tmp; | |
63 | } | |
64 | else | |
ca130fe4 | 65 | nlspath = NLSPATH; |
a641835a | 66 | } |
390500b1 | 67 | |
ca130fe4 | 68 | result = (__nl_catd) malloc (sizeof (*result)); |
390500b1 | 69 | if (result == NULL) |
0f585390 PP |
70 | { |
71 | /* We cannot get enough memory. */ | |
72 | result = (nl_catd) -1; | |
73 | } | |
74 | else if (__open_catalog (cat_name, nlspath, env_var, result) != 0) | |
faaa6f62 UD |
75 | { |
76 | /* Couldn't open the file. */ | |
77 | free ((void *) result); | |
0f585390 | 78 | result = (nl_catd) -1; |
faaa6f62 | 79 | } |
39e16978 | 80 | |
0f585390 | 81 | free (tmp); |
a641835a RM |
82 | return (nl_catd) result; |
83 | } | |
84 | ||
85 | ||
86 | /* Return message from message catalog. */ | |
87 | char * | |
88 | catgets (nl_catd catalog_desc, int set, int message, const char *string) | |
89 | { | |
90 | __nl_catd catalog; | |
91 | size_t idx; | |
92 | size_t cnt; | |
93 | ||
94 | /* Be generous if catalog which failed to be open is used. */ | |
95 | if (catalog_desc == (nl_catd) -1 || ++set <= 0 || message < 0) | |
96 | return (char *) string; | |
97 | ||
98 | catalog = (__nl_catd) catalog_desc; | |
99 | ||
a641835a RM |
100 | idx = ((set * message) % catalog->plane_size) * 3; |
101 | cnt = 0; | |
102 | do | |
103 | { | |
d9fee042 JM |
104 | if (catalog->name_ptr[idx + 0] == (uint32_t) set |
105 | && catalog->name_ptr[idx + 1] == (uint32_t) message) | |
a641835a RM |
106 | return (char *) &catalog->strings[catalog->name_ptr[idx + 2]]; |
107 | ||
108 | idx += catalog->plane_size * 3; | |
109 | } | |
110 | while (++cnt < catalog->plane_depth); | |
111 | ||
2c6fe0bd | 112 | __set_errno (ENOMSG); |
a641835a RM |
113 | return (char *) string; |
114 | } | |
115 | ||
116 | ||
117 | /* Return resources used for loaded message catalog. */ | |
118 | int | |
119 | catclose (nl_catd catalog_desc) | |
120 | { | |
121 | __nl_catd catalog; | |
122 | ||
30d13751 UD |
123 | /* Be generous if catalog which failed to be open is used. */ |
124 | if (catalog_desc == (nl_catd) -1) | |
125 | { | |
126 | __set_errno (EBADF); | |
127 | return -1; | |
128 | } | |
129 | ||
a641835a RM |
130 | catalog = (__nl_catd) catalog_desc; |
131 | ||
7cabd57c | 132 | #ifdef _POSIX_MAPPED_FILES |
6d52618b | 133 | if (catalog->status == mmapped) |
40a55d20 | 134 | __munmap ((void *) catalog->file_ptr, catalog->file_size); |
7cabd57c UD |
135 | else |
136 | #endif /* _POSIX_MAPPED_FILES */ | |
137 | if (catalog->status == malloced) | |
138 | free ((void *) catalog->file_ptr); | |
ca130fe4 | 139 | else |
7cabd57c UD |
140 | { |
141 | __set_errno (EBADF); | |
142 | return -1; | |
143 | } | |
a641835a | 144 | |
a641835a RM |
145 | free ((void *) catalog); |
146 | ||
147 | return 0; | |
148 | } |