1 /* Implementation of the textdomain(3) function.
2 Copyright (C) 1995-2018 Free Software Foundation, Inc.
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.
9 This program 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
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 # include "libgnuintl.h"
31 /* Handle multi-threaded applications. */
33 # include <libc-lock.h>
34 # define gl_rwlock_define __libc_rwlock_define
35 # define gl_rwlock_wrlock __libc_rwlock_wrlock
36 # define gl_rwlock_unlock __libc_rwlock_unlock
41 /* @@ end of prolog @@ */
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. */
49 # define TEXTDOMAIN __textdomain
51 # define strdup(str) __strdup (str)
54 # define TEXTDOMAIN libintl_textdomain
57 /* Lock variable to protect the global data in the gettext implementation. */
58 gl_rwlock_define (extern, _nl_state_lock attribute_hidden
)
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". */
64 TEXTDOMAIN (const char *domainname
)
69 /* A NULL pointer requests the current setting. */
70 if (domainname
== NULL
)
71 return (char *) _nl_current_default_domain
;
73 gl_rwlock_wrlock (_nl_state_lock
);
75 old_domain
= (char *) _nl_current_default_domain
;
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)
81 _nl_current_default_domain
= _nl_default_default_domain
;
82 new_domain
= (char *) _nl_current_default_domain
;
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
;
90 /* If the following malloc fails `_nl_current_default_domain'
91 will be NULL. This value will be returned and so signals we
93 #if defined _LIBC || defined HAVE_STRDUP
94 new_domain
= strdup (domainname
);
96 size_t len
= strlen (domainname
) + 1;
97 new_domain
= (char *) malloc (len
);
98 if (new_domain
!= NULL
)
99 memcpy (new_domain
, domainname
, len
);
102 if (new_domain
!= NULL
)
103 _nl_current_default_domain
= new_domain
;
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
)
113 if (old_domain
!= new_domain
&& old_domain
!= _nl_default_default_domain
)
117 gl_rwlock_unlock (_nl_state_lock
);
123 /* Alias for function name in GNU C Library. */
124 weak_alias (__textdomain
, textdomain
);