(root)/
gettext-0.22.4/
gettext-runtime/
intl/
textdomain.c
       1  /* Implementation of the textdomain(3) function.
       2     Copyright (C) 1995-2015, 2021 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 <https://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 "glthread/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 strdup fails '_nl_current_default_domain'
      91  	 will be NULL.  This value will be returned and so signals we
      92  	 are out of memory.  */
      93        new_domain = strdup (domainname);
      94        if (new_domain != NULL)
      95  	_nl_current_default_domain = new_domain;
      96      }
      97  
      98    /* We use this possibility to signal a change of the loaded catalogs
      99       since this is most likely the case and there is no other easy we
     100       to do it.  Do it only when the call was successful.  */
     101    if (new_domain != NULL)
     102      {
     103        ++_nl_msg_cat_cntr;
     104  
     105        if (old_domain != new_domain && old_domain != _nl_default_default_domain)
     106  	free (old_domain);
     107      }
     108  
     109    gl_rwlock_unlock (_nl_state_lock);
     110  
     111    return new_domain;
     112  }
     113  
     114  #ifdef _LIBC
     115  /* Alias for function name in GNU C Library.  */
     116  weak_alias (__textdomain, textdomain);
     117  #endif