3 /* Implementation of the internal dcigettext function.
4 Copyright (C) 1995-1999, 2000-2002 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published
8 by the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 /* Tell glibc's <string.h> to provide a prototype for mempcpy().
22 This must come before <config.h> because <config.h> may include
23 <features.h>, and once <features.h> has been included, it's too late. */
25 # define _GNU_SOURCE 1
32 #include <sys/types.h>
35 # define alloca __builtin_alloca
36 # define HAVE_ALLOCA 1
38 # if defined HAVE_ALLOCA_H || defined _LIBC
56 # define __set_errno(val) errno = (val)
63 #if defined HAVE_UNISTD_H || defined _LIBC
69 #if defined HAVE_SYS_PARAM_H || defined _LIBC
70 # include <sys/param.h>
74 #include "plural-exp.h"
78 # include "libgnuintl.h"
80 #include "hash-string.h"
82 /* Thread safetyness. */
84 # include <bits/libc-lock.h>
86 /* Provide dummy implementation if this is outside glibc. */
87 # define __libc_lock_define_initialized(CLASS, NAME)
88 # define __libc_lock_lock(NAME)
89 # define __libc_lock_unlock(NAME)
90 # define __libc_rwlock_define_initialized(CLASS, NAME)
91 # define __libc_rwlock_rdlock(NAME)
92 # define __libc_rwlock_unlock(NAME)
95 /* Alignment of types. */
96 #if defined __GNUC__ && __GNUC__ >= 2
97 # define alignof(TYPE) __alignof__ (TYPE)
99 # define alignof(TYPE) \
100 ((int) &((struct { char dummy1; TYPE dummy2; } *) 0)->dummy2)
103 /* The internal variables in the standalone libintl.a must have different
104 names than the internal variables in GNU libc, otherwise programs
105 using libintl.a cannot be linked statically. */
107 # define _nl_default_default_domain _nl_default_default_domain__
108 # define _nl_current_default_domain _nl_current_default_domain__
109 # define _nl_default_dirname _nl_default_dirname__
110 # define _nl_domain_bindings _nl_domain_bindings__
113 /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>. */
115 # define offsetof(type,ident) ((size_t)&(((type*)0)->ident))
118 /* @@ end of prolog @@ */
121 /* Rename the non ANSI C functions. This is required by the standard
122 because some ANSI C functions will require linking with this object
123 file and the name space must not be polluted. */
124 # define getcwd __getcwd
126 # define stpcpy __stpcpy
128 # define tfind __tfind
130 # if !defined HAVE_GETCWD
132 # define getcwd(buf, max) getwd (buf)
137 static char *stpcpy
PARAMS ((char *dest
, const char *src
));
139 # ifndef HAVE_MEMPCPY
140 static void *mempcpy
PARAMS ((void *dest
, const void *src
, size_t n
));
144 /* Amount to increase buffer size by in each try. */
147 /* The following is from pathmax.h. */
148 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
149 PATH_MAX but might cause redefinition warnings when sys/param.h is
150 later included (as on MORE/BSD 4.3). */
151 #if defined _POSIX_VERSION || (defined HAVE_LIMITS_H && !defined __GNUC__)
155 #ifndef _POSIX_PATH_MAX
156 # define _POSIX_PATH_MAX 255
159 #if !defined PATH_MAX && defined _PC_PATH_MAX
160 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
163 /* Don't include sys/param.h if it already has been. */
164 #if defined HAVE_SYS_PARAM_H && !defined PATH_MAX && !defined MAXPATHLEN
165 # include <sys/param.h>
168 #if !defined PATH_MAX && defined MAXPATHLEN
169 # define PATH_MAX MAXPATHLEN
173 # define PATH_MAX _POSIX_PATH_MAX
177 ISSLASH(C) tests whether C is a directory separator character.
178 IS_ABSOLUTE_PATH(P) tests whether P is an absolute path. If it is not,
179 it may be concatenated to a directory pathname.
180 IS_PATH_WITH_DIR(P) tests whether P contains a directory specification.
182 #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
183 /* Win32, OS/2, DOS */
184 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
185 # define HAS_DEVICE(P) \
186 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
188 # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P))
189 # define IS_PATH_WITH_DIR(P) \
190 (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
193 # define ISSLASH(C) ((C) == '/')
194 # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
195 # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
198 /* This is the type used for the search tree where known translations
200 struct known_translation_t
202 /* Domain in which to search. */
208 /* State of the catalog counter at the point the string was found. */
211 /* Catalog where the string was found. */
212 struct loaded_l10nfile
*domain
;
214 /* And finally the translation. */
215 const char *translation
;
216 size_t translation_length
;
218 /* Pointer to the string in question. */
222 /* Root of the search tree with known translations. We can use this
223 only if the system provides the `tsearch' function family. */
224 #if defined HAVE_TSEARCH || defined _LIBC
230 # define tsearch __tsearch
233 /* Function to compare two entries in the table of known translations. */
234 static int transcmp
PARAMS ((const void *p1
, const void *p2
));
240 const struct known_translation_t
*s1
;
241 const struct known_translation_t
*s2
;
244 s1
= (const struct known_translation_t
*) p1
;
245 s2
= (const struct known_translation_t
*) p2
;
247 result
= strcmp (s1
->msgid
, s2
->msgid
);
250 result
= strcmp (s1
->domainname
, s2
->domainname
);
252 /* We compare the category last (though this is the cheapest
253 operation) since it is hopefully always the same (namely
255 result
= s1
->category
- s2
->category
;
262 /* Name of the default domain used for gettext(3) prior any call to
263 textdomain(3). The default value for this is "messages". */
264 const char _nl_default_default_domain
[] = "messages";
266 /* Value used as the default domain for gettext(3). */
267 const char *_nl_current_default_domain
= _nl_default_default_domain
;
269 /* Contains the default location of the message catalogs. */
271 extern const char _nl_default_dirname
[];
273 const char _nl_default_dirname
[] = LOCALEDIR
;
276 /* List with bindings of specific domains created by bindtextdomain()
278 struct binding
*_nl_domain_bindings
;
280 /* Prototypes for local functions. */
281 static char *plural_lookup
PARAMS ((struct loaded_l10nfile
*domain
,
283 const char *translation
,
284 size_t translation_len
))
286 static const char *category_to_name
PARAMS ((int category
)) internal_function
;
287 static const char *guess_category_value
PARAMS ((int category
,
288 const char *categoryname
))
292 /* For those loosing systems which don't have `alloca' we have to add
293 some additional code emulating it. */
295 /* Nothing has to be done. */
296 # define ADD_BLOCK(list, address) /* nothing */
297 # define FREE_BLOCKS(list) /* nothing */
302 struct block_list
*next
;
304 # define ADD_BLOCK(list, addr) \
306 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
307 /* If we cannot get a free block we cannot add the new element to \
309 if (newp != NULL) { \
310 newp->address = (addr); \
311 newp->next = (list); \
315 # define FREE_BLOCKS(list) \
317 while (list != NULL) { \
318 struct block_list *old = list; \
324 # define alloca(size) (malloc (size))
325 #endif /* have alloca */
329 /* List of blocks allocated for translations. */
330 typedef struct transmem_list
332 struct transmem_list
*next
;
335 static struct transmem_list
*transmem_list
;
337 typedef unsigned char transmem_block_t
;
341 /* Names for the libintl functions are a problem. They must not clash
342 with existing names and they should follow ANSI C. But this source
343 code is also used in GNU C Library where the names have a __
344 prefix. So we have to make a difference here. */
346 # define DCIGETTEXT __dcigettext
348 # define DCIGETTEXT dcigettext__
351 /* Lock variable to protect the global data in the gettext implementation. */
353 __libc_rwlock_define_initialized (, _nl_state_lock
)
356 /* Checking whether the binaries runs SUID must be done and glibc provides
357 easier methods therefore we make a difference here. */
359 # define ENABLE_SECURE __libc_enable_secure
360 # define DETERMINE_SECURE
368 # ifndef HAVE_GETEUID
369 # define geteuid() getuid()
371 # ifndef HAVE_GETEGID
372 # define getegid() getgid()
374 static int enable_secure
;
375 # define ENABLE_SECURE (enable_secure == 1)
376 # define DETERMINE_SECURE \
377 if (enable_secure == 0) \
379 if (getuid () != geteuid () || getgid () != getegid ()) \
382 enable_secure = -1; \
386 /* Get the function to evaluate the plural expression. */
387 #include "eval-plural.h"
389 /* Look up MSGID in the DOMAINNAME message catalog for the current
390 CATEGORY locale and, if PLURAL is nonzero, search over string
391 depending on the plural form determined by N. */
393 DCIGETTEXT (domainname
, msgid1
, msgid2
, plural
, n
, category
)
394 const char *domainname
;
402 struct block_list
*block_list
= NULL
;
404 struct loaded_l10nfile
*domain
;
405 struct binding
*binding
;
406 const char *categoryname
;
407 const char *categoryvalue
;
408 char *dirname
, *xdomainname
;
413 #if defined HAVE_TSEARCH || defined _LIBC
414 struct known_translation_t
*search
;
415 struct known_translation_t
**foundp
= NULL
;
418 size_t domainname_len
;
420 /* If no real MSGID is given return NULL. */
424 __libc_rwlock_rdlock (_nl_state_lock
);
426 /* If DOMAINNAME is NULL, we are interested in the default domain. If
427 CATEGORY is not LC_MESSAGES this might not make much sense but the
428 definition left this undefined. */
429 if (domainname
== NULL
)
430 domainname
= _nl_current_default_domain
;
432 /* OS/2 specific: backward compatibility with older libintl versions */
433 #ifdef LC_MESSAGES_COMPAT
434 if (category
== LC_MESSAGES_COMPAT
)
435 category
= LC_MESSAGES
;
438 #if defined HAVE_TSEARCH || defined _LIBC
439 msgid_len
= strlen (msgid1
) + 1;
441 /* Try to find the translation among those which we found at
443 search
= (struct known_translation_t
*)
444 alloca (offsetof (struct known_translation_t
, msgid
) + msgid_len
);
445 memcpy (search
->msgid
, msgid1
, msgid_len
);
446 search
->domainname
= (char *) domainname
;
447 search
->category
= category
;
449 foundp
= (struct known_translation_t
**) tfind (search
, &root
, transcmp
);
450 if (foundp
!= NULL
&& (*foundp
)->counter
== _nl_msg_cat_cntr
)
452 /* Now deal with plural. */
454 retval
= plural_lookup ((*foundp
)->domain
, n
, (*foundp
)->translation
,
455 (*foundp
)->translation_length
);
457 retval
= (char *) (*foundp
)->translation
;
459 __libc_rwlock_unlock (_nl_state_lock
);
464 /* Preserve the `errno' value. */
467 /* See whether this is a SUID binary or not. */
470 /* First find matching binding. */
471 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
473 int compare
= strcmp (domainname
, binding
->domainname
);
479 /* It is not in the list. */
486 dirname
= (char *) _nl_default_dirname
;
487 else if (IS_ABSOLUTE_PATH (binding
->dirname
))
488 dirname
= binding
->dirname
;
491 /* We have a relative path. Make it absolute now. */
492 size_t dirname_len
= strlen (binding
->dirname
) + 1;
496 path_max
= (unsigned int) PATH_MAX
;
497 path_max
+= 2; /* The getcwd docs say to do this. */
501 dirname
= (char *) alloca (path_max
+ dirname_len
);
502 ADD_BLOCK (block_list
, dirname
);
505 ret
= getcwd (dirname
, path_max
);
506 if (ret
!= NULL
|| errno
!= ERANGE
)
509 path_max
+= path_max
/ 2;
510 path_max
+= PATH_INCR
;
515 /* We cannot get the current working directory. Don't signal an
516 error but simply return the default string. */
517 FREE_BLOCKS (block_list
);
518 __libc_rwlock_unlock (_nl_state_lock
);
519 __set_errno (saved_errno
);
522 /* Use the Germanic plural rule. */
523 : n
== 1 ? (char *) msgid1
: (char *) msgid2
);
526 stpcpy (stpcpy (strchr (dirname
, '\0'), "/"), binding
->dirname
);
529 /* Now determine the symbolic name of CATEGORY and its value. */
530 categoryname
= category_to_name (category
);
531 categoryvalue
= guess_category_value (category
, categoryname
);
533 domainname_len
= strlen (domainname
);
534 xdomainname
= (char *) alloca (strlen (categoryname
)
535 + domainname_len
+ 5);
536 ADD_BLOCK (block_list
, xdomainname
);
538 stpcpy (mempcpy (stpcpy (stpcpy (xdomainname
, categoryname
), "/"),
539 domainname
, domainname_len
),
542 /* Creating working area. */
543 single_locale
= (char *) alloca (strlen (categoryvalue
) + 1);
544 ADD_BLOCK (block_list
, single_locale
);
547 /* Search for the given string. This is a loop because we perhaps
548 got an ordered list of languages to consider for the translation. */
551 /* Make CATEGORYVALUE point to the next element of the list. */
552 while (categoryvalue
[0] != '\0' && categoryvalue
[0] == ':')
554 if (categoryvalue
[0] == '\0')
556 /* The whole contents of CATEGORYVALUE has been searched but
557 no valid entry has been found. We solve this situation
558 by implicitly appending a "C" entry, i.e. no translation
560 single_locale
[0] = 'C';
561 single_locale
[1] = '\0';
565 char *cp
= single_locale
;
566 while (categoryvalue
[0] != '\0' && categoryvalue
[0] != ':')
567 *cp
++ = *categoryvalue
++;
570 /* When this is a SUID binary we must not allow accessing files
571 outside the dedicated directories. */
572 if (ENABLE_SECURE
&& IS_PATH_WITH_DIR (single_locale
))
573 /* Ingore this entry. */
577 /* If the current locale value is C (or POSIX) we don't load a
578 domain. Return the MSGID. */
579 if (strcmp (single_locale
, "C") == 0
580 || strcmp (single_locale
, "POSIX") == 0)
582 FREE_BLOCKS (block_list
);
583 __libc_rwlock_unlock (_nl_state_lock
);
584 __set_errno (saved_errno
);
587 /* Use the Germanic plural rule. */
588 : n
== 1 ? (char *) msgid1
: (char *) msgid2
);
592 /* Find structure describing the message catalog matching the
593 DOMAINNAME and CATEGORY. */
594 domain
= _nl_find_domain (dirname
, single_locale
, xdomainname
, binding
);
598 retval
= _nl_find_msg (domain
, binding
, msgid1
, &retlen
);
604 for (cnt
= 0; domain
->successor
[cnt
] != NULL
; ++cnt
)
606 retval
= _nl_find_msg (domain
->successor
[cnt
], binding
,
611 domain
= domain
->successor
[cnt
];
619 /* Found the translation of MSGID1 in domain DOMAIN:
620 starting at RETVAL, RETLEN bytes. */
621 FREE_BLOCKS (block_list
);
622 __set_errno (saved_errno
);
623 #if defined HAVE_TSEARCH || defined _LIBC
626 /* Create a new entry and add it to the search tree. */
627 struct known_translation_t
*newp
;
629 newp
= (struct known_translation_t
*)
630 malloc (offsetof (struct known_translation_t
, msgid
)
631 + msgid_len
+ domainname_len
+ 1);
635 mempcpy (newp
->msgid
, msgid1
, msgid_len
);
636 memcpy (newp
->domainname
, domainname
, domainname_len
+ 1);
637 newp
->category
= category
;
638 newp
->counter
= _nl_msg_cat_cntr
;
639 newp
->domain
= domain
;
640 newp
->translation
= retval
;
641 newp
->translation_length
= retlen
;
643 /* Insert the entry in the search tree. */
644 foundp
= (struct known_translation_t
**)
645 tsearch (newp
, &root
, transcmp
);
647 || __builtin_expect (*foundp
!= newp
, 0))
648 /* The insert failed. */
654 /* We can update the existing entry. */
655 (*foundp
)->counter
= _nl_msg_cat_cntr
;
656 (*foundp
)->domain
= domain
;
657 (*foundp
)->translation
= retval
;
658 (*foundp
)->translation_length
= retlen
;
661 /* Now deal with plural. */
663 retval
= plural_lookup (domain
, n
, retval
, retlen
);
665 __libc_rwlock_unlock (_nl_state_lock
);
676 _nl_find_msg (domain_file
, domainbinding
, msgid
, lengthp
)
677 struct loaded_l10nfile
*domain_file
;
678 struct binding
*domainbinding
;
682 struct loaded_domain
*domain
;
687 if (domain_file
->decided
== 0)
688 _nl_load_domain (domain_file
, domainbinding
);
690 if (domain_file
->data
== NULL
)
693 domain
= (struct loaded_domain
*) domain_file
->data
;
695 /* Locate the MSGID and its translation. */
696 if (domain
->hash_size
> 2 && domain
->hash_tab
!= NULL
)
698 /* Use the hashing table. */
699 nls_uint32 len
= strlen (msgid
);
700 nls_uint32 hash_val
= hash_string (msgid
);
701 nls_uint32 idx
= hash_val
% domain
->hash_size
;
702 nls_uint32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
706 nls_uint32 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
709 /* Hash table entry is empty. */
712 /* Compare msgid with the original string at index nstr-1.
713 We compare the lengths with >=, not ==, because plural entries
714 are represented by strings with an embedded NUL. */
715 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) >= len
717 domain
->data
+ W (domain
->must_swap
,
718 domain
->orig_tab
[nstr
- 1].offset
))
725 if (idx
>= domain
->hash_size
- incr
)
726 idx
-= domain
->hash_size
- incr
;
734 /* Try the default method: binary search in the sorted array of
739 top
= domain
->nstrings
;
744 act
= (bottom
+ top
) / 2;
745 cmp_val
= strcmp (msgid
, (domain
->data
746 + W (domain
->must_swap
,
747 domain
->orig_tab
[act
].offset
)));
750 else if (cmp_val
> 0)
755 /* No translation was found. */
760 /* The translation was found at index ACT. If we have to convert the
761 string to use a different character set, this is the time. */
762 result
= ((char *) domain
->data
763 + W (domain
->must_swap
, domain
->trans_tab
[act
].offset
));
764 resultlen
= W (domain
->must_swap
, domain
->trans_tab
[act
].length
) + 1;
766 #if defined _LIBC || HAVE_ICONV
767 if (domain
->codeset_cntr
768 != (domainbinding
!= NULL
? domainbinding
->codeset_cntr
: 0))
770 /* The domain's codeset has changed through bind_textdomain_codeset()
771 since the message catalog was initialized or last accessed. We
772 have to reinitialize the converter. */
773 _nl_free_domain_conv (domain
);
774 _nl_init_domain_conv (domain_file
, domain
, domainbinding
);
779 domain
->conv
!= (__gconv_t
) -1
782 domain
->conv
!= (iconv_t
) -1
787 /* We are supposed to do a conversion. First allocate an
788 appropriate table with the same structure as the table
789 of translations in the file, where we can put the pointers
790 to the converted strings in.
791 There is a slight complication with plural entries. They
792 are represented by consecutive NUL terminated strings. We
793 handle this case by converting RESULTLEN bytes, including
796 if (domain
->conv_tab
== NULL
797 && ((domain
->conv_tab
= (char **) calloc (domain
->nstrings
,
800 /* Mark that we didn't succeed allocating a table. */
801 domain
->conv_tab
= (char **) -1;
803 if (__builtin_expect (domain
->conv_tab
== (char **) -1, 0))
804 /* Nothing we can do, no more memory. */
807 if (domain
->conv_tab
[act
] == NULL
)
809 /* We haven't used this string so far, so it is not
810 translated yet. Do this now. */
811 /* We use a bit more efficient memory handling.
812 We allocate always larger blocks which get used over
813 time. This is faster than many small allocations. */
814 __libc_lock_define_initialized (static, lock
)
815 # define INITIAL_BLOCK_SIZE 4080
816 static unsigned char *freemem
;
817 static size_t freemem_size
;
819 const unsigned char *inbuf
;
820 unsigned char *outbuf
;
823 transmem_block_t
*transmem_list
= NULL
;
826 __libc_lock_lock (lock
);
828 inbuf
= (const unsigned char *) result
;
829 outbuf
= freemem
+ sizeof (size_t);
834 transmem_block_t
*newmem
;
836 size_t non_reversible
;
839 if (freemem_size
< sizeof (size_t))
842 res
= __gconv (domain
->conv
,
843 &inbuf
, inbuf
+ resultlen
,
845 outbuf
+ freemem_size
- sizeof (size_t),
848 if (res
== __GCONV_OK
|| res
== __GCONV_EMPTY_INPUT
)
851 if (res
!= __GCONV_FULL_OUTPUT
)
853 __libc_lock_unlock (lock
);
860 const char *inptr
= (const char *) inbuf
;
861 size_t inleft
= resultlen
;
862 char *outptr
= (char *) outbuf
;
865 if (freemem_size
< sizeof (size_t))
868 outleft
= freemem_size
- sizeof (size_t);
869 if (iconv (domain
->conv
,
870 (ICONV_CONST
char **) &inptr
, &inleft
,
874 outbuf
= (unsigned char *) outptr
;
879 __libc_lock_unlock (lock
);
886 /* We must allocate a new buffer or resize the old one. */
887 if (malloc_count
> 0)
890 freemem_size
= malloc_count
* INITIAL_BLOCK_SIZE
;
891 newmem
= (transmem_block_t
*) realloc (transmem_list
,
895 transmem_list
= transmem_list
->next
;
898 struct transmem_list
*old
= transmem_list
;
900 transmem_list
= transmem_list
->next
;
908 freemem_size
= INITIAL_BLOCK_SIZE
;
909 newmem
= (transmem_block_t
*) malloc (freemem_size
);
911 if (__builtin_expect (newmem
== NULL
, 0))
915 __libc_lock_unlock (lock
);
920 /* Add the block to the list of blocks we have to free
922 newmem
->next
= transmem_list
;
923 transmem_list
= newmem
;
925 freemem
= newmem
->data
;
926 freemem_size
-= offsetof (struct transmem_list
, data
);
928 transmem_list
= newmem
;
932 outbuf
= freemem
+ sizeof (size_t);
935 /* We have now in our buffer a converted string. Put this
936 into the table of conversions. */
937 *(size_t *) freemem
= outbuf
- freemem
- sizeof (size_t);
938 domain
->conv_tab
[act
] = (char *) freemem
;
939 /* Shrink freemem, but keep it aligned. */
940 freemem_size
-= outbuf
- freemem
;
942 freemem
+= freemem_size
& (alignof (size_t) - 1);
943 freemem_size
= freemem_size
& ~ (alignof (size_t) - 1);
945 __libc_lock_unlock (lock
);
948 /* Now domain->conv_tab[act] contains the translation of all
949 the plural variants. */
950 result
= domain
->conv_tab
[act
] + sizeof (size_t);
951 resultlen
= *(size_t *) domain
->conv_tab
[act
];
955 /* The result string is converted. */
957 #endif /* _LIBC || HAVE_ICONV */
959 *lengthp
= resultlen
;
964 /* Look up a plural variant. */
967 plural_lookup (domain
, n
, translation
, translation_len
)
968 struct loaded_l10nfile
*domain
;
970 const char *translation
;
971 size_t translation_len
;
973 struct loaded_domain
*domaindata
= (struct loaded_domain
*) domain
->data
;
974 unsigned long int index
;
977 index
= plural_eval (domaindata
->plural
, n
);
978 if (index
>= domaindata
->nplurals
)
979 /* This should never happen. It means the plural expression and the
980 given maximum value do not match. */
983 /* Skip INDEX strings at TRANSLATION. */
988 p
= __rawmemchr (p
, '\0');
990 p
= strchr (p
, '\0');
992 /* And skip over the NUL byte. */
995 if (p
>= translation
+ translation_len
)
996 /* This should never happen. It means the plural expression
997 evaluated to a value larger than the number of variants
998 available for MSGID1. */
999 return (char *) translation
;
1005 /* Return string representation of locale CATEGORY. */
1008 category_to_name (category
)
1017 retval
= "LC_COLLATE";
1022 retval
= "LC_CTYPE";
1027 retval
= "LC_MONETARY";
1032 retval
= "LC_NUMERIC";
1042 retval
= "LC_MESSAGES";
1047 retval
= "LC_RESPONSE";
1052 /* This might not make sense but is perhaps better than any other
1058 /* If you have a better idea for a default value let me know. */
1065 /* Guess value of current locale from value of the environment variables. */
1068 guess_category_value (category
, categoryname
)
1070 const char *categoryname
;
1072 const char *language
;
1075 /* The highest priority value is the `LANGUAGE' environment
1076 variable. But we don't use the value if the currently selected
1077 locale is the C locale. This is a GNU extension. */
1078 language
= getenv ("LANGUAGE");
1079 if (language
!= NULL
&& language
[0] == '\0')
1082 /* We have to proceed with the POSIX methods of looking to `LC_ALL',
1083 `LC_xxx', and `LANG'. On some systems this can be done by the
1084 `setlocale' function itself. */
1086 retval
= setlocale (category
, NULL
);
1088 retval
= _nl_locale_name (category
, categoryname
);
1091 /* Ignore LANGUAGE if the locale is set to "C" because
1092 1. "C" locale usually uses the ASCII encoding, and most international
1093 messages use non-ASCII characters. These characters get displayed
1094 as question marks (if using glibc's iconv()) or as invalid 8-bit
1095 characters (because other iconv()s refuse to convert most non-ASCII
1096 characters to ASCII). In any case, the output is ugly.
1097 2. The precise output of some programs in the "C" locale is specified
1098 by POSIX and should not depend on environment variables like
1099 "LANGUAGE". We allow such programs to use gettext(). */
1100 return language
!= NULL
&& strcmp (retval
, "C") != 0 ? language
: retval
;
1103 /* @@ begin of epilog @@ */
1105 /* We don't want libintl.a to depend on any other library. So we
1106 avoid the non-standard function stpcpy. In GNU C Library this
1107 function is available, though. Also allow the symbol HAVE_STPCPY
1109 #if !_LIBC && !HAVE_STPCPY
1115 while ((*dest
++ = *src
++) != '\0')
1121 #if !_LIBC && !HAVE_MEMPCPY
1123 mempcpy (dest
, src
, n
)
1128 return (void *) ((char *) memcpy (dest
, src
, n
) + n
);
1134 /* If we want to free all resources we have to do some work at
1136 static void __attribute__ ((unused
))
1141 while (_nl_domain_bindings
!= NULL
)
1143 struct binding
*oldp
= _nl_domain_bindings
;
1144 _nl_domain_bindings
= _nl_domain_bindings
->next
;
1145 if (oldp
->dirname
!= _nl_default_dirname
)
1146 /* Yes, this is a pointer comparison. */
1147 free (oldp
->dirname
);
1148 free (oldp
->codeset
);
1152 if (_nl_current_default_domain
!= _nl_default_default_domain
)
1153 /* Yes, again a pointer comparison. */
1154 free ((char *) _nl_current_default_domain
);
1156 /* Remove the search tree with the known translations. */
1157 __tdestroy (root
, free
);
1160 while (transmem_list
!= NULL
)
1162 old
= transmem_list
;
1163 transmem_list
= transmem_list
->next
;
1168 text_set_element (__libc_subfreeres
, free_mem
);