xref: /illumos-gate/usr/src/test/libc-tests/tests/catopen/catopen.c (revision 66582b606a8194f7f3ba5b3a3a6dca5b0d346361)
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 #include <nl_types.h>
22 #include <sys/types.h>
23 
24 /*
25  * This is designed to check that we properly are honoring the global and
26  * per-thread locale when opening up a message catalog. To do this, we use the
27  * "TEST" message catalog which only exists in the system in the C/POSIX
28  * locales and thus alternate with our test locale zz_AA.UTF-8 which should not
29  * have it.
30  */
31 
32 #define	INVALID_CAT	((nl_catd)-1)
33 
34 static void
35 catopen_verify(boolean_t find)
36 {
37 	nl_catd cat;
38 
39 	cat = catopen("TEST", NL_CAT_LOCALE);
40 	if (find == B_TRUE) {
41 		assert(cat != INVALID_CAT);
42 		(void) catclose(cat);
43 	} else {
44 		assert(cat == INVALID_CAT);
45 	}
46 }
47 
48 int
49 main(void)
50 {
51 	locale_t loc;
52 
53 	(void) setlocale(LC_ALL, "C");
54 	catopen_verify(B_TRUE);
55 
56 	(void) setlocale(LC_ALL, "zz_AA.UTF-8");
57 	catopen_verify(B_FALSE);
58 
59 	(void) setlocale(LC_MESSAGES, "C");
60 	catopen_verify(B_TRUE);
61 
62 	(void) setlocale(LC_ALL, "C");
63 	loc = newlocale(LC_MESSAGES_MASK, "zz_AA.UTF-8", NULL);
64 	assert(loc != NULL);
65 
66 	catopen_verify(B_TRUE);
67 	(void) uselocale(loc);
68 	catopen_verify(B_FALSE);
69 
70 	(void) uselocale(LC_GLOBAL_LOCALE);
71 	catopen_verify(B_TRUE);
72 	freelocale(loc);
73 
74 	(void) setlocale(LC_ALL, "zz_AA.UTF-8");
75 	catopen_verify(B_FALSE);
76 
77 	loc = newlocale(LC_MESSAGES_MASK, "C", NULL);
78 	assert(loc != NULL);
79 
80 	catopen_verify(B_FALSE);
81 	(void) uselocale(loc);
82 	catopen_verify(B_TRUE);
83 
84 	return (0);
85 }
86