Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-runtime / intl / textdomain.c
1 /* Implementation of the textdomain(3) function.
2    Copyright (C) 1995-2015 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "gettextP.h"
25 #ifdef _LIBC
26 # include <libintl.h>
27 #else
28 # include "libgnuintl.h"
29 #endif
30
31 /* Handle multi-threaded applications.  */
32 #ifdef _LIBC
33 # include <bits/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
37 #else
38 # include "lock.h"
39 #endif
40
41 /* @@ end of prolog @@ */
42
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
50 # ifndef strdup
51 #  define strdup(str) __strdup (str)
52 # endif
53 #else
54 # define TEXTDOMAIN libintl_textdomain
55 #endif
56
57 /* Lock variable to protect the global data in the gettext implementation.  */
58 gl_rwlock_define (extern, _nl_state_lock attribute_hidden)
59
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 *
64 TEXTDOMAIN (const char *domainname)
65 {
66   char *new_domain;
67   char *old_domain;
68
69   /* A NULL pointer requests the current setting.  */
70   if (domainname == NULL)
71     return (char *) _nl_current_default_domain;
72
73   gl_rwlock_wrlock (_nl_state_lock);
74
75   old_domain = (char *) _nl_current_default_domain;
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)
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;
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.  */
93 #if defined _LIBC || defined HAVE_STRDUP
94       new_domain = strdup (domainname);
95 #else
96       size_t len = strlen (domainname) + 1;
97       new_domain = (char *) malloc (len);
98       if (new_domain != NULL)
99         memcpy (new_domain, domainname, len);
100 #endif
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);
115     }
116
117   gl_rwlock_unlock (_nl_state_lock);
118
119   return new_domain;
120 }
121
122 #ifdef _LIBC
123 /* Alias for function name in GNU C Library.  */
124 weak_alias (__textdomain, textdomain);
125 #endif