xref: /titanic_51/usr/src/test/libc-tests/tests/strerror/strerror.c (revision b599bd937c305a895426e8c412ca920ce7824850)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2015 Joyent, Inc.
14  */
15 
16 #include <string.h>
17 #include <locale.h>
18 #include <assert.h>
19 #include <errno.h>
20 #include <stdio.h>
21 
22 /*
23  * This is designed to test strerorr and strerror_l's ability to react properly
24  * to being in various locales. This also serves as a regression test for
25  * illumos#6133.
26  *
27  * For this test, we utilize the poorly named 'zz_AA.UTF_8' locale which
28  * was created because it actually has a translation! It 'translates'
29  * the string:
30  *
31  * "No such file or directory" -> "It's a trap!"
32  *
33  * It's otherwise a boring en_US.UTF-8 locale under the hood.
34  *
35  * We explicitly want to verify the following cases:
36  *
37  * + strerror() honors the global locale before uselocale
38  * + strerror() honors the per-thread locale
39  * + strerror_l() always reflects the chosen locale
40  */
41 
42 static int err = ENOENT;
43 static const char *en = "No such file or directory";
44 static const char *trans = "It's a trap!";
45 
46 static void
47 strerror_verify(const char *exp)
48 {
49 	const char *r;
50 	errno = 0;
51 	r = strerror(err);
52 	assert(errno == 0);
53 	assert(strcmp(r, exp) == 0);
54 }
55 
56 static void
57 strerror_l_verify(locale_t loc, const char *exp)
58 {
59 	const char *r;
60 	errno = 0;
61 	r = strerror_l(err, loc);
62 	assert(errno == 0);
63 	assert(strcmp(r, exp) == 0);
64 }
65 
66 int
67 main(void)
68 {
69 	locale_t loc;
70 
71 	(void) setlocale(LC_ALL, "C");
72 	strerror_verify(en);
73 
74 	(void) setlocale(LC_ALL, "zz_AA.UTF-8");
75 	strerror_verify(trans);
76 
77 	(void) setlocale(LC_MESSAGES, "C");
78 	strerror_verify(en);
79 
80 	(void) setlocale(LC_ALL, "C");
81 	loc = newlocale(LC_MESSAGES_MASK, "zz_AA.UTF-8", NULL);
82 	assert(loc != NULL);
83 
84 	strerror_verify(en);
85 	strerror_l_verify(NULL, en);
86 	strerror_l_verify(loc, trans);
87 
88 	(void) uselocale(loc);
89 	strerror_verify(trans);
90 	strerror_l_verify(NULL, trans);
91 	strerror_l_verify(loc, trans);
92 
93 	(void) uselocale(LC_GLOBAL_LOCALE);
94 	strerror_verify(en);
95 	strerror_l_verify(NULL, en);
96 	strerror_l_verify(loc, trans);
97 
98 	freelocale(loc);
99 	return (0);
100 }
101