1 1.1 mrg /* Implementation of the textdomain(3) function. 2 1.1 mrg Copyright (C) 1995-1998, 2000, 2001, 2002 Free Software Foundation, Inc. 3 1.1 mrg 4 1.1 mrg This program is free software; you can redistribute it and/or modify it 5 1.1 mrg under the terms of the GNU Library General Public License as published 6 1.1 mrg by the Free Software Foundation; either version 2, or (at your option) 7 1.1 mrg any later version. 8 1.1 mrg 9 1.1 mrg This program is distributed in the hope that it will be useful, 10 1.1 mrg but WITHOUT ANY WARRANTY; without even the implied warranty of 11 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 1.1 mrg Library General Public License for more details. 13 1.1 mrg 14 1.1 mrg You should have received a copy of the GNU Library General Public 15 1.1 mrg License along with this program; if not, write to the Free Software 16 1.1 mrg Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 17 1.1 mrg USA. */ 18 1.1 mrg 19 1.1 mrg #ifdef HAVE_CONFIG_H 20 1.1 mrg # include <config.h> 21 1.1 mrg #endif 22 1.1 mrg 23 1.1 mrg #include <stdlib.h> 24 1.1 mrg #include <string.h> 25 1.1 mrg 26 1.1 mrg #ifdef _LIBC 27 1.1 mrg # include <libintl.h> 28 1.1 mrg #else 29 1.1 mrg # include "libgnuintl.h" 30 1.1 mrg #endif 31 1.1 mrg #include "gettextP.h" 32 1.1 mrg 33 1.1 mrg #ifdef _LIBC 34 1.1 mrg /* We have to handle multi-threaded applications. */ 35 1.1 mrg # include <bits/libc-lock.h> 36 1.1 mrg #else 37 1.1 mrg /* Provide dummy implementation if this is outside glibc. */ 38 1.1 mrg # define __libc_rwlock_define(CLASS, NAME) 39 1.1 mrg # define __libc_rwlock_wrlock(NAME) 40 1.1 mrg # define __libc_rwlock_unlock(NAME) 41 1.1 mrg #endif 42 1.1 mrg 43 1.1 mrg /* The internal variables in the standalone libintl.a must have different 44 1.1 mrg names than the internal variables in GNU libc, otherwise programs 45 1.1 mrg using libintl.a cannot be linked statically. */ 46 1.1 mrg #if !defined _LIBC 47 1.1 mrg # define _nl_default_default_domain libintl_nl_default_default_domain 48 1.1 mrg # define _nl_current_default_domain libintl_nl_current_default_domain 49 1.1 mrg #endif 50 1.1 mrg 51 1.1 mrg /* @@ end of prolog @@ */ 52 1.1 mrg 53 1.1 mrg /* Name of the default text domain. */ 54 1.1 mrg extern const char _nl_default_default_domain[] attribute_hidden; 55 1.1 mrg 56 1.1 mrg /* Default text domain in which entries for gettext(3) are to be found. */ 57 1.1 mrg extern const char *_nl_current_default_domain attribute_hidden; 58 1.1 mrg 59 1.1 mrg 60 1.1 mrg /* Names for the libintl functions are a problem. They must not clash 61 1.1 mrg with existing names and they should follow ANSI C. But this source 62 1.1 mrg code is also used in GNU C Library where the names have a __ 63 1.1 mrg prefix. So we have to make a difference here. */ 64 1.1 mrg #ifdef _LIBC 65 1.1 mrg # define TEXTDOMAIN __textdomain 66 1.1 mrg # ifndef strdup 67 1.1 mrg # define strdup(str) __strdup (str) 68 1.1 mrg # endif 69 1.1 mrg #else 70 1.1 mrg # define TEXTDOMAIN libintl_textdomain 71 1.1 mrg #endif 72 1.1 mrg 73 1.1 mrg /* Lock variable to protect the global data in the gettext implementation. */ 74 1.1 mrg __libc_rwlock_define (extern, _nl_state_lock attribute_hidden) 75 1.1 mrg 76 1.1 mrg /* Set the current default message catalog to DOMAINNAME. 77 1.1 mrg If DOMAINNAME is null, return the current default. 78 1.1 mrg If DOMAINNAME is "", reset to the default of "messages". */ 79 1.1 mrg char * 80 1.1 mrg TEXTDOMAIN (domainname) 81 1.1 mrg const char *domainname; 82 1.1 mrg { 83 1.1 mrg char *new_domain; 84 1.1 mrg char *old_domain; 85 1.1 mrg 86 1.1 mrg /* A NULL pointer requests the current setting. */ 87 1.1 mrg if (domainname == NULL) 88 1.1 mrg return (char *) _nl_current_default_domain; 89 1.1 mrg 90 1.1 mrg __libc_rwlock_wrlock (_nl_state_lock); 91 1.1 mrg 92 1.1 mrg old_domain = (char *) _nl_current_default_domain; 93 1.1 mrg 94 1.1 mrg /* If domain name is the null string set to default domain "messages". */ 95 1.1 mrg if (domainname[0] == '\0' 96 1.1 mrg || strcmp (domainname, _nl_default_default_domain) == 0) 97 1.1 mrg { 98 1.1 mrg _nl_current_default_domain = _nl_default_default_domain; 99 1.1 mrg new_domain = (char *) _nl_current_default_domain; 100 1.1 mrg } 101 1.1 mrg else if (strcmp (domainname, old_domain) == 0) 102 1.1 mrg /* This can happen and people will use it to signal that some 103 1.1 mrg environment variable changed. */ 104 1.1 mrg new_domain = old_domain; 105 1.1 mrg else 106 1.1 mrg { 107 1.1 mrg /* If the following malloc fails `_nl_current_default_domain' 108 1.1 mrg will be NULL. This value will be returned and so signals we 109 1.1 mrg are out of core. */ 110 1.1 mrg #if defined _LIBC || defined HAVE_STRDUP 111 1.1 mrg new_domain = strdup (domainname); 112 1.1 mrg #else 113 1.1 mrg size_t len = strlen (domainname) + 1; 114 1.1 mrg new_domain = (char *) malloc (len); 115 1.1 mrg if (new_domain != NULL) 116 1.1 mrg memcpy (new_domain, domainname, len); 117 1.1 mrg #endif 118 1.1 mrg 119 1.1 mrg if (new_domain != NULL) 120 1.1 mrg _nl_current_default_domain = new_domain; 121 1.1 mrg } 122 1.1 mrg 123 1.1 mrg /* We use this possibility to signal a change of the loaded catalogs 124 1.1 mrg since this is most likely the case and there is no other easy we 125 1.1 mrg to do it. Do it only when the call was successful. */ 126 1.1 mrg if (new_domain != NULL) 127 1.1 mrg { 128 1.1 mrg ++_nl_msg_cat_cntr; 129 1.1 mrg 130 1.1 mrg if (old_domain != new_domain && old_domain != _nl_default_default_domain) 131 1.1 mrg free (old_domain); 132 1.1 mrg } 133 1.1 mrg 134 1.1 mrg __libc_rwlock_unlock (_nl_state_lock); 135 1.1 mrg 136 1.1 mrg return new_domain; 137 1.1 mrg } 138 1.1 mrg 139 1.1 mrg #ifdef _LIBC 140 1.1 mrg /* Alias for function name in GNU C Library. */ 141 1.1 mrg weak_alias (__textdomain, textdomain); 142 1.1 mrg #endif 143