1 /* Implementation of the internal dcigettext function.
2 Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 /* Tell glibc's <string.h> to provide a prototype for mempcpy().
20 This must come before <config.h> because <config.h> may include
21 <features.h>, and once <features.h> has been included, it's too late. */
23 # define _GNU_SOURCE 1
30 #include <sys/types.h>
32 #if defined __GNUC__ && !defined C_ALLOCA
33 # define alloca __builtin_alloca
34 # define HAVE_ALLOCA 1
36 # if (defined HAVE_ALLOCA_H || defined _LIBC) && !defined C_ALLOCA
54 # define __set_errno(val) errno = (val)
57 #if defined STDC_HEADERS || defined _LIBC
69 #if defined HAVE_STRING_H || defined _LIBC
74 #if !HAVE_STRCHR && !defined _LIBC
80 #if defined HAVE_UNISTD_H || defined _LIBC
84 #if defined HAVE_LOCALE_H || defined _LIBC
88 #if defined HAVE_SYS_PARAM_H || defined _LIBC
89 # include <sys/param.h>
96 # include "libgnuintl.h"
98 #include "hash-string.h"
100 /* Thread safetyness. */
102 # include <bits/libc-lock.h>
104 /* Provide dummy implementation if this is outside glibc. */
105 # define __libc_lock_define_initialized(CLASS, NAME)
106 # define __libc_lock_lock(NAME)
107 # define __libc_lock_unlock(NAME)
108 # define __libc_rwlock_define_initialized(CLASS, NAME)
109 # define __libc_rwlock_rdlock(NAME)
110 # define __libc_rwlock_unlock(NAME)
113 /* Alignment of types. */
114 #if defined __GNUC__ && __GNUC__ >= 2
115 # define alignof(TYPE) __alignof__ (TYPE)
117 # define alignof(TYPE) \
118 ((int) &((struct { char dummy1; TYPE dummy2; } *) 0)->dummy2)
121 /* The internal variables in the standalone libintl.a must have different
122 names than the internal variables in GNU libc, otherwise programs
123 using libintl.a cannot be linked statically. */
125 # define _nl_default_default_domain _nl_default_default_domain__
126 # define _nl_current_default_domain _nl_current_default_domain__
127 # define _nl_default_dirname _nl_default_dirname__
128 # define _nl_domain_bindings _nl_domain_bindings__
131 /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>. */
133 # define offsetof(type,ident) ((size_t)&(((type*)0)->ident))
136 /* @@ end of prolog @@ */
139 /* Rename the non ANSI C functions. This is required by the standard
140 because some ANSI C functions will require linking with this object
141 file and the name space must not be polluted. */
142 # define getcwd __getcwd
144 # define stpcpy __stpcpy
146 # define tfind __tfind
148 # if !defined HAVE_GETCWD
150 # define getcwd(buf, max) getwd (buf)
155 static char *stpcpy
PARAMS ((char *dest
, const char *src
));
157 # ifndef HAVE_MEMPCPY
158 static void *mempcpy
PARAMS ((void *dest
, const void *src
, size_t n
));
162 /* Amount to increase buffer size by in each try. */
165 /* The following is from pathmax.h. */
166 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
167 PATH_MAX but might cause redefinition warnings when sys/param.h is
168 later included (as on MORE/BSD 4.3). */
169 #if defined _POSIX_VERSION || (defined HAVE_LIMITS_H && !defined __GNUC__)
173 #ifndef _POSIX_PATH_MAX
174 # define _POSIX_PATH_MAX 255
177 #if !defined PATH_MAX && defined _PC_PATH_MAX
178 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
181 /* Don't include sys/param.h if it already has been. */
182 #if defined HAVE_SYS_PARAM_H && !defined PATH_MAX && !defined MAXPATHLEN
183 # include <sys/param.h>
186 #if !defined PATH_MAX && defined MAXPATHLEN
187 # define PATH_MAX MAXPATHLEN
191 # define PATH_MAX _POSIX_PATH_MAX
194 /* XPG3 defines the result of `setlocale (category, NULL)' as:
195 ``Directs `setlocale()' to query `category' and return the current
196 setting of `local'.''
197 However it does not specify the exact format. Neither do SUSV2 and
198 ISO C 99. So we can use this feature only on selected systems (e.g.
199 those using GNU C Library). */
201 # define HAVE_LOCALE_NULL
204 /* This is the type used for the search tree where known translations
206 struct known_translation_t
208 /* Domain in which to search. */
214 /* State of the catalog counter at the point the string was found. */
217 /* Catalog where the string was found. */
218 struct loaded_l10nfile
*domain
;
220 /* And finally the translation. */
221 const char *translation
;
222 size_t translation_length
;
224 /* Pointer to the string in question. */
228 /* Root of the search tree with known translations. We can use this
229 only if the system provides the `tsearch' function family. */
230 #if defined HAVE_TSEARCH || defined _LIBC
236 # define tsearch __tsearch
239 /* Function to compare two entries in the table of known translations. */
240 static int transcmp
PARAMS ((const void *p1
, const void *p2
));
246 const struct known_translation_t
*s1
;
247 const struct known_translation_t
*s2
;
250 s1
= (const struct known_translation_t
*) p1
;
251 s2
= (const struct known_translation_t
*) p2
;
253 result
= strcmp (s1
->msgid
, s2
->msgid
);
256 result
= strcmp (s1
->domainname
, s2
->domainname
);
258 /* We compare the category last (though this is the cheapest
259 operation) since it is hopefully always the same (namely
261 result
= s1
->category
- s2
->category
;
268 /* Name of the default domain used for gettext(3) prior any call to
269 textdomain(3). The default value for this is "messages". */
270 const char _nl_default_default_domain
[] = "messages";
272 /* Value used as the default domain for gettext(3). */
273 const char *_nl_current_default_domain
= _nl_default_default_domain
;
275 /* Contains the default location of the message catalogs. */
276 const char _nl_default_dirname
[] = LOCALEDIR
;
278 /* List with bindings of specific domains created by bindtextdomain()
280 struct binding
*_nl_domain_bindings
;
282 /* Prototypes for local functions. */
283 static char *plural_lookup
PARAMS ((struct loaded_l10nfile
*domain
,
285 const char *translation
,
286 size_t translation_len
))
288 static unsigned long int plural_eval
PARAMS ((struct expression
*pexp
,
289 unsigned long int n
))
291 static const char *category_to_name
PARAMS ((int category
)) internal_function
;
292 static const char *guess_category_value
PARAMS ((int category
,
293 const char *categoryname
))
297 /* For those loosing systems which don't have `alloca' we have to add
298 some additional code emulating it. */
300 /* Nothing has to be done. */
301 # define ADD_BLOCK(list, address) /* nothing */
302 # define FREE_BLOCKS(list) /* nothing */
307 struct block_list
*next
;
309 # define ADD_BLOCK(list, addr) \
311 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
312 /* If we cannot get a free block we cannot add the new element to \
314 if (newp != NULL) { \
315 newp->address = (addr); \
316 newp->next = (list); \
320 # define FREE_BLOCKS(list) \
322 while (list != NULL) { \
323 struct block_list *old = list; \
329 # define alloca(size) (malloc (size))
330 #endif /* have alloca */
334 /* List of blocks allocated for translations. */
335 typedef struct transmem_list
337 struct transmem_list
*next
;
340 static struct transmem_list
*transmem_list
;
342 typedef unsigned char transmem_block_t
;
346 /* Names for the libintl functions are a problem. They must not clash
347 with existing names and they should follow ANSI C. But this source
348 code is also used in GNU C Library where the names have a __
349 prefix. So we have to make a difference here. */
351 # define DCIGETTEXT __dcigettext
353 # define DCIGETTEXT dcigettext__
356 /* Lock variable to protect the global data in the gettext implementation. */
357 __libc_rwlock_define_initialized (, _nl_state_lock
)
359 /* Checking whether the binaries runs SUID must be done and glibc provides
360 easier methods therefore we make a difference here. */
362 # define ENABLE_SECURE __libc_enable_secure
363 # define DETERMINE_SECURE
365 static int enable_secure
;
366 # define ENABLE_SECURE (enable_secure == 1)
367 # define DETERMINE_SECURE \
368 if (enable_secure == 0) \
370 if (getuid () != geteuid () || getgid () != getegid ()) \
373 enable_secure = -1; \
377 /* Look up MSGID in the DOMAINNAME message catalog for the current
378 CATEGORY locale and, if PLURAL is nonzero, search over string
379 depending on the plural form determined by N. */
381 DCIGETTEXT (domainname
, msgid1
, msgid2
, plural
, n
, category
)
382 const char *domainname
;
390 struct block_list
*block_list
= NULL
;
392 struct loaded_l10nfile
*domain
;
393 struct binding
*binding
;
394 const char *categoryname
;
395 const char *categoryvalue
;
396 char *dirname
, *xdomainname
;
401 #if defined HAVE_TSEARCH || defined _LIBC
402 struct known_translation_t
*search
;
403 struct known_translation_t
**foundp
= NULL
;
406 size_t domainname_len
;
408 /* If no real MSGID is given return NULL. */
412 __libc_rwlock_rdlock (_nl_state_lock
);
414 /* If DOMAINNAME is NULL, we are interested in the default domain. If
415 CATEGORY is not LC_MESSAGES this might not make much sense but the
416 definition left this undefined. */
417 if (domainname
== NULL
)
418 domainname
= _nl_current_default_domain
;
420 #if defined HAVE_TSEARCH || defined _LIBC
421 msgid_len
= strlen (msgid1
) + 1;
423 /* Try to find the translation among those which we found at
425 search
= (struct known_translation_t
*)
426 alloca (offsetof (struct known_translation_t
, msgid
) + msgid_len
);
427 memcpy (search
->msgid
, msgid1
, msgid_len
);
428 search
->domainname
= (char *) domainname
;
429 search
->category
= category
;
431 foundp
= (struct known_translation_t
**) tfind (search
, &root
, transcmp
);
432 if (foundp
!= NULL
&& (*foundp
)->counter
== _nl_msg_cat_cntr
)
434 /* Now deal with plural. */
436 retval
= plural_lookup ((*foundp
)->domain
, n
, (*foundp
)->translation
,
437 (*foundp
)->translation_length
);
439 retval
= (char *) (*foundp
)->translation
;
441 __libc_rwlock_unlock (_nl_state_lock
);
446 /* Preserve the `errno' value. */
449 /* See whether this is a SUID binary or not. */
452 /* First find matching binding. */
453 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
455 int compare
= strcmp (domainname
, binding
->domainname
);
461 /* It is not in the list. */
468 dirname
= (char *) _nl_default_dirname
;
469 else if (binding
->dirname
[0] == '/')
470 dirname
= binding
->dirname
;
473 /* We have a relative path. Make it absolute now. */
474 size_t dirname_len
= strlen (binding
->dirname
) + 1;
478 path_max
= (unsigned int) PATH_MAX
;
479 path_max
+= 2; /* The getcwd docs say to do this. */
481 dirname
= (char *) alloca (path_max
+ dirname_len
);
482 ADD_BLOCK (block_list
, dirname
);
485 while ((ret
= getcwd (dirname
, path_max
)) == NULL
&& errno
== ERANGE
)
487 path_max
+= PATH_INCR
;
488 dirname
= (char *) alloca (path_max
+ dirname_len
);
489 ADD_BLOCK (block_list
, dirname
);
495 /* We cannot get the current working directory. Don't signal an
496 error but simply return the default string. */
497 FREE_BLOCKS (block_list
);
498 __set_errno (saved_errno
);
501 /* Use the Germanic plural rule. */
502 : n
== 1 ? (char *) msgid1
: (char *) msgid2
);
505 stpcpy (stpcpy (strchr (dirname
, '\0'), "/"), binding
->dirname
);
508 /* Now determine the symbolic name of CATEGORY and its value. */
509 categoryname
= category_to_name (category
);
510 categoryvalue
= guess_category_value (category
, categoryname
);
512 domainname_len
= strlen (domainname
);
513 xdomainname
= (char *) alloca (strlen (categoryname
)
514 + domainname_len
+ 5);
515 ADD_BLOCK (block_list
, xdomainname
);
517 stpcpy (mempcpy (stpcpy (stpcpy (xdomainname
, categoryname
), "/"),
518 domainname
, domainname_len
),
521 /* Creating working area. */
522 single_locale
= (char *) alloca (strlen (categoryvalue
) + 1);
523 ADD_BLOCK (block_list
, single_locale
);
526 /* Search for the given string. This is a loop because we perhaps
527 got an ordered list of languages to consider for the translation. */
530 /* Make CATEGORYVALUE point to the next element of the list. */
531 while (categoryvalue
[0] != '\0' && categoryvalue
[0] == ':')
533 if (categoryvalue
[0] == '\0')
535 /* The whole contents of CATEGORYVALUE has been searched but
536 no valid entry has been found. We solve this situation
537 by implicitly appending a "C" entry, i.e. no translation
539 single_locale
[0] = 'C';
540 single_locale
[1] = '\0';
544 char *cp
= single_locale
;
545 while (categoryvalue
[0] != '\0' && categoryvalue
[0] != ':')
546 *cp
++ = *categoryvalue
++;
549 /* When this is a SUID binary we must not allow accessing files
550 outside the dedicated directories. */
551 if (ENABLE_SECURE
&& strchr (single_locale
, '/') != NULL
)
552 /* Ingore this entry. */
556 /* If the current locale value is C (or POSIX) we don't load a
557 domain. Return the MSGID. */
558 if (strcmp (single_locale
, "C") == 0
559 || strcmp (single_locale
, "POSIX") == 0)
561 FREE_BLOCKS (block_list
);
562 __libc_rwlock_unlock (_nl_state_lock
);
563 __set_errno (saved_errno
);
566 /* Use the Germanic plural rule. */
567 : n
== 1 ? (char *) msgid1
: (char *) msgid2
);
571 /* Find structure describing the message catalog matching the
572 DOMAINNAME and CATEGORY. */
573 domain
= _nl_find_domain (dirname
, single_locale
, xdomainname
, binding
);
577 retval
= _nl_find_msg (domain
, msgid1
, &retlen
);
583 for (cnt
= 0; domain
->successor
[cnt
] != NULL
; ++cnt
)
585 retval
= _nl_find_msg (domain
->successor
[cnt
], msgid1
,
590 domain
= domain
->successor
[cnt
];
598 /* Found the translation of MSGID1 in domain DOMAIN:
599 starting at RETVAL, RETLEN bytes. */
600 FREE_BLOCKS (block_list
);
601 __set_errno (saved_errno
);
602 #if defined HAVE_TSEARCH || defined _LIBC
605 /* Create a new entry and add it to the search tree. */
606 struct known_translation_t
*newp
;
608 newp
= (struct known_translation_t
*)
609 malloc (offsetof (struct known_translation_t
, msgid
)
610 + msgid_len
+ domainname_len
+ 1);
614 mempcpy (newp
->msgid
, msgid1
, msgid_len
);
615 memcpy (newp
->domainname
, domainname
, domainname_len
+ 1);
616 newp
->category
= category
;
617 newp
->counter
= _nl_msg_cat_cntr
;
618 newp
->domain
= domain
;
619 newp
->translation
= retval
;
620 newp
->translation_length
= retlen
;
622 /* Insert the entry in the search tree. */
623 foundp
= (struct known_translation_t
**)
624 tsearch (newp
, &root
, transcmp
);
626 || __builtin_expect (*foundp
!= newp
, 0))
627 /* The insert failed. */
633 /* We can update the existing entry. */
634 (*foundp
)->counter
= _nl_msg_cat_cntr
;
635 (*foundp
)->domain
= domain
;
636 (*foundp
)->translation
= retval
;
637 (*foundp
)->translation_length
= retlen
;
640 /* Now deal with plural. */
642 retval
= plural_lookup (domain
, n
, retval
, retlen
);
644 __libc_rwlock_unlock (_nl_state_lock
);
655 _nl_find_msg (domain_file
, msgid
, lengthp
)
656 struct loaded_l10nfile
*domain_file
;
660 struct loaded_domain
*domain
;
665 if (domain_file
->decided
== 0)
666 _nl_load_domain (domain_file
);
668 if (domain_file
->data
== NULL
)
671 domain
= (struct loaded_domain
*) domain_file
->data
;
673 /* Locate the MSGID and its translation. */
674 if (domain
->hash_size
> 2 && domain
->hash_tab
!= NULL
)
676 /* Use the hashing table. */
677 nls_uint32 len
= strlen (msgid
);
678 nls_uint32 hash_val
= hash_string (msgid
);
679 nls_uint32 idx
= hash_val
% domain
->hash_size
;
680 nls_uint32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
684 nls_uint32 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
687 /* Hash table entry is empty. */
690 /* Compare msgid with the original string at index nstr-1.
691 We compare the lengths with >=, not ==, because plural entries
692 are represented by strings with an embedded NUL. */
693 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) >= len
695 domain
->data
+ W (domain
->must_swap
,
696 domain
->orig_tab
[nstr
- 1].offset
))
703 if (idx
>= domain
->hash_size
- incr
)
704 idx
-= domain
->hash_size
- incr
;
712 /* Try the default method: binary search in the sorted array of
717 top
= domain
->nstrings
;
722 act
= (bottom
+ top
) / 2;
723 cmp_val
= strcmp (msgid
, (domain
->data
724 + W (domain
->must_swap
,
725 domain
->orig_tab
[act
].offset
)));
728 else if (cmp_val
> 0)
733 /* No translation was found. */
738 /* The translation was found at index ACT. If we have to convert the
739 string to use a different character set, this is the time. */
740 result
= ((char *) domain
->data
741 + W (domain
->must_swap
, domain
->trans_tab
[act
].offset
));
742 resultlen
= W (domain
->must_swap
, domain
->trans_tab
[act
].length
) + 1;
744 #if defined _LIBC || HAVE_ICONV
747 domain
->conv
!= (__gconv_t
) -1
750 domain
->conv
!= (iconv_t
) -1
755 /* We are supposed to do a conversion. First allocate an
756 appropriate table with the same structure as the table
757 of translations in the file, where we can put the pointers
758 to the converted strings in.
759 There is a slight complication with plural entries. They
760 are represented by consecutive NUL terminated strings. We
761 handle this case by converting RESULTLEN bytes, including
764 if (domain
->conv_tab
== NULL
765 && ((domain
->conv_tab
= (char **) calloc (domain
->nstrings
,
768 /* Mark that we didn't succeed allocating a table. */
769 domain
->conv_tab
= (char **) -1;
771 if (__builtin_expect (domain
->conv_tab
== (char **) -1, 0))
772 /* Nothing we can do, no more memory. */
775 if (domain
->conv_tab
[act
] == NULL
)
777 /* We haven't used this string so far, so it is not
778 translated yet. Do this now. */
779 /* We use a bit more efficient memory handling.
780 We allocate always larger blocks which get used over
781 time. This is faster than many small allocations. */
782 __libc_lock_define_initialized (static, lock
)
783 # define INITIAL_BLOCK_SIZE 4080
784 static unsigned char *freemem
;
785 static size_t freemem_size
;
787 const unsigned char *inbuf
;
788 unsigned char *outbuf
;
791 transmem_block_t
*transmem_list
= NULL
;
794 __libc_lock_lock (lock
);
796 inbuf
= (const unsigned char *) result
;
797 outbuf
= freemem
+ sizeof (size_t);
802 transmem_block_t
*newmem
;
804 size_t non_reversible
;
807 if (freemem_size
< sizeof (size_t))
810 res
= __gconv (domain
->conv
,
811 &inbuf
, inbuf
+ resultlen
,
813 outbuf
+ freemem_size
- sizeof (size_t),
816 if (res
== __GCONV_OK
|| res
== __GCONV_EMPTY_INPUT
)
819 if (res
!= __GCONV_FULL_OUTPUT
)
821 __libc_lock_unlock (lock
);
828 const char *inptr
= (const char *) inbuf
;
829 size_t inleft
= resultlen
;
830 char *outptr
= (char *) outbuf
;
833 if (freemem_size
< sizeof (size_t))
836 outleft
= freemem_size
- sizeof (size_t);
837 if (iconv (domain
->conv
,
838 (ICONV_CONST
char **) &inptr
, &inleft
,
842 outbuf
= (unsigned char *) outptr
;
847 __libc_lock_unlock (lock
);
854 /* We must allocate a new buffer or resize the old one. */
855 if (malloc_count
> 0)
858 freemem_size
= malloc_count
* INITIAL_BLOCK_SIZE
;
859 newmem
= (transmem_block_t
*) realloc (transmem_list
,
863 transmem_list
= transmem_list
->next
;
866 struct transmem_list
*old
= transmem_list
;
868 transmem_list
= transmem_list
->next
;
876 freemem_size
= INITIAL_BLOCK_SIZE
;
877 newmem
= (transmem_block_t
*) malloc (freemem_size
);
879 if (__builtin_expect (newmem
== NULL
, 0))
883 __libc_lock_unlock (lock
);
888 /* Add the block to the list of blocks we have to free
890 newmem
->next
= transmem_list
;
891 transmem_list
= newmem
;
893 freemem
= newmem
->data
;
894 freemem_size
-= offsetof (struct transmem_list
, data
);
896 transmem_list
= newmem
;
900 outbuf
= freemem
+ sizeof (size_t);
903 /* We have now in our buffer a converted string. Put this
904 into the table of conversions. */
905 *(size_t *) freemem
= outbuf
- freemem
- sizeof (size_t);
906 domain
->conv_tab
[act
] = (char *) freemem
;
907 /* Shrink freemem, but keep it aligned. */
908 freemem_size
-= outbuf
- freemem
;
910 freemem
+= freemem_size
& (alignof (size_t) - 1);
911 freemem_size
= freemem_size
& ~ (alignof (size_t) - 1);
913 __libc_lock_unlock (lock
);
916 /* Now domain->conv_tab[act] contains the translation of all
917 the plural variants. */
918 result
= domain
->conv_tab
[act
] + sizeof (size_t);
919 resultlen
= *(size_t *) domain
->conv_tab
[act
];
923 /* The result string is converted. */
925 #endif /* _LIBC || HAVE_ICONV */
927 *lengthp
= resultlen
;
932 /* Look up a plural variant. */
935 plural_lookup (domain
, n
, translation
, translation_len
)
936 struct loaded_l10nfile
*domain
;
938 const char *translation
;
939 size_t translation_len
;
941 struct loaded_domain
*domaindata
= (struct loaded_domain
*) domain
->data
;
942 unsigned long int index
;
945 index
= plural_eval (domaindata
->plural
, n
);
946 if (index
>= domaindata
->nplurals
)
947 /* This should never happen. It means the plural expression and the
948 given maximum value do not match. */
951 /* Skip INDEX strings at TRANSLATION. */
956 p
= __rawmemchr (p
, '\0');
958 p
= strchr (p
, '\0');
960 /* And skip over the NUL byte. */
963 if (p
>= translation
+ translation_len
)
964 /* This should never happen. It means the plural expression
965 evaluated to a value larger than the number of variants
966 available for MSGID1. */
967 return (char *) translation
;
973 /* Function to evaluate the plural expression and return an index value. */
974 static unsigned long int
976 plural_eval (pexp
, n
)
977 struct expression
*pexp
;
983 switch (pexp
->operation
)
988 return pexp
->val
.num
;
996 /* pexp->operation must be lnot. */
997 unsigned long int arg
= plural_eval (pexp
->val
.args
[0], n
);
1002 unsigned long int leftarg
= plural_eval (pexp
->val
.args
[0], n
);
1003 if (pexp
->operation
== lor
)
1004 return leftarg
|| plural_eval (pexp
->val
.args
[1], n
);
1005 else if (pexp
->operation
== land
)
1006 return leftarg
&& plural_eval (pexp
->val
.args
[1], n
);
1009 unsigned long int rightarg
= plural_eval (pexp
->val
.args
[1], n
);
1011 switch (pexp
->operation
)
1014 return leftarg
* rightarg
;
1016 return leftarg
/ rightarg
;
1018 return leftarg
% rightarg
;
1020 return leftarg
+ rightarg
;
1022 return leftarg
- rightarg
;
1024 return leftarg
< rightarg
;
1026 return leftarg
> rightarg
;
1028 return leftarg
<= rightarg
;
1029 case greater_or_equal
:
1030 return leftarg
>= rightarg
;
1032 return leftarg
== rightarg
;
1034 return leftarg
!= rightarg
;
1044 /* pexp->operation must be qmop. */
1045 unsigned long int boolarg
= plural_eval (pexp
->val
.args
[0], n
);
1046 return plural_eval (pexp
->val
.args
[boolarg
? 1 : 2], n
);
1054 /* Return string representation of locale CATEGORY. */
1057 category_to_name (category
)
1066 retval
= "LC_COLLATE";
1071 retval
= "LC_CTYPE";
1076 retval
= "LC_MONETARY";
1081 retval
= "LC_NUMERIC";
1091 retval
= "LC_MESSAGES";
1096 retval
= "LC_RESPONSE";
1101 /* This might not make sense but is perhaps better than any other
1107 /* If you have a better idea for a default value let me know. */
1114 /* Guess value of current locale from value of the environment variables. */
1117 guess_category_value (category
, categoryname
)
1119 const char *categoryname
;
1121 const char *language
;
1124 /* The highest priority value is the `LANGUAGE' environment
1125 variable. But we don't use the value if the currently selected
1126 locale is the C locale. This is a GNU extension. */
1127 language
= getenv ("LANGUAGE");
1128 if (language
!= NULL
&& language
[0] == '\0')
1131 /* We have to proceed with the POSIX methods of looking to `LC_ALL',
1132 `LC_xxx', and `LANG'. On some systems this can be done by the
1133 `setlocale' function itself. */
1134 #if defined _LIBC || (defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL)
1135 retval
= setlocale (category
, NULL
);
1137 /* Setting of LC_ALL overwrites all other. */
1138 retval
= getenv ("LC_ALL");
1139 if (retval
== NULL
|| retval
[0] == '\0')
1141 /* Next comes the name of the desired category. */
1142 retval
= getenv (categoryname
);
1143 if (retval
== NULL
|| retval
[0] == '\0')
1145 /* Last possibility is the LANG environment variable. */
1146 retval
= getenv ("LANG");
1147 if (retval
== NULL
|| retval
[0] == '\0')
1148 /* We use C as the default domain. POSIX says this is
1149 implementation defined. */
1155 return language
!= NULL
&& strcmp (retval
, "C") != 0 ? language
: retval
;
1158 /* @@ begin of epilog @@ */
1160 /* We don't want libintl.a to depend on any other library. So we
1161 avoid the non-standard function stpcpy. In GNU C Library this
1162 function is available, though. Also allow the symbol HAVE_STPCPY
1164 #if !_LIBC && !HAVE_STPCPY
1170 while ((*dest
++ = *src
++) != '\0')
1176 #if !_LIBC && !HAVE_MEMPCPY
1178 mempcpy (dest
, src
, n
)
1183 return (void *) ((char *) memcpy (dest
, src
, n
) + n
);
1189 /* If we want to free all resources we have to do some work at
1191 static void __attribute__ ((unused
))
1196 while (_nl_domain_bindings
!= NULL
)
1198 struct binding
*oldp
= _nl_domain_bindings
;
1199 _nl_domain_bindings
= _nl_domain_bindings
->next
;
1200 if (oldp
->dirname
!= _nl_default_dirname
)
1201 /* Yes, this is a pointer comparison. */
1202 free (oldp
->dirname
);
1203 free (oldp
->codeset
);
1207 if (_nl_current_default_domain
!= _nl_default_default_domain
)
1208 /* Yes, again a pointer comparison. */
1209 free ((char *) _nl_current_default_domain
);
1211 /* Remove the search tree with the known translations. */
1212 __tdestroy (root
, free
);
1215 while (transmem_list
!= NULL
)
1217 old
= transmem_list
;
1218 transmem_list
= transmem_list
->next
;
1223 text_set_element (__libc_subfreeres
, free_mem
);