x86-64: Skip zero length in __mem[pcpy|move|set]_erms
[glibc.git] / catgets / catgets.c
blobab203517b846895a26e59f40e40908f4fd485312
1 /* Copyright (C) 1996-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper, <drepper@gnu.org>.
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.
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.
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/>. */
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>
27 #include "catgetsinfo.h"
30 /* Open the catalog and return a descriptor for the catalog. */
31 nl_catd
32 catopen (const char *cat_name, int flag)
34 __nl_catd result;
35 const char *env_var = NULL;
36 const char *nlspath = NULL;
37 char *tmp = NULL;
39 if (strchr (cat_name, '/') == NULL)
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");
48 if (env_var == NULL || *env_var == '\0'
49 || (__libc_enable_secure && strchr (env_var, '/') != NULL))
50 env_var = "C";
52 nlspath = getenv ("NLSPATH");
53 if (nlspath != NULL && *nlspath != '\0')
55 /* Append the system dependent directory. */
56 size_t len = strlen (nlspath) + 1 + sizeof NLSPATH;
57 tmp = malloc (len);
59 if (__glibc_unlikely (tmp == NULL))
60 return (nl_catd) -1;
62 __stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH);
63 nlspath = tmp;
65 else
66 nlspath = NLSPATH;
69 result = (__nl_catd) malloc (sizeof (*result));
70 if (result == NULL)
72 /* We cannot get enough memory. */
73 result = (nl_catd) -1;
75 else if (__open_catalog (cat_name, nlspath, env_var, result) != 0)
77 /* Couldn't open the file. */
78 free ((void *) result);
79 result = (nl_catd) -1;
82 free (tmp);
83 return (nl_catd) result;
87 /* Return message from message catalog. */
88 char *
89 catgets (nl_catd catalog_desc, int set, int message, const char *string)
91 __nl_catd catalog;
92 size_t idx;
93 size_t cnt;
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;
99 catalog = (__nl_catd) catalog_desc;
101 idx = ((set * message) % catalog->plane_size) * 3;
102 cnt = 0;
105 if (catalog->name_ptr[idx + 0] == (uint32_t) set
106 && catalog->name_ptr[idx + 1] == (uint32_t) message)
107 return (char *) &catalog->strings[catalog->name_ptr[idx + 2]];
109 idx += catalog->plane_size * 3;
111 while (++cnt < catalog->plane_depth);
113 __set_errno (ENOMSG);
114 return (char *) string;
118 /* Return resources used for loaded message catalog. */
120 catclose (nl_catd catalog_desc)
122 __nl_catd catalog;
124 /* Be generous if catalog which failed to be open is used. */
125 if (catalog_desc == (nl_catd) -1)
127 __set_errno (EBADF);
128 return -1;
131 catalog = (__nl_catd) catalog_desc;
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
142 __set_errno (EBADF);
143 return -1;
146 free ((void *) catalog);
148 return 0;