1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper, <drepper@gnu.ai.mit.edu>.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
28 #include "catgetsinfo.h"
31 /* Open the catalog and return a descriptor for the catalog. */
33 catopen (const char *cat_name
, int flag
)
39 result
= (__nl_catd
) malloc (sizeof (*result
));
41 /* We cannot get enough memory. */
44 result
->status
= closed
;
46 result
->cat_name
= __strdup (cat_name
);
47 if (result
->cat_name
== NULL
)
54 if (strchr (cat_name
, '/') == NULL
)
56 if (flag
== NL_CAT_LOCALE
)
58 env_var
= getenv ("LC_ALL");
61 env_var
= getenv ("LC_MESSAGES");
64 env_var
= getenv ("LANG");
72 env_var
= getenv ("LANG");
77 result
->env_var
= __strdup (env_var
);
78 if (result
->env_var
== NULL
)
80 free ((void *) result
->cat_name
);
81 free ((void *) result
);
86 nlspath
= __secure_getenv ("NLSPATH");
87 if (nlspath
!= NULL
&& *nlspath
!= '\0')
89 /* Append the system dependent directory. */
90 size_t len
= strlen (nlspath
) + 1 + sizeof NLSPATH
;
91 char *tmp
= alloca (len
);
93 __stpcpy (__stpcpy (__stpcpy (tmp
, nlspath
), ":"), NLSPATH
);
99 result
->nlspath
= __strdup (nlspath
);
100 if (result
->nlspath
== NULL
)
102 free ((void *) result
->cat_name
);
103 free ((void *) result
->env_var
);
104 free ((void *) result
);
105 __set_errno (ENOMEM
);
111 result
->env_var
= NULL
;
112 result
->nlspath
= NULL
;
115 return (nl_catd
) result
;
119 /* Return message from message catalog. */
121 catgets (nl_catd catalog_desc
, int set
, int message
, const char *string
)
127 /* Be generous if catalog which failed to be open is used. */
128 if (catalog_desc
== (nl_catd
) -1 || ++set
<= 0 || message
< 0)
129 return (char *) string
;
131 catalog
= (__nl_catd
) catalog_desc
;
133 if (catalog
->status
== closed
)
134 __open_catalog (catalog
, 1);
136 if (catalog
->status
== nonexisting
)
139 return (char *) string
;
142 idx
= ((set
* message
) % catalog
->plane_size
) * 3;
146 if (catalog
->name_ptr
[idx
+ 0] == (u_int32_t
) set
147 && catalog
->name_ptr
[idx
+ 1] == (u_int32_t
) message
)
148 return (char *) &catalog
->strings
[catalog
->name_ptr
[idx
+ 2]];
150 idx
+= catalog
->plane_size
* 3;
152 while (++cnt
< catalog
->plane_depth
);
154 __set_errno (ENOMSG
);
155 return (char *) string
;
159 /* Return resources used for loaded message catalog. */
161 catclose (nl_catd catalog_desc
)
165 catalog
= (__nl_catd
) catalog_desc
;
167 if (catalog
->status
== mmapped
)
168 munmap ((void *) catalog
->file_ptr
, catalog
->file_size
);
169 else if (catalog
->status
== malloced
)
170 free ((void *) catalog
->file_ptr
);
171 else if (catalog
->status
!= closed
&& catalog
->status
!= nonexisting
)
177 if (catalog
->nlspath
)
178 free ((void *) catalog
->nlspath
);
179 if (catalog
->env_var
)
180 free ((void *) catalog
->env_var
);
181 free ((void *) catalog
);