textdomain.c 3.8 KB

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