1 /* Implementation of the dcgettext(3) function.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of the GNU C Library. Its master source is NOT part of
5 the C library, however.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include <sys/types.h>
28 #if defined __GNUC__ && !defined C_ALLOCA
29 # define alloca __builtin_alloca
30 # define HAVE_ALLOCA 1
32 # if (defined HAVE_ALLOCA_H || defined _LIBC) && !defined C_ALLOCA
50 # define __set_errno(val) errno = (val)
53 #if defined STDC_HEADERS || defined _LIBC
64 #if defined HAVE_STRING_H || defined _LIBC
66 # define _GNU_SOURCE 1
72 #if !HAVE_STRCHR && !defined _LIBC
78 #if defined HAVE_UNISTD_H || defined _LIBC
87 # include "libgettext.h"
89 #include "hash-string.h"
91 /* @@ end of prolog @@ */
94 /* Rename the non ANSI C functions. This is required by the standard
95 because some ANSI C functions will require linking with this object
96 file and the name space must not be polluted. */
97 # define getcwd __getcwd
99 # define stpcpy __stpcpy
102 # if !defined HAVE_GETCWD
104 # define getcwd(buf, max) getwd (buf)
109 static char *stpcpy
PARAMS ((char *dest
, const char *src
));
113 /* Amount to increase buffer size by in each try. */
116 /* The following is from pathmax.h. */
117 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
118 PATH_MAX but might cause redefinition warnings when sys/param.h is
119 later included (as on MORE/BSD 4.3). */
120 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
124 #ifndef _POSIX_PATH_MAX
125 # define _POSIX_PATH_MAX 255
128 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
129 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
132 /* Don't include sys/param.h if it already has been. */
133 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
134 # include <sys/param.h>
137 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
138 # define PATH_MAX MAXPATHLEN
142 # define PATH_MAX _POSIX_PATH_MAX
145 /* XPG3 defines the result of `setlocale (category, NULL)' as:
146 ``Directs `setlocale()' to query `category' and return the current
147 setting of `local'.''
148 However it does not specify the exact format. And even worse: POSIX
149 defines this not at all. So we can use this feature only on selected
150 system (e.g. those using GNU C Library). */
152 # define HAVE_LOCALE_NULL
155 /* Name of the default domain used for gettext(3) prior any call to
156 textdomain(3). The default value for this is "messages". */
157 const char _nl_default_default_domain
[] = "messages";
159 /* Value used as the default domain for gettext(3). */
160 const char *_nl_current_default_domain
= _nl_default_default_domain
;
162 /* Contains the default location of the message catalogs. */
163 const char _nl_default_dirname
[] = GNULOCALEDIR
;
165 /* List with bindings of specific domains created by bindtextdomain()
167 struct binding
*_nl_domain_bindings
;
169 /* Prototypes for local functions. */
170 static char *find_msg
PARAMS ((struct loaded_l10nfile
*domain_file
,
171 const char *msgid
)) internal_function
;
172 static const char *category_to_name
PARAMS ((int category
)) internal_function
;
173 static const char *guess_category_value
PARAMS ((int category
,
174 const char *categoryname
))
178 /* For those loosing systems which don't have `alloca' we have to add
179 some additional code emulating it. */
181 /* Nothing has to be done. */
182 # define ADD_BLOCK(list, address) /* nothing */
183 # define FREE_BLOCKS(list) /* nothing */
188 struct block_list
*next
;
190 # define ADD_BLOCK(list, addr) \
192 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
193 /* If we cannot get a free block we cannot add the new element to \
195 if (newp != NULL) { \
196 newp->address = (addr); \
197 newp->next = (list); \
201 # define FREE_BLOCKS(list) \
203 while (list != NULL) { \
204 struct block_list *old = list; \
210 # define alloca(size) (malloc (size))
211 #endif /* have alloca */
214 /* Names for the libintl functions are a problem. They must not clash
215 with existing names and they should follow ANSI C. But this source
216 code is also used in GNU C Library where the names have a __
217 prefix. So we have to make a difference here. */
219 # define DCGETTEXT __dcgettext
221 # define DCGETTEXT dcgettext__
224 /* Checking whether the binaries runs SUID must be done and glibc provides
225 easier methods therefore we make a difference here. */
227 # define ENABLE_SECURE __libc_enable_secure
228 # define DETERMINE_SECURE
230 static int enable_secure
;
231 # define ENABLE_SECURE (enable_secure == 1)
232 # define DETERMINE_SECURE \
233 if (enable_secure == 0) \
235 if (getuid () != geteuid () || getgid () != getegid ()) \
238 enable_secure = -1; \
242 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
245 DCGETTEXT (domainname
, msgid
, category
)
246 const char *domainname
;
251 struct block_list
*block_list
= NULL
;
253 struct loaded_l10nfile
*domain
;
254 struct binding
*binding
;
255 const char *categoryname
;
256 const char *categoryvalue
;
257 char *dirname
, *xdomainname
;
260 int saved_errno
= errno
;
262 /* If no real MSGID is given return NULL. */
266 /* See whether this is a SUID binary or not. */
269 /* If DOMAINNAME is NULL, we are interested in the default domain. If
270 CATEGORY is not LC_MESSAGES this might not make much sense but the
271 definition left this undefined. */
272 if (domainname
== NULL
)
273 domainname
= _nl_current_default_domain
;
275 /* First find matching binding. */
276 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
278 int compare
= strcmp (domainname
, binding
->domainname
);
284 /* It is not in the list. */
291 dirname
= (char *) _nl_default_dirname
;
292 else if (binding
->dirname
[0] == '/')
293 dirname
= binding
->dirname
;
296 /* We have a relative path. Make it absolute now. */
297 size_t dirname_len
= strlen (binding
->dirname
) + 1;
301 path_max
= (unsigned int) PATH_MAX
;
302 path_max
+= 2; /* The getcwd docs say to do this. */
304 dirname
= (char *) alloca (path_max
+ dirname_len
);
305 ADD_BLOCK (block_list
, dirname
);
308 while ((ret
= getcwd (dirname
, path_max
)) == NULL
&& errno
== ERANGE
)
310 path_max
+= PATH_INCR
;
311 dirname
= (char *) alloca (path_max
+ dirname_len
);
312 ADD_BLOCK (block_list
, dirname
);
318 /* We cannot get the current working directory. Don't signal an
319 error but simply return the default string. */
320 FREE_BLOCKS (block_list
);
321 __set_errno (saved_errno
);
322 return (char *) msgid
;
325 stpcpy (stpcpy (strchr (dirname
, '\0'), "/"), binding
->dirname
);
328 /* Now determine the symbolic name of CATEGORY and its value. */
329 categoryname
= category_to_name (category
);
330 categoryvalue
= guess_category_value (category
, categoryname
);
332 xdomainname
= (char *) alloca (strlen (categoryname
)
333 + strlen (domainname
) + 5);
334 ADD_BLOCK (block_list
, xdomainname
);
336 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname
, categoryname
), "/"),
340 /* Creating working area. */
341 single_locale
= (char *) alloca (strlen (categoryvalue
) + 1);
342 ADD_BLOCK (block_list
, single_locale
);
345 /* Search for the given string. This is a loop because we perhaps
346 got an ordered list of languages to consider for the translation. */
349 /* Make CATEGORYVALUE point to the next element of the list. */
350 while (categoryvalue
[0] != '\0' && categoryvalue
[0] == ':')
352 if (categoryvalue
[0] == '\0')
354 /* The whole contents of CATEGORYVALUE has been searched but
355 no valid entry has been found. We solve this situation
356 by implicitly appending a "C" entry, i.e. no translation
358 single_locale
[0] = 'C';
359 single_locale
[1] = '\0';
363 char *cp
= single_locale
;
364 while (categoryvalue
[0] != '\0' && categoryvalue
[0] != ':')
365 *cp
++ = *categoryvalue
++;
368 /* When this is a SUID binary we must not allow accessing files
369 outside the dedicated directories. */
371 && (memchr (single_locale
, '/',
372 _nl_find_language (single_locale
) - single_locale
)
374 /* Ingore this entry. */
378 /* If the current locale value is C (or POSIX) we don't load a
379 domain. Return the MSGID. */
380 if (strcmp (single_locale
, "C") == 0
381 || strcmp (single_locale
, "POSIX") == 0)
383 FREE_BLOCKS (block_list
);
384 __set_errno (saved_errno
);
385 return (char *) msgid
;
389 /* Find structure describing the message catalog matching the
390 DOMAINNAME and CATEGORY. */
391 domain
= _nl_find_domain (dirname
, single_locale
, xdomainname
);
395 retval
= find_msg (domain
, msgid
);
401 for (cnt
= 0; domain
->successor
[cnt
] != NULL
; ++cnt
)
403 retval
= find_msg (domain
->successor
[cnt
], msgid
);
412 FREE_BLOCKS (block_list
);
413 __set_errno (saved_errno
);
422 /* Alias for function name in GNU C Library. */
423 weak_alias (__dcgettext
, dcgettext
);
429 find_msg (domain_file
, msgid
)
430 struct loaded_l10nfile
*domain_file
;
435 struct loaded_domain
*domain
;
437 if (domain_file
->decided
== 0)
438 _nl_load_domain (domain_file
);
440 if (domain_file
->data
== NULL
)
443 domain
= (struct loaded_domain
*) domain_file
->data
;
445 /* Locate the MSGID and its translation. */
446 if (domain
->hash_size
> 2 && domain
->hash_tab
!= NULL
)
448 /* Use the hashing table. */
449 nls_uint32 len
= strlen (msgid
);
450 nls_uint32 hash_val
= hash_string (msgid
);
451 nls_uint32 idx
= hash_val
% domain
->hash_size
;
452 nls_uint32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
453 nls_uint32 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
456 /* Hash table entry is empty. */
459 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
461 domain
->data
+ W (domain
->must_swap
,
462 domain
->orig_tab
[nstr
- 1].offset
)) == 0)
463 return (char *) domain
->data
+ W (domain
->must_swap
,
464 domain
->trans_tab
[nstr
- 1].offset
);
468 if (idx
>= domain
->hash_size
- incr
)
469 idx
-= domain
->hash_size
- incr
;
473 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
475 /* Hash table entry is empty. */
478 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
480 domain
->data
+ W (domain
->must_swap
,
481 domain
->orig_tab
[nstr
- 1].offset
))
483 return (char *) domain
->data
484 + W (domain
->must_swap
, domain
->trans_tab
[nstr
- 1].offset
);
489 /* Now we try the default method: binary search in the sorted
490 array of messages. */
492 top
= domain
->nstrings
;
497 act
= (bottom
+ top
) / 2;
498 cmp_val
= strcmp (msgid
, domain
->data
499 + W (domain
->must_swap
,
500 domain
->orig_tab
[act
].offset
));
503 else if (cmp_val
> 0)
509 /* If an translation is found return this. */
510 return bottom
>= top
? NULL
: (char *) domain
->data
511 + W (domain
->must_swap
,
512 domain
->trans_tab
[act
].offset
);
516 /* Return string representation of locale CATEGORY. */
519 category_to_name (category
)
528 retval
= "LC_COLLATE";
538 retval
= "LC_MONETARY";
543 retval
= "LC_NUMERIC";
553 retval
= "LC_MESSAGES";
558 retval
= "LC_RESPONSE";
563 /* This might not make sense but is perhaps better than any other
569 /* If you have a better idea for a default value let me know. */
576 /* Guess value of current locale from value of the environment variables. */
579 guess_category_value (category
, categoryname
)
581 const char *categoryname
;
585 /* The highest priority value is the `LANGUAGE' environment
586 variable. This is a GNU extension. */
587 retval
= getenv ("LANGUAGE");
588 if (retval
!= NULL
&& retval
[0] != '\0')
591 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
592 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
593 systems this can be done by the `setlocale' function itself. */
594 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
595 return setlocale (category
, NULL
);
597 /* Setting of LC_ALL overwrites all other. */
598 retval
= getenv ("LC_ALL");
599 if (retval
!= NULL
&& retval
[0] != '\0')
602 /* Next comes the name of the desired category. */
603 retval
= getenv (categoryname
);
604 if (retval
!= NULL
&& retval
[0] != '\0')
607 /* Last possibility is the LANG environment variable. */
608 retval
= getenv ("LANG");
609 if (retval
!= NULL
&& retval
[0] != '\0')
612 /* We use C as the default domain. POSIX says this is implementation
618 /* @@ begin of epilog @@ */
620 /* We don't want libintl.a to depend on any other library. So we
621 avoid the non-standard function stpcpy. In GNU C Library this
622 function is available, though. Also allow the symbol HAVE_STPCPY
624 #if !_LIBC && !HAVE_STPCPY
630 while ((*dest
++ = *src
++) != '\0')
638 /* If we want to free all resources we have to do some work at
640 static void __attribute__ ((unused
))
643 struct binding
*runp
;
645 for (runp
= _nl_domain_bindings
; runp
!= NULL
; runp
= runp
->next
)
647 free (runp
->domainname
);
648 if (runp
->dirname
!= _nl_default_dirname
)
649 /* Yes, this is a pointer comparison. */
650 free (runp
->dirname
);
653 if (_nl_current_default_domain
!= _nl_default_default_domain
)
654 /* Yes, again a pointer comparison. */
655 free ((char *) _nl_current_default_domain
);
658 text_set_element (__libc_subfreeres
, free_mem
);