Update.
[glibc.git] / intl / textdomain.c
bloba5b93fa4209eadcf1aeafa82ab9af963d0110539
1 /* Implementation of the textdomain(3) function.
2 Copyright (C) 1995-1998, 2000, 2001 Free Software Foundation, Inc.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
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
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #if defined STDC_HEADERS || defined _LIBC
24 # include <stdlib.h>
25 #endif
27 #if defined STDC_HEADERS || defined HAVE_STRING_H || defined _LIBC
28 # include <string.h>
29 #else
30 # include <strings.h>
31 # ifndef memcpy
32 # define memcpy(Dst, Src, Num) (bcopy (Src, Dst, Num), (Dst))
33 # endif
34 #endif
36 #ifdef _LIBC
37 # include <libintl.h>
38 #else
39 # include "libgnuintl.h"
40 #endif
41 #include "gettextP.h"
43 #ifdef _LIBC
44 /* We have to handle multi-threaded applications. */
45 # include <bits/libc-lock.h>
46 #else
47 /* Provide dummy implementation if this is outside glibc. */
48 # define __libc_rwlock_define(CLASS, NAME)
49 # define __libc_rwlock_wrlock(NAME)
50 # define __libc_rwlock_unlock(NAME)
51 #endif
53 /* The internal variables in the standalone libintl.a must have different
54 names than the internal variables in GNU libc, otherwise programs
55 using libintl.a cannot be linked statically. */
56 #if !defined _LIBC
57 # define _nl_default_default_domain _nl_default_default_domain__
58 # define _nl_current_default_domain _nl_current_default_domain__
59 #endif
61 /* @@ end of prolog @@ */
63 /* Name of the default text domain. */
64 extern const char _nl_default_default_domain[];
66 /* Default text domain in which entries for gettext(3) are to be found. */
67 extern const char *_nl_current_default_domain;
70 /* Names for the libintl functions are a problem. They must not clash
71 with existing names and they should follow ANSI C. But this source
72 code is also used in GNU C Library where the names have a __
73 prefix. So we have to make a difference here. */
74 #ifdef _LIBC
75 # define TEXTDOMAIN __textdomain
76 # ifndef strdup
77 # define strdup(str) __strdup (str)
78 # endif
79 #else
80 # define TEXTDOMAIN textdomain__
81 #endif
83 /* Lock variable to protect the global data in the gettext implementation. */
84 __libc_rwlock_define (extern, _nl_state_lock)
86 /* Set the current default message catalog to DOMAINNAME.
87 If DOMAINNAME is null, return the current default.
88 If DOMAINNAME is "", reset to the default of "messages". */
89 char *
90 TEXTDOMAIN (domainname)
91 const char *domainname;
93 char *new_domain;
94 char *old_domain;
96 /* A NULL pointer requests the current setting. */
97 if (domainname == NULL)
98 return (char *) _nl_current_default_domain;
100 __libc_rwlock_wrlock (_nl_state_lock);
102 old_domain = (char *) _nl_current_default_domain;
104 /* If domain name is the null string set to default domain "messages". */
105 if (domainname[0] == '\0'
106 || strcmp (domainname, _nl_default_default_domain) == 0)
108 _nl_current_default_domain = _nl_default_default_domain;
109 new_domain = (char *) _nl_current_default_domain;
111 else if (strcmp (domainname, old_domain) == 0)
112 /* This can happen and people will use it to signal that some
113 environment variable changed. */
114 new_domain = old_domain;
115 else
117 /* If the following malloc fails `_nl_current_default_domain'
118 will be NULL. This value will be returned and so signals we
119 are out of core. */
120 #if defined _LIBC || defined HAVE_STRDUP
121 new_domain = strdup (domainname);
122 #else
123 size_t len = strlen (domainname) + 1;
124 new_domain = (char *) malloc (len);
125 if (new_domain != NULL)
126 memcpy (new_domain, domainname, len);
127 #endif
129 if (new_domain != NULL)
130 _nl_current_default_domain = new_domain;
133 /* We use this possibility to signal a change of the loaded catalogs
134 since this is most likely the case and there is no other easy we
135 to do it. Do it only when the call was successful. */
136 if (new_domain != NULL)
138 ++_nl_msg_cat_cntr;
140 if (old_domain != new_domain && old_domain != _nl_default_default_domain)
141 free (old_domain);
144 __libc_rwlock_unlock (_nl_state_lock);
146 return new_domain;
149 #ifdef _LIBC
150 /* Alias for function name in GNU C Library. */
151 weak_alias (__textdomain, textdomain);
152 #endif