]>
Commit | Line | Data |
---|---|---|
c84142e8 | 1 | /* Implementation of the textdomain(3) function. |
dff8da6b | 2 | Copyright (C) 1995-2024 Free Software Foundation, Inc. |
24906b43 | 3 | |
6d248857 WN |
4 | This program is free software: you can redistribute it and/or modify |
5 | it under the terms of the GNU Lesser General Public License as published by | |
6 | the Free Software Foundation; either version 2.1 of the License, or | |
7 | (at your option) any later version. | |
0393dfd6 | 8 | |
6d248857 | 9 | This program is distributed in the hope that it will be useful, |
c84142e8 | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
6d248857 WN |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU Lesser General Public License for more details. | |
24906b43 | 13 | |
6d248857 | 14 | You should have received a copy of the GNU Lesser General Public License |
5a82c748 | 15 | along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
24906b43 RM |
16 | |
17 | #ifdef HAVE_CONFIG_H | |
18 | # include <config.h> | |
19 | #endif | |
20 | ||
0555fcce UD |
21 | #include <stdlib.h> |
22 | #include <string.h> | |
24906b43 | 23 | |
6d248857 | 24 | #include "gettextP.h" |
24906b43 RM |
25 | #ifdef _LIBC |
26 | # include <libintl.h> | |
27 | #else | |
4a4d50f3 | 28 | # include "libgnuintl.h" |
24906b43 | 29 | #endif |
0ed99ce4 | 30 | |
6d248857 | 31 | /* Handle multi-threaded applications. */ |
0ed99ce4 | 32 | #ifdef _LIBC |
ec999b8e | 33 | # include <libc-lock.h> |
6d248857 WN |
34 | # define gl_rwlock_define __libc_rwlock_define |
35 | # define gl_rwlock_wrlock __libc_rwlock_wrlock | |
36 | # define gl_rwlock_unlock __libc_rwlock_unlock | |
0ed99ce4 | 37 | #else |
6d248857 | 38 | # include "lock.h" |
52d895a4 UD |
39 | #endif |
40 | ||
24906b43 RM |
41 | /* @@ end of prolog @@ */ |
42 | ||
24906b43 RM |
43 | |
44 | /* Names for the libintl functions are a problem. They must not clash | |
45 | with existing names and they should follow ANSI C. But this source | |
46 | code is also used in GNU C Library where the names have a __ | |
47 | prefix. So we have to make a difference here. */ | |
48 | #ifdef _LIBC | |
49 | # define TEXTDOMAIN __textdomain | |
f38931a9 UD |
50 | # ifndef strdup |
51 | # define strdup(str) __strdup (str) | |
52 | # endif | |
24906b43 | 53 | #else |
0f131646 | 54 | # define TEXTDOMAIN libintl_textdomain |
24906b43 RM |
55 | #endif |
56 | ||
0ed99ce4 | 57 | /* Lock variable to protect the global data in the gettext implementation. */ |
6d248857 | 58 | gl_rwlock_define (extern, _nl_state_lock attribute_hidden) |
0ed99ce4 | 59 | |
24906b43 RM |
60 | /* Set the current default message catalog to DOMAINNAME. |
61 | If DOMAINNAME is null, return the current default. | |
62 | If DOMAINNAME is "", reset to the default of "messages". */ | |
63 | char * | |
6d248857 | 64 | TEXTDOMAIN (const char *domainname) |
24906b43 | 65 | { |
0ed99ce4 UD |
66 | char *new_domain; |
67 | char *old_domain; | |
24906b43 RM |
68 | |
69 | /* A NULL pointer requests the current setting. */ | |
70 | if (domainname == NULL) | |
71 | return (char *) _nl_current_default_domain; | |
72 | ||
6d248857 | 73 | gl_rwlock_wrlock (_nl_state_lock); |
0ed99ce4 UD |
74 | |
75 | old_domain = (char *) _nl_current_default_domain; | |
24906b43 RM |
76 | |
77 | /* If domain name is the null string set to default domain "messages". */ | |
78 | if (domainname[0] == '\0' | |
79 | || strcmp (domainname, _nl_default_default_domain) == 0) | |
0ed99ce4 UD |
80 | { |
81 | _nl_current_default_domain = _nl_default_default_domain; | |
82 | new_domain = (char *) _nl_current_default_domain; | |
83 | } | |
84 | else if (strcmp (domainname, old_domain) == 0) | |
85 | /* This can happen and people will use it to signal that some | |
86 | environment variable changed. */ | |
87 | new_domain = old_domain; | |
24906b43 RM |
88 | else |
89 | { | |
90 | /* If the following malloc fails `_nl_current_default_domain' | |
91 | will be NULL. This value will be returned and so signals we | |
92 | are out of core. */ | |
40a55d20 | 93 | #if defined _LIBC || defined HAVE_STRDUP |
0ed99ce4 | 94 | new_domain = strdup (domainname); |
40a55d20 | 95 | #else |
24906b43 | 96 | size_t len = strlen (domainname) + 1; |
0ed99ce4 UD |
97 | new_domain = (char *) malloc (len); |
98 | if (new_domain != NULL) | |
99 | memcpy (new_domain, domainname, len); | |
40a55d20 | 100 | #endif |
0ed99ce4 UD |
101 | |
102 | if (new_domain != NULL) | |
103 | _nl_current_default_domain = new_domain; | |
104 | } | |
105 | ||
106 | /* We use this possibility to signal a change of the loaded catalogs | |
107 | since this is most likely the case and there is no other easy we | |
108 | to do it. Do it only when the call was successful. */ | |
109 | if (new_domain != NULL) | |
110 | { | |
111 | ++_nl_msg_cat_cntr; | |
112 | ||
113 | if (old_domain != new_domain && old_domain != _nl_default_default_domain) | |
114 | free (old_domain); | |
24906b43 RM |
115 | } |
116 | ||
6d248857 | 117 | gl_rwlock_unlock (_nl_state_lock); |
24906b43 | 118 | |
0ed99ce4 | 119 | return new_domain; |
24906b43 RM |
120 | } |
121 | ||
122 | #ifdef _LIBC | |
123 | /* Alias for function name in GNU C Library. */ | |
124 | weak_alias (__textdomain, textdomain); | |
125 | #endif |