1 /* Program that prints the names of the categories of the current locale.
2    Copyright (C) 2019-2021 Free Software Foundation, Inc.
3 
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8 
9    This program 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
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16 
17 /* Written by Bruno Haible <bruno@clisp.org>, 2019.  */
18 
19 #include <config.h>
20 
21 #include <locale.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 /* We want to use the system's setlocale() function here, not the gnulib
26    override.  */
27 #undef setlocale
28 
29 /* Specification:
30    <https://pubs.opengroup.org/onlinepubs/9699919799/utilities/locale.html>
31    Here we implement only the invocation without any command-line options.  */
32 
33 static const char *
defaulted_getenv(const char * variable)34 defaulted_getenv (const char *variable)
35 {
36   const char *value = getenv (variable);
37   return (value != NULL ? value : "");
38 }
39 
40 static void
print_category(int category,const char * variable)41 print_category (int category, const char *variable)
42 {
43   const char *value = defaulted_getenv (variable);
44   if (value[0] != '\0' && defaulted_getenv ("LC_ALL")[0] == '\0')
45     /* The variable is set in the environment and not overridden by LC_ALL.  */
46     printf ("%s=%s\n", variable, value);
47   else
48     printf ("%s=\"%s\"\n", variable, setlocale (category, NULL));
49 }
50 
51 int
main(void)52 main (void)
53 {
54   setlocale (LC_ALL, "");
55 
56   printf ("LANG=%s\n", defaulted_getenv ("LANG"));
57   print_category (LC_CTYPE, "LC_CTYPE");
58   print_category (LC_NUMERIC, "LC_NUMERIC");
59   print_category (LC_TIME, "LC_TIME");
60   print_category (LC_COLLATE, "LC_COLLATE");
61   print_category (LC_MONETARY, "LC_MONETARY");
62   print_category (LC_MESSAGES, "LC_MESSAGES");
63 #ifdef LC_PAPER
64   print_category (LC_PAPER, "LC_PAPER");
65 #endif
66 #ifdef LC_NAME
67   print_category (LC_NAME, "LC_NAME");
68 #endif
69 #ifdef LC_ADDRESS
70   print_category (LC_ADDRESS, "LC_ADDRESS");
71 #endif
72 #ifdef LC_TELEPHONE
73   print_category (LC_TELEPHONE, "LC_TELEPHONE");
74 #endif
75 #ifdef LC_MEASUREMENT
76   print_category (LC_MEASUREMENT, "LC_MEASUREMENT");
77 #endif
78 #ifdef LC_IDENTIFICATION
79   print_category (LC_IDENTIFICATION, "LC_IDENTIFICATION");
80 #endif
81 
82   printf ("LC_ALL=%s\n", defaulted_getenv ("LC_ALL"));
83 
84   return 0;
85 }
86