xref: /netbsd-src/external/bsd/mdocml/dist/test-wchar.c (revision 544c191c349c1704c9d5e679d12ec15cff579663)
1*544c191cSchristos /*	Id: test-wchar.c,v 1.5 2018/08/15 02:15:52 schwarze Exp 	*/
2fec65c98Schristos /*
3fec65c98Schristos  * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
4fec65c98Schristos  *
5fec65c98Schristos  * Permission to use, copy, modify, and distribute this software for any
6fec65c98Schristos  * purpose with or without fee is hereby granted, provided that the above
7fec65c98Schristos  * copyright notice and this permission notice appear in all copies.
8fec65c98Schristos  *
9fec65c98Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10fec65c98Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11fec65c98Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12fec65c98Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13fec65c98Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14fec65c98Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15fec65c98Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16fec65c98Schristos  */
17fec65c98Schristos 
18fec65c98Schristos #include <locale.h>
19fec65c98Schristos #include <stdio.h>
20fec65c98Schristos #include <wchar.h>
21fec65c98Schristos #include <unistd.h>
22fec65c98Schristos 
23fec65c98Schristos int
main(void)24fec65c98Schristos main(void)
25fec65c98Schristos {
26fec65c98Schristos 	wchar_t	 wc;
27fec65c98Schristos 	int	 width;
28fec65c98Schristos 
29fec65c98Schristos 	if (setlocale(LC_ALL, "") == NULL) {
30fec65c98Schristos 		fputs("setlocale(LC_ALL, \"\") failed\n", stderr);
319ff1f2acSchristos 		return 1;
32fec65c98Schristos 	}
33fec65c98Schristos 
349508192eSchristos 	if (setlocale(LC_CTYPE, UTF8_LOCALE) == NULL) {
359508192eSchristos 		fprintf(stderr, "setlocale(LC_CTYPE, \"%s\") failed\n",
369508192eSchristos 		    UTF8_LOCALE);
379ff1f2acSchristos 		return 1;
38fec65c98Schristos 	}
39fec65c98Schristos 
40fec65c98Schristos 	if (sizeof(wchar_t) < 4) {
41fec65c98Schristos 		fprintf(stderr, "wchar_t is only %zu bytes\n",
42fec65c98Schristos 		    sizeof(wchar_t));
439ff1f2acSchristos 		return 1;
44fec65c98Schristos 	}
45fec65c98Schristos 
46fec65c98Schristos 	if ((width = wcwidth(L' ')) != 1) {
47fec65c98Schristos 		fprintf(stderr, "wcwidth(L' ') returned %d\n", width);
489ff1f2acSchristos 		return 1;
49fec65c98Schristos 	}
50fec65c98Schristos 
51fec65c98Schristos 	dup2(STDERR_FILENO, STDOUT_FILENO);
52fec65c98Schristos 	wc = L'*';
53fec65c98Schristos 	if (putwchar(wc) != (wint_t)wc) {
54fec65c98Schristos 		fputs("bad putwchar return value\n", stderr);
559ff1f2acSchristos 		return 1;
56fec65c98Schristos 	}
57fec65c98Schristos 
589ff1f2acSchristos 	return 0;
59fec65c98Schristos }
60