1 /* Implementation of the locale program according to POSIX 9945-2.
2 Copyright (C) 1995-1997, 1999-2007, 2008 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1995.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
36 #include <stdio_ext.h>
43 #include "localeinfo.h"
44 #include "charmap-dir.h"
45 #include "../locarchive.h"
47 extern void *xmalloc (size_t __n
);
48 extern char *xstrdup (const char *__str
);
50 #define ARCHIVE_NAME LOCALEDIR "/locale-archive"
52 /* If set print the name of the category. */
53 static int show_category_name
;
55 /* If set print the name of the item. */
56 static int show_keyword_name
;
58 /* Print names of all available locales. */
61 /* Print names of all available character maps. */
62 static int do_charmaps
= 0;
64 /* Nonzero if verbose output is wanted. */
67 /* Name and version of program. */
68 static void print_version (FILE *stream
, struct argp_state
*state
);
69 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
71 /* Definitions of arguments for argp functions. */
72 static const struct argp_option options
[] =
74 { NULL
, 0, NULL
, 0, N_("System information:") },
75 { "all-locales", 'a', NULL
, OPTION_NO_USAGE
,
76 N_("Write names of available locales") },
77 { "charmaps", 'm', NULL
, OPTION_NO_USAGE
,
78 N_("Write names of available charmaps") },
79 { NULL
, 0, NULL
, 0, N_("Modify output format:") },
80 { "category-name", 'c', NULL
, 0, N_("Write names of selected categories") },
81 { "keyword-name", 'k', NULL
, 0, N_("Write names of selected keywords") },
82 { "verbose", 'v', NULL
, 0, N_("Print more information") },
83 { NULL
, 0, NULL
, 0, NULL
}
86 /* Short description of program. */
87 static const char doc
[] = N_("Get locale-specific information.\v\
88 For bug reporting instructions, please see:\n\
89 <http://www.gnu.org/software/libc/bugs.html>.\n");
91 /* Strings for arguments in help texts. */
92 static const char args_doc
[] = N_("NAME\n[-a|-m]");
94 /* Prototype for option handler. */
95 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
97 /* Data structure to communicate with argp functions. */
98 static struct argp argp
=
100 options
, parse_opt
, args_doc
, doc
104 /* We don't have these constants defined because we don't use them. Give
106 #define CTYPE_MB_CUR_MIN 0
107 #define CTYPE_MB_CUR_MAX 0
108 #define CTYPE_HASH_SIZE 0
109 #define CTYPE_HASH_LAYERS 0
110 #define CTYPE_CLASS 0
111 #define CTYPE_TOUPPER_EB 0
112 #define CTYPE_TOLOWER_EB 0
113 #define CTYPE_TOUPPER_EL 0
114 #define CTYPE_TOLOWER_EL 0
116 /* Definition of the data structure which represents a category and its
127 enum { std
, opt
} status
;
128 enum value_type value_type
;
134 /* Simple helper macro. */
135 #define NELEMS(arr) ((sizeof (arr)) / (sizeof (arr[0])))
137 /* For some tricky stuff. */
138 #define NO_PAREN(Item, More...) Item, ## More
140 /* We have all categories defined in `categories.def'. Now construct
141 the description and data structure used for all categories. */
142 #define DEFINE_ELEMENT(Item, More...) { Item, ## More },
143 #define DEFINE_CATEGORY(category, name, items, postload) \
144 static struct cat_item category##_desc[] = \
149 #include "categories.def"
150 #undef DEFINE_CATEGORY
152 static struct category category
[] =
154 #define DEFINE_CATEGORY(category, name, items, postload) \
155 [category] = { _NL_NUM_##category, name, NELEMS (category##_desc), \
157 #include "categories.def"
158 #undef DEFINE_CATEGORY
160 #define NCATEGORIES NELEMS (category)
163 /* Automatically set variable. */
164 extern const char *__progname
;
166 /* helper function for extended name handling. */
167 extern void locale_special (const char *name
, int show_category_name
,
168 int show_keyword_name
);
170 /* Prototypes for local functions. */
171 static void print_LC_IDENTIFICATION (void *mapped
, size_t size
);
172 static void print_LC_CTYPE (void *mapped
, size_t size
);
173 static void write_locales (void);
174 static int nameentcmp (const void *a
, const void *b
);
175 static int write_archive_locales (void **all_datap
, char *linebuf
);
176 static void write_charmaps (void);
177 static void show_locale_vars (void);
178 static void show_info (const char *name
);
182 main (int argc
, char *argv
[])
186 /* Set initial values for global variables. */
187 show_category_name
= 0;
188 show_keyword_name
= 0;
190 /* Set locale. Do not set LC_ALL because the other categories must
191 not be affected (according to POSIX.2). */
192 if (setlocale (LC_CTYPE
, "") == NULL
)
193 error (0, errno
, gettext ("Cannot set LC_CTYPE to default locale"));
194 if (setlocale (LC_MESSAGES
, "") == NULL
)
195 error (0, errno
, gettext ("Cannot set LC_MESSAGES to default locale"));
197 /* Initialize the message catalog. */
198 textdomain (PACKAGE
);
200 /* Parse and process arguments. */
201 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
203 /* `-a' requests the names of all available locales. */
206 if (setlocale (LC_COLLATE
, "") == NULL
)
208 gettext ("Cannot set LC_COLLATE to default locale"));
213 /* `m' requests the names of all available charmaps. The names can be
214 used for the -f argument to localedef(1). */
215 if (do_charmaps
!= 0)
221 /* Specific information about the current locale are requested.
222 Change to this locale now. */
223 if (setlocale (LC_ALL
, "") == NULL
)
224 error (0, errno
, gettext ("Cannot set LC_ALL to default locale"));
226 /* If no real argument is given we have to print the contents of the
227 current locale definition variables. These are LANG and the LC_*. */
228 if (remaining
== argc
&& show_keyword_name
== 0 && show_category_name
== 0)
234 /* Process all given names. */
235 while (remaining
< argc
)
236 show_info (argv
[remaining
++]);
242 /* Handle program arguments. */
244 parse_opt (int key
, char *arg
, struct argp_state
*state
)
252 show_category_name
= 1;
258 show_keyword_name
= 1;
264 return ARGP_ERR_UNKNOWN
;
270 /* Print the version information. */
272 print_version (FILE *stream
, struct argp_state
*state
)
274 fprintf (stream
, "locale (GNU %s) %s\n", PACKAGE
, VERSION
);
275 fprintf (stream
, gettext ("\
276 Copyright (C) %s Free Software Foundation, Inc.\n\
277 This is free software; see the source for copying conditions. There is NO\n\
278 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
280 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
284 /* Simple action function which prints arguments as strings. */
286 print_names (const void *nodep
, VISIT value
, int level
)
288 if (value
== postorder
|| value
== leaf
)
289 puts (*(char **) nodep
);
294 select_dirs (const struct dirent
*dirent
)
298 if (strcmp (dirent
->d_name
, ".") != 0 && strcmp (dirent
->d_name
, "..") != 0)
302 #ifdef _DIRENT_HAVE_D_TYPE
303 if (dirent
->d_type
!= DT_UNKNOWN
&& dirent
->d_type
!= DT_LNK
)
304 mode
= DTTOIF (dirent
->d_type
);
309 char buf
[sizeof (LOCALEDIR
) + strlen (dirent
->d_name
) + 1];
311 stpcpy (stpcpy (stpcpy (buf
, LOCALEDIR
), "/"), dirent
->d_name
);
313 if (stat64 (buf
, &st
) == 0)
317 result
= S_ISDIR (mode
);
325 print_LC_IDENTIFICATION (void *mapped
, size_t size
)
327 /* Read the information from the file. */
331 unsigned int nstrings
;
332 unsigned int strindex
[0];
333 } *filedata
= mapped
;
335 if (filedata
->magic
== LIMAGIC (LC_IDENTIFICATION
)
337 + (filedata
->nstrings
338 * sizeof (unsigned int))
343 #define HANDLE(idx, name) \
344 str = ((char *) mapped \
345 + filedata->strindex[_NL_ITEM_INDEX (_NL_IDENTIFICATION_##idx)]); \
347 printf ("%9s | %s\n", name, str)
348 HANDLE (TITLE
, "title");
349 HANDLE (SOURCE
, "source");
350 HANDLE (ADDRESS
, "address");
351 HANDLE (CONTACT
, "contact");
352 HANDLE (EMAIL
, "email");
353 HANDLE (TEL
, "telephone");
355 HANDLE (LANGUAGE
, "language");
356 HANDLE (TERRITORY
, "territory");
357 HANDLE (AUDIENCE
, "audience");
358 HANDLE (APPLICATION
, "application");
359 HANDLE (ABBREVIATION
, "abbreviation");
360 HANDLE (REVISION
, "revision");
361 HANDLE (DATE
, "date");
367 print_LC_CTYPE (void *mapped
, size_t size
)
372 unsigned int nstrings
;
373 unsigned int strindex
[0];
374 } *filedata
= mapped
;
376 if (filedata
->magic
== LIMAGIC (LC_CTYPE
)
378 + (filedata
->nstrings
379 * sizeof (unsigned int))
384 str
= ((char *) mapped
385 + filedata
->strindex
[_NL_ITEM_INDEX (_NL_CTYPE_CODESET_NAME
)]);
387 printf (" codeset | %s\n", str
);
392 /* Write the names of all available locales to stdout. We have some
393 sources of the information: the contents of the locale directory
394 and the locale.alias file. To avoid duplicates and print the
395 result is a reasonable order we put all entries is a search tree
396 and print them afterwards. */
401 void *all_data
= NULL
;
402 struct dirent
**dirents
;
406 size_t alias_path_len
;
408 int first_locale
= 1;
410 #define PUT(name) tsearch (name, &all_data, \
411 (int (*) (const void *, const void *)) strcoll)
412 #define GET(name) tfind (name, &all_data, \
413 (int (*) (const void *, const void *)) strcoll)
415 /* `POSIX' locale is always available (POSIX.2 4.34.3). */
417 /* And so is the "C" locale. */
420 memset (linebuf
, '-', sizeof (linebuf
) - 1);
421 linebuf
[sizeof (linebuf
) - 1] = '\0';
423 /* First scan the locale archive. */
424 if (write_archive_locales (&all_data
, linebuf
))
427 /* Now we can look for all files in the directory. */
428 ndirents
= scandir (LOCALEDIR
, &dirents
, select_dirs
, alphasort
);
429 for (cnt
= 0; cnt
< ndirents
; ++cnt
)
431 /* Test whether at least the LC_CTYPE data is there. Some
432 directories only contain translations. */
433 char buf
[sizeof (LOCALEDIR
) + strlen (dirents
[cnt
]->d_name
)
434 + sizeof "/LC_IDENTIFICATION"];
438 stpcpy (enddir
= stpcpy (stpcpy (stpcpy (buf
, LOCALEDIR
), "/"),
439 dirents
[cnt
]->d_name
),
440 "/LC_IDENTIFICATION");
442 if (stat64 (buf
, &st
) == 0 && S_ISREG (st
.st_mode
))
444 if (verbose
&& GET (dirents
[cnt
]->d_name
) == NULL
)
446 /* Provide some nice output of all kinds of
451 putchar_unlocked ('\n');
454 printf ("locale: %-15.15s directory: %.*s\n%s\n",
455 dirents
[cnt
]->d_name
, (int) (enddir
- buf
), buf
,
458 fd
= open64 (buf
, O_RDONLY
);
461 void *mapped
= mmap64 (NULL
, st
.st_size
, PROT_READ
,
463 if (mapped
!= MAP_FAILED
)
465 print_LC_IDENTIFICATION (mapped
, st
.st_size
);
467 munmap (mapped
, st
.st_size
);
472 /* Now try to get the charset information. */
473 strcpy (enddir
, "/LC_CTYPE");
474 fd
= open64 (buf
, O_RDONLY
);
475 if (fd
!= -1 && fstat64 (fd
, &st
) >= 0
476 && ((mapped
= mmap64 (NULL
, st
.st_size
, PROT_READ
,
480 print_LC_CTYPE (mapped
, st
.st_size
);
482 munmap (mapped
, st
.st_size
);
490 /* If the verbose format is not selected we simply
491 collect the names. */
492 PUT (xstrdup (dirents
[cnt
]->d_name
));
498 /* Now read the locale.alias files. */
499 if (argz_create_sep (LOCALE_ALIAS_PATH
, ':', &alias_path
, &alias_path_len
))
500 error (1, errno
, gettext ("while preparing output"));
503 while ((entry
= argz_next (alias_path
, alias_path_len
, entry
)))
505 static const char aliasfile
[] = "/locale.alias";
507 char full_name
[strlen (entry
) + sizeof aliasfile
];
509 stpcpy (stpcpy (full_name
, entry
), aliasfile
);
510 fp
= fopen (full_name
, "rm");
512 /* Ignore non-existing files. */
515 /* No threads present. */
516 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
518 while (! feof_unlocked (fp
))
520 /* It is a reasonable approach to use a fix buffer here
522 a) we are only interested in the first two fields
523 b) these fields must be usable as file names and so must
530 if (fgets_unlocked (buf
, BUFSIZ
, fp
) == NULL
)
535 /* Ignore leading white space. */
536 while (isspace (cp
[0]) && cp
[0] != '\n')
539 /* A leading '#' signals a comment line. */
540 if (cp
[0] != '\0' && cp
[0] != '#' && cp
[0] != '\n')
543 while (cp
[0] != '\0' && !isspace (cp
[0]))
545 /* Terminate alias name. */
549 /* Now look for the beginning of the value. */
550 while (isspace (cp
[0]))
556 while (cp
[0] != '\0' && !isspace (cp
[0]))
558 /* Terminate value. */
561 /* This has to be done to make the following
562 test for the end of line possible. We are
563 looking for the terminating '\n' which do not
568 else if (cp
[0] != '\0')
572 if (! verbose
&& GET (value
) != NULL
)
573 PUT (xstrdup (alias
));
577 /* Possibly not the whole line fits into the buffer.
578 Ignore the rest of the line. */
579 while (strchr (cp
, '\n') == NULL
)
582 if (fgets_unlocked (buf
, BUFSIZ
, fp
) == NULL
)
583 /* Make sure the inner loop will be left. The outer
584 loop will exit at the `feof' test. */
594 twalk (all_data
, print_names
);
602 uint32_t locrec_offset
;
607 nameentcmp (const void *a
, const void *b
)
609 return strcoll (((const struct nameent
*) a
)->name
,
610 ((const struct nameent
*) b
)->name
);
615 write_archive_locales (void **all_datap
, char *linebuf
)
618 void *all_data
= *all_datap
;
620 struct locarhead
*head
;
621 struct namehashent
*namehashtab
;
622 char *addr
= MAP_FAILED
;
626 fd
= open64 (ARCHIVE_NAME
, O_RDONLY
);
630 if (fstat64 (fd
, &st
) < 0 || st
.st_size
< sizeof (*head
))
634 addr
= mmap64 (NULL
, len
, PROT_READ
, MAP_SHARED
, fd
, 0);
635 if (addr
== MAP_FAILED
)
638 head
= (struct locarhead
*) addr
;
639 if (head
->namehash_offset
+ head
->namehash_size
> len
640 || head
->string_offset
+ head
->string_size
> len
641 || head
->locrectab_offset
+ head
->locrectab_size
> len
642 || head
->sumhash_offset
+ head
->sumhash_size
> len
)
645 namehashtab
= (struct namehashent
*) (addr
+ head
->namehash_offset
);
648 for (cnt
= 0; cnt
< head
->namehash_size
; ++cnt
)
649 if (namehashtab
[cnt
].locrec_offset
!= 0)
651 PUT (xstrdup (addr
+ namehashtab
[cnt
].name_offset
));
657 struct nameent
*names
;
660 names
= (struct nameent
*) xmalloc (head
->namehash_used
661 * sizeof (struct nameent
));
662 for (cnt
= used
= 0; cnt
< head
->namehash_size
; ++cnt
)
663 if (namehashtab
[cnt
].locrec_offset
!= 0)
665 names
[used
].name
= addr
+ namehashtab
[cnt
].name_offset
;
666 names
[used
++].locrec_offset
= namehashtab
[cnt
].locrec_offset
;
669 /* Sort the names. */
670 qsort (names
, used
, sizeof (struct nameent
), nameentcmp
);
672 for (cnt
= 0; cnt
< used
; ++cnt
)
674 struct locrecent
*locrec
;
676 PUT (xstrdup (names
[cnt
].name
));
679 putchar_unlocked ('\n');
681 printf ("locale: %-15.15s archive: " ARCHIVE_NAME
"\n%s\n",
682 names
[cnt
].name
, linebuf
);
684 locrec
= (struct locrecent
*) (addr
+ names
[cnt
].locrec_offset
);
686 print_LC_IDENTIFICATION (addr
687 + locrec
->record
[LC_IDENTIFICATION
].offset
,
688 locrec
->record
[LC_IDENTIFICATION
].len
);
690 print_LC_CTYPE (addr
+ locrec
->record
[LC_CTYPE
].offset
,
691 locrec
->record
[LC_CTYPE
].len
);
698 if (addr
!= MAP_FAILED
)
701 *all_datap
= all_data
;
706 /* Write the names of all available character maps to stdout. */
708 write_charmaps (void)
710 void *all_data
= NULL
;
714 /* Look for all files in the charmap directory. */
715 dir
= charmap_opendir (CHARMAP_PATH
);
719 while ((dirent
= charmap_readdir (dir
)) != NULL
)
724 PUT (xstrdup (dirent
));
726 aliases
= charmap_aliases (CHARMAP_PATH
, dirent
);
729 /* Add the code_set_name and the aliases. */
730 for (p
= aliases
; *p
; p
++)
733 /* Add the code_set_name only. Most aliases are obsolete. */
739 charmap_free_aliases (aliases
);
742 charmap_closedir (dir
);
744 twalk (all_data
, print_names
);
748 /* We have to show the contents of the environments determining the
751 show_locale_vars (void)
754 const char *lcall
= getenv ("LC_ALL");
755 const char *lang
= getenv ("LANG") ? : "";
757 auto void get_source (const char *name
);
759 void get_source (const char *name
)
761 char *val
= getenv (name
);
763 if ((lcall
?: "")[0] != '\0' || val
== NULL
)
764 printf ("%s=\"%s\"\n", name
,
765 (lcall
?: "")[0] ? lcall
: (lang
?: "")[0] ? lang
: "POSIX");
767 printf ("%s=%s\n", name
, val
);
770 /* LANG has to be the first value. */
771 printf ("LANG=%s\n", lang
);
773 /* Now all categories in an unspecified order. */
774 for (cat_no
= 0; cat_no
< NCATEGORIES
; ++cat_no
)
775 if (cat_no
!= LC_ALL
)
776 get_source (category
[cat_no
].name
);
778 /* The last is the LC_ALL value. */
779 printf ("LC_ALL=%s\n", lcall
? : "");
783 /* Show the information request for NAME. */
785 show_info (const char *name
)
789 auto void print_item (struct cat_item
*item
);
791 void print_item (struct cat_item
*item
)
793 switch (item
->value_type
)
796 if (show_keyword_name
)
797 printf ("%s=\"", item
->name
);
798 fputs (nl_langinfo (item
->item_id
) ? : "", stdout
);
799 if (show_keyword_name
)
808 if (show_keyword_name
)
809 printf ("%s=\"", item
->name
);
811 for (cnt
= 0; cnt
< item
->max
- 1; ++cnt
)
813 val
= nl_langinfo (item
->item_id
+ cnt
);
819 val
= nl_langinfo (item
->item_id
+ cnt
);
823 if (show_keyword_name
)
831 const char *val
= nl_langinfo (item
->item_id
) ? : "";
834 if (show_keyword_name
)
835 printf ("%s=", item
->name
);
837 for (cnt
= 0; cnt
< item
->max
&& *val
!= '\0'; ++cnt
)
839 printf ("%s%s%s%s", first
? "" : ";",
840 show_keyword_name
? "\"" : "", val
,
841 show_keyword_name
? "\"" : "");
842 val
= strchr (val
, '\0') + 1;
850 const char *val
= nl_langinfo (item
->item_id
);
852 if (show_keyword_name
)
853 printf ("%s=", item
->name
);
856 printf ("%d", *val
== '\177' ? -1 : *val
);
862 const char *val
= nl_langinfo (item
->item_id
);
863 int cnt
= val
? strlen (val
) : 0;
865 if (show_keyword_name
)
866 printf ("%s=", item
->name
);
870 printf ("%d;", *val
== '\177' ? -1 : *val
);
875 printf ("%d\n", cnt
== 0 || *val
== '\177' ? -1 : *val
);
880 union { unsigned int word
; char *string
; } val
;
881 val
.string
= nl_langinfo (item
->item_id
);
882 if (show_keyword_name
)
883 printf ("%s=", item
->name
);
885 printf ("%d\n", val
.word
);
891 /* We don't print wide character information since the same
892 information is available in a multibyte string. */
899 for (cat_no
= 0; cat_no
< NCATEGORIES
; ++cat_no
)
900 if (cat_no
!= LC_ALL
)
904 if (strcmp (name
, category
[cat_no
].name
) == 0)
905 /* Print the whole category. */
907 if (show_category_name
!= 0)
908 puts (category
[cat_no
].name
);
910 for (item_no
= 0; item_no
< category
[cat_no
].number
; ++item_no
)
911 print_item (&category
[cat_no
].item_desc
[item_no
]);
916 for (item_no
= 0; item_no
< category
[cat_no
].number
; ++item_no
)
917 if (strcmp (name
, category
[cat_no
].item_desc
[item_no
].name
) == 0)
919 if (show_category_name
!= 0)
920 puts (category
[cat_no
].name
);
922 print_item (&category
[cat_no
].item_desc
[item_no
]);
927 /* The name is not a standard one.
928 For testing and perhaps advanced use allow some more symbols. */
929 locale_special (name
, show_category_name
, show_keyword_name
);