Back to index

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