16449 Want smbios additional information (Type 40) decoding
[illumos-gate.git] / usr / src / test / libc-tests / tests / strerror / strerror.c
blob78dce8e04fe77b07c3780e07c67b2ba5b7695429
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.
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.
13 * Copyright 2015 Joyent, Inc.
16 #include <string.h>
17 #include <locale.h>
18 #include <assert.h>
19 #include <errno.h>
20 #include <stdio.h>
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.
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:
31 * "No such file or directory" -> "It's a trap!"
33 * It's otherwise a boring en_US.UTF-8 locale under the hood.
35 * We explicitly want to verify the following cases:
37 * + strerror() honors the global locale before uselocale
38 * + strerror() honors the per-thread locale
39 * + strerror_l() always reflects the chosen locale
42 static int err = ENOENT;
43 static const char *en = "No such file or directory";
44 static const char *trans = "It's a trap!";
46 static void
47 strerror_verify(const char *exp)
49 const char *r;
50 errno = 0;
51 r = strerror(err);
52 assert(errno == 0);
53 assert(strcmp(r, exp) == 0);
56 static void
57 strerror_l_verify(locale_t loc, const char *exp)
59 const char *r;
60 errno = 0;
61 r = strerror_l(err, loc);
62 assert(errno == 0);
63 assert(strcmp(r, exp) == 0);
66 int
67 main(void)
69 locale_t loc;
71 (void) setlocale(LC_ALL, "C");
72 strerror_verify(en);
74 (void) setlocale(LC_ALL, "zz_AA.UTF-8");
75 strerror_verify(trans);
77 (void) setlocale(LC_MESSAGES, "C");
78 strerror_verify(en);
80 (void) setlocale(LC_ALL, "C");
81 loc = newlocale(LC_MESSAGES_MASK, "zz_AA.UTF-8", NULL);
82 assert(loc != NULL);
84 strerror_verify(en);
85 strerror_l_verify(NULL, en);
86 strerror_l_verify(loc, trans);
88 (void) uselocale(loc);
89 strerror_verify(trans);
90 strerror_l_verify(NULL, trans);
91 strerror_l_verify(loc, trans);
93 (void) uselocale(LC_GLOBAL_LOCALE);
94 strerror_verify(en);
95 strerror_l_verify(NULL, en);
96 strerror_l_verify(loc, trans);
98 freelocale(loc);
99 return (0);