You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

textdomain.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Implementation of the textdomain(3) function.
  2. Copyright (C) 1995-1998, 2000-2003 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 (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