1 /* Copyright (C) 1996-2015 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/>. */
27 #include "catgetsinfo.h"
30 /* Open the catalog and return a descriptor for the catalog. */
32 catopen (const char *cat_name
, int flag
)
35 const char *env_var
= NULL
;
36 const char *nlspath
= 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
);
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
))
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
;
59 if (__glibc_unlikely (tmp
== NULL
))
62 __stpcpy (__stpcpy (__stpcpy (tmp
, nlspath
), ":"), NLSPATH
);
69 result
= (__nl_catd
) malloc (sizeof (*result
));
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;
83 return (nl_catd
) result
;
87 /* Return message from message catalog. */
89 catgets (nl_catd catalog_desc
, int set
, int message
, const char *string
)
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;
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]];
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
)
124 /* Be generous if catalog which failed to be open is used. */
125 if (catalog_desc
== (nl_catd
) -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
);
137 #endif /* _POSIX_MAPPED_FILES */
138 if (catalog
->status
== malloced
)
139 free ((void *) catalog
->file_ptr
);
146 free ((void *) catalog
);