Back to index

glibc  2.9
textdomain.c
Go to the documentation of this file.
00001 /* Implementation of the textdomain(3) function.
00002    Copyright (C) 1995-1998, 2000, 2001, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #ifdef HAVE_CONFIG_H
00021 # include <config.h>
00022 #endif
00023 
00024 #include <stdlib.h>
00025 #include <string.h>
00026 
00027 #ifdef _LIBC
00028 # include <libintl.h>
00029 #else
00030 # include "libgnuintl.h"
00031 #endif
00032 #include "gettextP.h"
00033 
00034 #ifdef _LIBC
00035 /* We have to handle multi-threaded applications.  */
00036 # include <bits/libc-lock.h>
00037 #else
00038 /* Provide dummy implementation if this is outside glibc.  */
00039 # define __libc_rwlock_define(CLASS, NAME)
00040 # define __libc_rwlock_wrlock(NAME)
00041 # define __libc_rwlock_unlock(NAME)
00042 #endif
00043 
00044 /* The internal variables in the standalone libintl.a must have different
00045    names than the internal variables in GNU libc, otherwise programs
00046    using libintl.a cannot be linked statically.  */
00047 #if !defined _LIBC
00048 # define _nl_default_default_domain libintl_nl_default_default_domain
00049 # define _nl_current_default_domain libintl_nl_current_default_domain
00050 #endif
00051 
00052 /* @@ end of prolog @@ */
00053 
00054 /* Name of the default text domain.  */
00055 extern const char _nl_default_default_domain[] attribute_hidden;
00056 
00057 /* Default text domain in which entries for gettext(3) are to be found.  */
00058 extern const char *_nl_current_default_domain attribute_hidden;
00059 
00060 
00061 /* Names for the libintl functions are a problem.  They must not clash
00062    with existing names and they should follow ANSI C.  But this source
00063    code is also used in GNU C Library where the names have a __
00064    prefix.  So we have to make a difference here.  */
00065 #ifdef _LIBC
00066 # define TEXTDOMAIN __textdomain
00067 # ifndef strdup
00068 #  define strdup(str) __strdup (str)
00069 # endif
00070 #else
00071 # define TEXTDOMAIN libintl_textdomain
00072 #endif
00073 
00074 /* Lock variable to protect the global data in the gettext implementation.  */
00075 __libc_rwlock_define (extern, _nl_state_lock attribute_hidden)
00076 
00077 /* Set the current default message catalog to DOMAINNAME.
00078    If DOMAINNAME is null, return the current default.
00079    If DOMAINNAME is "", reset to the default of "messages".  */
00080 char *
00081 TEXTDOMAIN (domainname)
00082      const char *domainname;
00083 {
00084   char *new_domain;
00085   char *old_domain;
00086 
00087   /* A NULL pointer requests the current setting.  */
00088   if (domainname == NULL)
00089     return (char *) _nl_current_default_domain;
00090 
00091   __libc_rwlock_wrlock (_nl_state_lock);
00092 
00093   old_domain = (char *) _nl_current_default_domain;
00094 
00095   /* If domain name is the null string set to default domain "messages".  */
00096   if (domainname[0] == '\0'
00097       || strcmp (domainname, _nl_default_default_domain) == 0)
00098     {
00099       _nl_current_default_domain = _nl_default_default_domain;
00100       new_domain = (char *) _nl_current_default_domain;
00101     }
00102   else if (strcmp (domainname, old_domain) == 0)
00103     /* This can happen and people will use it to signal that some
00104        environment variable changed.  */
00105     new_domain = old_domain;
00106   else
00107     {
00108       /* If the following malloc fails `_nl_current_default_domain'
00109         will be NULL.  This value will be returned and so signals we
00110         are out of core.  */
00111 #if defined _LIBC || defined HAVE_STRDUP
00112       new_domain = strdup (domainname);
00113 #else
00114       size_t len = strlen (domainname) + 1;
00115       new_domain = (char *) malloc (len);
00116       if (new_domain != NULL)
00117        memcpy (new_domain, domainname, len);
00118 #endif
00119 
00120       if (new_domain != NULL)
00121        _nl_current_default_domain = new_domain;
00122     }
00123 
00124   /* We use this possibility to signal a change of the loaded catalogs
00125      since this is most likely the case and there is no other easy we
00126      to do it.  Do it only when the call was successful.  */
00127   if (new_domain != NULL)
00128     {
00129       ++_nl_msg_cat_cntr;
00130 
00131       if (old_domain != new_domain && old_domain != _nl_default_default_domain)
00132        free (old_domain);
00133     }
00134 
00135   __libc_rwlock_unlock (_nl_state_lock);
00136 
00137   return new_domain;
00138 }
00139 
00140 #ifdef _LIBC
00141 /* Alias for function name in GNU C Library.  */
00142 weak_alias (__textdomain, textdomain);
00143 #endif