textdomain.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Implementation of the textdomain(3) function.
  2. Copyright (C) 1995-1998, 2000, 2001, 2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  14. USA. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #ifdef _LIBC
  21. # include <libintl.h>
  22. #else
  23. # include "libgnuintl.h"
  24. #endif
  25. #include "gettextP.h"
  26. #ifdef _LIBC
  27. /* We have to handle multi-threaded applications. */
  28. # include <bits/libc-lock.h>
  29. #else
  30. /* Provide dummy implementation if this is outside glibc. */
  31. # define __libc_rwlock_define(CLASS, NAME)
  32. # define __libc_rwlock_wrlock(NAME)
  33. # define __libc_rwlock_unlock(NAME)
  34. #endif
  35. /* The internal variables in the standalone libintl.a must have different
  36. names than the internal variables in GNU libc, otherwise programs
  37. using libintl.a cannot be linked statically. */
  38. #if !defined _LIBC
  39. # define _nl_default_default_domain libintl_nl_default_default_domain
  40. # define _nl_current_default_domain libintl_nl_current_default_domain
  41. #endif
  42. /* @@ end of prolog @@ */
  43. /* Name of the default text domain. */
  44. extern const char _nl_default_default_domain[] attribute_hidden;
  45. /* Default text domain in which entries for gettext(3) are to be found. */
  46. extern const char *_nl_current_default_domain attribute_hidden;
  47. /* Names for the libintl functions are a problem. They must not clash
  48. with existing names and they should follow ANSI C. But this source
  49. code is also used in GNU C Library where the names have a __
  50. prefix. So we have to make a difference here. */
  51. #ifdef _LIBC
  52. # define TEXTDOMAIN __textdomain
  53. # ifndef strdup
  54. # define strdup(str) __strdup (str)
  55. # endif
  56. #else
  57. # define TEXTDOMAIN libintl_textdomain
  58. #endif
  59. /* Lock variable to protect the global data in the gettext implementation. */
  60. __libc_rwlock_define (extern, _nl_state_lock attribute_hidden)
  61. /* Set the current default message catalog to DOMAINNAME.
  62. If DOMAINNAME is null, return the current default.
  63. If DOMAINNAME is "", reset to the default of "messages". */
  64. char *
  65. TEXTDOMAIN (domainname)
  66. const char *domainname;
  67. {
  68. char *new_domain;
  69. char *old_domain;
  70. /* A NULL pointer requests the current setting. */
  71. if (domainname == NULL)
  72. return (char *) _nl_current_default_domain;
  73. __libc_rwlock_wrlock (_nl_state_lock);
  74. old_domain = (char *) _nl_current_default_domain;
  75. /* If domain name is the null string set to default domain "messages". */
  76. if (domainname[0] == '\0'
  77. || strcmp (domainname, _nl_default_default_domain) == 0)
  78. {
  79. _nl_current_default_domain = _nl_default_default_domain;
  80. new_domain = (char *) _nl_current_default_domain;
  81. }
  82. else if (strcmp (domainname, old_domain) == 0)
  83. /* This can happen and people will use it to signal that some
  84. environment variable changed. */
  85. new_domain = old_domain;
  86. else
  87. {
  88. /* If the following malloc fails `_nl_current_default_domain'
  89. will be NULL. This value will be returned and so signals we
  90. are out of core. */
  91. #if defined _LIBC || defined HAVE_STRDUP
  92. new_domain = strdup (domainname);
  93. #else
  94. size_t len = strlen (domainname) + 1;
  95. new_domain = (char *) malloc (len);
  96. if (new_domain != NULL)
  97. memcpy (new_domain, domainname, len);
  98. #endif
  99. if (new_domain != NULL)
  100. _nl_current_default_domain = new_domain;
  101. }
  102. /* We use this possibility to signal a change of the loaded catalogs
  103. since this is most likely the case and there is no other easy we
  104. to do it. Do it only when the call was successful. */
  105. if (new_domain != NULL)
  106. {
  107. ++_nl_msg_cat_cntr;
  108. if (old_domain != new_domain && old_domain != _nl_default_default_domain)
  109. free (old_domain);
  110. }
  111. __libc_rwlock_unlock (_nl_state_lock);
  112. return new_domain;
  113. }
  114. #ifdef _LIBC
  115. /* Alias for function name in GNU C Library. */
  116. weak_alias (__textdomain, textdomain);
  117. #endif