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