textdomain.c 3.7 KB

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