textdomain.c 4.3 KB

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