1 /* Copyright (C) 1996-2000, 2001, 2002 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 #include "catgetsinfo.h"
32 /* Open the catalog and return a descriptor for the catalog. */
34 catopen (const char *cat_name
, int flag
)
37 const char *env_var
= NULL
;
38 const char *nlspath
= NULL
;
40 if (strchr (cat_name
, '/') == NULL
)
42 if (flag
== NL_CAT_LOCALE
)
43 /* Use the current locale setting for LC_MESSAGES. */
44 env_var
= setlocale (LC_MESSAGES
, NULL
);
46 /* Use the LANG environment variable. */
47 env_var
= getenv ("LANG");
49 if (env_var
== NULL
|| *env_var
== '\0'
50 || (__libc_enable_secure
&& strchr (env_var
, '/') != NULL
))
53 nlspath
= getenv ("NLSPATH");
54 if (nlspath
!= NULL
&& *nlspath
!= '\0')
56 /* Append the system dependent directory. */
57 size_t len
= strlen (nlspath
) + 1 + sizeof NLSPATH
;
58 char *tmp
= alloca (len
);
60 __stpcpy (__stpcpy (__stpcpy (tmp
, nlspath
), ":"), NLSPATH
);
67 result
= (__nl_catd
) malloc (sizeof (*result
));
69 /* We cannot get enough memory. */
72 if (__open_catalog (cat_name
, nlspath
, env_var
, result
) != 0)
74 /* Couldn't open the file. */
75 free ((void *) result
);
79 return (nl_catd
) result
;
83 /* Return message from message catalog. */
85 catgets (nl_catd catalog_desc
, int set
, int message
, const char *string
)
91 /* Be generous if catalog which failed to be open is used. */
92 if (catalog_desc
== (nl_catd
) -1 || ++set
<= 0 || message
< 0)
93 return (char *) string
;
95 catalog
= (__nl_catd
) catalog_desc
;
97 idx
= ((set
* message
) % catalog
->plane_size
) * 3;
101 if (catalog
->name_ptr
[idx
+ 0] == (u_int32_t
) set
102 && catalog
->name_ptr
[idx
+ 1] == (u_int32_t
) message
)
103 return (char *) &catalog
->strings
[catalog
->name_ptr
[idx
+ 2]];
105 idx
+= catalog
->plane_size
* 3;
107 while (++cnt
< catalog
->plane_depth
);
109 __set_errno (ENOMSG
);
110 return (char *) string
;
114 /* Return resources used for loaded message catalog. */
116 catclose (nl_catd catalog_desc
)
120 /* Be generous if catalog which failed to be open is used. */
121 if (catalog_desc
== (nl_catd
) -1)
127 catalog
= (__nl_catd
) catalog_desc
;
129 #ifdef _POSIX_MAPPED_FILES
130 if (catalog
->status
== mmapped
)
131 __munmap ((void *) catalog
->file_ptr
, catalog
->file_size
);
133 #endif /* _POSIX_MAPPED_FILES */
134 if (catalog
->status
== malloced
)
135 free ((void *) catalog
->file_ptr
);
142 free ((void *) catalog
);