GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/locale/locale.c Lines: 0 47 0.0 %
Date: 2017-11-07 Branches: 0 64 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: locale.c,v 1.12 2016/02/05 12:59:12 jca Exp $	*/
2
/*
3
 * Copyright (c) 2013 Stefan Sperling <stsp@openbsd.org>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
18
#include <err.h>
19
#include <locale.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23
#include <unistd.h>
24
25
extern char		*__progname;
26
27
struct category_name {
28
	int category;
29
	const char *name;
30
} categories[] = {
31
	{ LC_COLLATE,	"LC_COLLATE" },
32
	{ LC_CTYPE,	"LC_CTYPE" },
33
	{ LC_MONETARY,	"LC_MONETARY" },
34
	{ LC_NUMERIC,	"LC_NUMERIC" },
35
	{ LC_TIME,	"LC_TIME" },
36
	{ LC_MESSAGES,	"LC_MESSAGES" },
37
	{ 0, 		NULL},
38
};
39
40
static void
41
put_assignment(const char *name, const char *value, int double_quoted)
42
{
43
	char c;
44
45
	fputs(name, stdout);
46
	putchar('=');
47
	if (double_quoted)
48
		putchar('"');
49
	if (value != NULL)
50
		while ((c = *value++) != '\0')
51
			switch (c) {
52
			case ' ': case '\t': case '\n': case '\'':
53
			case '(': case ')': case '<': case '>':
54
			case '&': case ';': case '|': case '~':
55
				if (!double_quoted)
56
			case '"': case '\\': case '$': case '`':
57
					putchar('\\');
58
			default:
59
				putchar(c);
60
				break;
61
			}
62
	if (double_quoted)
63
		putchar('"');
64
	putchar('\n');
65
}
66
67
static void
68
show_current_locale(void)
69
{
70
	char *lang, *lc_all;
71
	int i;
72
73
	lang = getenv("LANG");
74
	lc_all = getenv("LC_ALL");
75
76
	put_assignment("LANG", lang, 0);
77
	for (i = 0; categories[i].name != NULL; i++) {
78
		if (lc_all == NULL && getenv(categories[i].name))
79
			put_assignment(categories[i].name,
80
			    getenv(categories[i].name), 0);
81
		else
82
			put_assignment(categories[i].name,
83
			    setlocale(categories[i].category, NULL), 1);
84
	}
85
	put_assignment("LC_ALL", lc_all, 0);
86
}
87
88
const char * const some_locales[] = {
89
	"C",
90
	"C.UTF-8",
91
	"POSIX",
92
	"POSIX.UTF-8",
93
	"Pig.UTF-8",
94
	"ar_SD.UTF-8",
95
	"ar_SY.UTF-8",
96
	"ca_ES.UTF-8",
97
	"cs_CZ.UTF-8",
98
	"da_DK.UTF-8",
99
	"de_AT.UTF-8",
100
	"de_CH.UTF-8",
101
	"de_DE.UTF-8",
102
	"el_GR.UTF-8",
103
	"en_AU.UTF-8",
104
	"en_CA.UTF-8",
105
	"en_GB.UTF-8",
106
	"en_US.UTF-8",
107
	"es_AR.UTF-8",
108
	"es_BO.UTF-8",
109
	"es_CH.UTF-8",
110
	"es_CO.UTF-8",
111
	"es_CR.UTF-8",
112
	"es_CU.UTF-8",
113
	"es_DO.UTF-8",
114
	"es_EC.UTF-8",
115
	"es_ES.UTF-8",
116
	"es_GQ.UTF-8",
117
	"es_GT.UTF-8",
118
	"es_HN.UTF-8",
119
	"es_MX.UTF-8",
120
	"es_NI.UTF-8",
121
	"es_PA.UTF-8",
122
	"es_PE.UTF-8",
123
	"es_PR.UTF-8",
124
	"es_PY.UTF-8",
125
	"es_SV.UTF-8",
126
	"es_US.UTF-8",
127
	"es_UY.UTF-8",
128
	"es_VE.UTF-8",
129
	"fa_IR.UTF-8",
130
	"fi_FI.UTF-8",
131
	"fr_BE.UTF-8",
132
	"fr_CA.UTF-8",
133
	"fr_CH.UTF-8",
134
	"fr_FR.UTF-8",
135
	"hu_HU.UTF-8",
136
	"hy_AM.UTF-8",
137
	"is_IS.UTF-8",
138
	"it_CH.UTF-8",
139
	"it_IT.UTF-8",
140
	"ja_JP.UTF-8",
141
	"ko_KR.UTF-8",
142
	"lt_LT.UTF-8",
143
	"nl_BE.UTF-8",
144
	"nl_NL.UTF-8",
145
	"no_NO.UTF-8",
146
	"pl_PL.UTF-8",
147
	"pt_PT.UTF-8",
148
	"ro_RO.UTF-8",
149
	"ru_RU.UTF-8",
150
	"sk_SK.UTF-8",
151
	"sl_SI.UTF-8",
152
	"sv_SE.UTF-8",
153
	"tr_TR.UTF-8",
154
	"uk_UA.UTF-8",
155
	"zh_CN.UTF-8",
156
	"zh_TW.UTF-8",
157
	NULL
158
};
159
160
static void
161
show_locales(void)
162
{
163
	int i = 0;
164
165
	while (some_locales[i])
166
		puts(some_locales[i++]);
167
}
168
169
static void
170
usage(void)
171
{
172
	fprintf(stderr, "usage: %s [-a | -m]\n", __progname);
173
	exit(1);
174
}
175
176
int
177
main(int argc, char *argv[])
178
{
179
	int opt, aflag = 0, mflag = 0;
180
181
	setlocale(LC_ALL, "");
182
183
	if (pledge("stdio flock rpath cpath wpath", NULL) == -1)
184
		err(1, "pledge");
185
186
	if (argc == 1) {
187
		show_current_locale();
188
		return 0;
189
	}
190
191
	while ((opt = getopt(argc, argv, "am")) != -1) {
192
		switch (opt) {
193
		case 'a':
194
			aflag = 1;
195
			break;
196
		case 'm':
197
			mflag = 1;
198
			break;
199
		default:
200
			usage();
201
		}
202
	}
203
	argc -= optind;
204
	argv += optind;
205
206
	if (argc != 0 || (aflag && mflag))
207
		usage();
208
	else if (aflag)
209
		show_locales();
210
	else if (mflag)
211
		printf("UTF-8\n");
212
213
	return 0;
214
}