catgets.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (C) 1996-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper, <drepper@gnu.org>.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <locale.h>
  17. #include <nl_types.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <sys/mman.h>
  22. #include "catgetsinfo.h"
  23. /* Open the catalog and return a descriptor for the catalog. */
  24. nl_catd
  25. catopen (const char *cat_name, int flag)
  26. {
  27. __nl_catd result;
  28. const char *env_var = NULL;
  29. const char *nlspath = NULL;
  30. char *tmp = NULL;
  31. if (strchr (cat_name, '/') == NULL)
  32. {
  33. if (flag == NL_CAT_LOCALE)
  34. /* Use the current locale setting for LC_MESSAGES. */
  35. env_var = setlocale (LC_MESSAGES, NULL);
  36. else
  37. /* Use the LANG environment variable. */
  38. env_var = getenv ("LANG");
  39. if (env_var == NULL || *env_var == '\0'
  40. || (__libc_enable_secure && strchr (env_var, '/') != NULL))
  41. env_var = "C";
  42. nlspath = getenv ("NLSPATH");
  43. if (nlspath != NULL && *nlspath != '\0')
  44. {
  45. /* Append the system dependent directory. */
  46. size_t len = strlen (nlspath) + 1 + sizeof NLSPATH;
  47. tmp = malloc (len);
  48. if (__glibc_unlikely (tmp == NULL))
  49. return (nl_catd) -1;
  50. __stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH);
  51. nlspath = tmp;
  52. }
  53. else
  54. nlspath = NLSPATH;
  55. }
  56. result = (__nl_catd) malloc (sizeof (*result));
  57. if (result == NULL)
  58. {
  59. /* We cannot get enough memory. */
  60. result = (nl_catd) -1;
  61. }
  62. else if (__open_catalog (cat_name, nlspath, env_var, result) != 0)
  63. {
  64. /* Couldn't open the file. */
  65. free ((void *) result);
  66. result = (nl_catd) -1;
  67. }
  68. free (tmp);
  69. return (nl_catd) result;
  70. }
  71. /* Return message from message catalog. */
  72. char *
  73. catgets (nl_catd catalog_desc, int set, int message, const char *string)
  74. {
  75. __nl_catd catalog;
  76. size_t idx;
  77. size_t cnt;
  78. /* Be generous if catalog which failed to be open is used. */
  79. if (catalog_desc == (nl_catd) -1 || ++set <= 0 || message < 0)
  80. return (char *) string;
  81. catalog = (__nl_catd) catalog_desc;
  82. idx = ((set * message) % catalog->plane_size) * 3;
  83. cnt = 0;
  84. do
  85. {
  86. if (catalog->name_ptr[idx + 0] == (uint32_t) set
  87. && catalog->name_ptr[idx + 1] == (uint32_t) message)
  88. return (char *) &catalog->strings[catalog->name_ptr[idx + 2]];
  89. idx += catalog->plane_size * 3;
  90. }
  91. while (++cnt < catalog->plane_depth);
  92. __set_errno (ENOMSG);
  93. return (char *) string;
  94. }
  95. /* Return resources used for loaded message catalog. */
  96. int
  97. catclose (nl_catd catalog_desc)
  98. {
  99. __nl_catd catalog;
  100. /* Be generous if catalog which failed to be open is used. */
  101. if (catalog_desc == (nl_catd) -1)
  102. {
  103. __set_errno (EBADF);
  104. return -1;
  105. }
  106. catalog = (__nl_catd) catalog_desc;
  107. #ifdef _POSIX_MAPPED_FILES
  108. if (catalog->status == mmapped)
  109. __munmap ((void *) catalog->file_ptr, catalog->file_size);
  110. else
  111. #endif /* _POSIX_MAPPED_FILES */
  112. if (catalog->status == malloced)
  113. free ((void *) catalog->file_ptr);
  114. else
  115. {
  116. __set_errno (EBADF);
  117. return -1;
  118. }
  119. free ((void *) catalog);
  120. return 0;
  121. }