]> diplodocus.org Git - nmh/blob - test/getcwidth.c
Update manpages to use .TP for tagged paragraphs (part I).
[nmh] / test / getcwidth.c
1 /*
2 * getcwidth - Get the OS's idea of the width of a combining character
3 *
4 * This code is Copyright (c) 2012, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13
14 #ifdef HAVE_CONFIG_H
15 #include <config.h>
16 #endif
17
18 #ifdef MULTIBYTE_SUPPORT
19 #include <locale.h>
20 #include <wchar.h>
21 #endif
22
23 int
24 main(int argc, char *argv[])
25 {
26 wchar_t c;
27 int charlen;
28 char *p;
29
30 /*
31 * This is the UTF-8 for "n" + U+0308 (Combining Diaeresis)
32 */
33
34 unsigned char string[] = "n\xcc\x88";
35
36 setlocale(LC_ALL, "");
37
38 if (argc != 1) {
39 fprintf(stderr, "Usage: %s\n", argv[0]);
40 fprintf(stderr, "Returns the column width of a UTF-8 "
41 "multibyte character\n");
42 exit(1);
43 }
44
45 #ifndef MULTIBYTE_SUPPORT
46 fprintf(stderr, "Nmh was not configured with multibyte support\n");
47 exit(1);
48 #else
49 /*
50 * It's not clear to me that we can just call mbtowc() with a
51 * combining character; just to be safe, feed it in a base
52 * character first.
53 */
54
55 mbtowc(NULL, NULL, 0);
56
57 charlen = mbtowc(&c, (char *) string, strlen((char *) string));
58
59 if (charlen != 1) {
60 fprintf(stderr, "We expected a beginning character length "
61 "of 1, got %d instead\n", charlen);
62 exit(1);
63 }
64
65 p = (char *) (string + charlen);
66
67 charlen = mbtowc(&c, p, strlen(p));
68
69 if (charlen != 2) {
70 fprintf(stderr, "We expected a multibyte character length "
71 "of 2, got %d instead\n", charlen);
72 fprintf(stderr, "Are you using a UTF-8 locale?\n");
73 exit(1);
74 }
75
76 printf("%d\n", wcwidth(c));
77
78 exit(0);
79 #endif /* MULTIBYTE_SUPPORT */
80 }