catgets.c 3.7 KB

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