1*b599bd93SRobert Mustacchi /*
2*b599bd93SRobert Mustacchi  * This file and its contents are supplied under the terms of the
3*b599bd93SRobert Mustacchi  * Common Development and Distribution License ("CDDL"), version 1.0.
4*b599bd93SRobert Mustacchi  * You may only use this file in accordance with the terms of version
5*b599bd93SRobert Mustacchi  * 1.0 of the CDDL.
6*b599bd93SRobert Mustacchi  *
7*b599bd93SRobert Mustacchi  * A full copy of the text of the CDDL should have accompanied this
8*b599bd93SRobert Mustacchi  * source.  A copy of the CDDL is also available via the Internet at
9*b599bd93SRobert Mustacchi  * http://www.illumos.org/license/CDDL.
10*b599bd93SRobert Mustacchi  */
11*b599bd93SRobert Mustacchi 
12*b599bd93SRobert Mustacchi /*
13*b599bd93SRobert Mustacchi  * Copyright 2015 Joyent, Inc.
14*b599bd93SRobert Mustacchi  */
15*b599bd93SRobert Mustacchi 
16*b599bd93SRobert Mustacchi #include <string.h>
17*b599bd93SRobert Mustacchi #include <locale.h>
18*b599bd93SRobert Mustacchi #include <assert.h>
19*b599bd93SRobert Mustacchi #include <errno.h>
20*b599bd93SRobert Mustacchi #include <stdio.h>
21*b599bd93SRobert Mustacchi #include <nl_types.h>
22*b599bd93SRobert Mustacchi #include <sys/types.h>
23*b599bd93SRobert Mustacchi 
24*b599bd93SRobert Mustacchi /*
25*b599bd93SRobert Mustacchi  * This is designed to check that we properly are honoring the global and
26*b599bd93SRobert Mustacchi  * per-thread locale when opening up a message catalog. To do this, we use the
27*b599bd93SRobert Mustacchi  * "TEST" message catalog which only exists in the system in the C/POSIX
28*b599bd93SRobert Mustacchi  * locales and thus alternate with our test locale zz_AA.UTF-8 which should not
29*b599bd93SRobert Mustacchi  * have it.
30*b599bd93SRobert Mustacchi  */
31*b599bd93SRobert Mustacchi 
32*b599bd93SRobert Mustacchi #define	INVALID_CAT	((nl_catd)-1)
33*b599bd93SRobert Mustacchi 
34*b599bd93SRobert Mustacchi static void
catopen_verify(boolean_t find)35*b599bd93SRobert Mustacchi catopen_verify(boolean_t find)
36*b599bd93SRobert Mustacchi {
37*b599bd93SRobert Mustacchi 	nl_catd cat;
38*b599bd93SRobert Mustacchi 
39*b599bd93SRobert Mustacchi 	cat = catopen("TEST", NL_CAT_LOCALE);
40*b599bd93SRobert Mustacchi 	if (find == B_TRUE) {
41*b599bd93SRobert Mustacchi 		assert(cat != INVALID_CAT);
42*b599bd93SRobert Mustacchi 		(void) catclose(cat);
43*b599bd93SRobert Mustacchi 	} else {
44*b599bd93SRobert Mustacchi 		assert(cat == INVALID_CAT);
45*b599bd93SRobert Mustacchi 	}
46*b599bd93SRobert Mustacchi }
47*b599bd93SRobert Mustacchi 
48*b599bd93SRobert Mustacchi int
main(void)49*b599bd93SRobert Mustacchi main(void)
50*b599bd93SRobert Mustacchi {
51*b599bd93SRobert Mustacchi 	locale_t loc;
52*b599bd93SRobert Mustacchi 
53*b599bd93SRobert Mustacchi 	(void) setlocale(LC_ALL, "C");
54*b599bd93SRobert Mustacchi 	catopen_verify(B_TRUE);
55*b599bd93SRobert Mustacchi 
56*b599bd93SRobert Mustacchi 	(void) setlocale(LC_ALL, "zz_AA.UTF-8");
57*b599bd93SRobert Mustacchi 	catopen_verify(B_FALSE);
58*b599bd93SRobert Mustacchi 
59*b599bd93SRobert Mustacchi 	(void) setlocale(LC_MESSAGES, "C");
60*b599bd93SRobert Mustacchi 	catopen_verify(B_TRUE);
61*b599bd93SRobert Mustacchi 
62*b599bd93SRobert Mustacchi 	(void) setlocale(LC_ALL, "C");
63*b599bd93SRobert Mustacchi 	loc = newlocale(LC_MESSAGES_MASK, "zz_AA.UTF-8", NULL);
64*b599bd93SRobert Mustacchi 	assert(loc != NULL);
65*b599bd93SRobert Mustacchi 
66*b599bd93SRobert Mustacchi 	catopen_verify(B_TRUE);
67*b599bd93SRobert Mustacchi 	(void) uselocale(loc);
68*b599bd93SRobert Mustacchi 	catopen_verify(B_FALSE);
69*b599bd93SRobert Mustacchi 
70*b599bd93SRobert Mustacchi 	(void) uselocale(LC_GLOBAL_LOCALE);
71*b599bd93SRobert Mustacchi 	catopen_verify(B_TRUE);
72*b599bd93SRobert Mustacchi 	freelocale(loc);
73*b599bd93SRobert Mustacchi 
74*b599bd93SRobert Mustacchi 	(void) setlocale(LC_ALL, "zz_AA.UTF-8");
75*b599bd93SRobert Mustacchi 	catopen_verify(B_FALSE);
76*b599bd93SRobert Mustacchi 
77*b599bd93SRobert Mustacchi 	loc = newlocale(LC_MESSAGES_MASK, "C", NULL);
78*b599bd93SRobert Mustacchi 	assert(loc != NULL);
79*b599bd93SRobert Mustacchi 
80*b599bd93SRobert Mustacchi 	catopen_verify(B_FALSE);
81*b599bd93SRobert Mustacchi 	(void) uselocale(loc);
82*b599bd93SRobert Mustacchi 	catopen_verify(B_TRUE);
83*b599bd93SRobert Mustacchi 
84*b599bd93SRobert Mustacchi 	return (0);
85*b599bd93SRobert Mustacchi }
86