1 | /*
|
---|
2 | $File: LEGAL.NOTICE,v 1.15 2006/05/03 18:48:33 christos Exp $
|
---|
3 | Copyright (c) Ian F. Darwin 1986, 1987, 1989, 1990, 1991, 1992, 1994, 1995.
|
---|
4 | Software written by Ian F. Darwin and others;
|
---|
5 | maintained 1994- Christos Zoulas.
|
---|
6 |
|
---|
7 | This software is not subject to any export provision of the United States
|
---|
8 | Department of Commerce, and may be exported to any country or planet.
|
---|
9 |
|
---|
10 | Redistribution and use in source and binary forms, with or without
|
---|
11 | modification, are permitted provided that the following conditions
|
---|
12 | are met:
|
---|
13 | 1. Redistributions of source code must retain the above copyright
|
---|
14 | notice immediately at the beginning of the file, without modification,
|
---|
15 | this list of conditions, and the following disclaimer.
|
---|
16 | 2. Redistributions in binary form must reproduce the above copyright
|
---|
17 | notice, this list of conditions and the following disclaimer in the
|
---|
18 | documentation and/or other materials provided with the distribution.
|
---|
19 |
|
---|
20 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
---|
21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
23 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
|
---|
24 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
26 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
27 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
28 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
29 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
30 | SUCH DAMAGE.
|
---|
31 | */
|
---|
32 |
|
---|
33 |
|
---|
34 |
|
---|
35 | // Uncomment these lines to make the test work
|
---|
36 |
|
---|
37 | // #ifdef __WCHAR_TYPE__
|
---|
38 | // #undef __WCHAR_TYPE__
|
---|
39 | // #define __WCHAR_TYPE__ unsigned int
|
---|
40 | // #endif
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <wchar.h>
|
---|
43 |
|
---|
44 | size_t
|
---|
45 | file_mbswidth(const char *s)
|
---|
46 | {
|
---|
47 | size_t bytesconsumed, old_n, n, width = 0;
|
---|
48 | mbstate_t state;
|
---|
49 | wchar_t nextchar;
|
---|
50 | (void)memset(&state, 0, sizeof(mbstate_t));
|
---|
51 | old_n = n = strlen(s);
|
---|
52 |
|
---|
53 | while (n > 0) {
|
---|
54 | bytesconsumed = mbrtowc(&nextchar, s, n, &state);
|
---|
55 | printf("mjw... in while %d\n", bytesconsumed);
|
---|
56 | if (bytesconsumed == (size_t)(-1) ||
|
---|
57 | bytesconsumed == (size_t)(-2)) {
|
---|
58 | /* Something went wrong, return something reasonable */
|
---|
59 | return old_n;
|
---|
60 | }
|
---|
61 | if (s[0] == '\n') {
|
---|
62 | /*
|
---|
63 | * do what strlen() would do, so that caller
|
---|
64 | * is always right
|
---|
65 | */
|
---|
66 | width++;
|
---|
67 | } else
|
---|
68 | width += wcwidth(nextchar);
|
---|
69 |
|
---|
70 | s += bytesconsumed, n -= bytesconsumed;
|
---|
71 | }
|
---|
72 | return width;
|
---|
73 | }
|
---|
74 |
|
---|
75 | int
|
---|
76 | main(int argc, char *argv[])
|
---|
77 | {
|
---|
78 | wchar_t t;
|
---|
79 | printf("%d, %d\n", sizeof(wchar_t), sizeof(t));
|
---|
80 | if (sizeof(wchar_t) != 4)
|
---|
81 | {
|
---|
82 | printf("Error: glibc expects sizeof(wchar_t) to be 4\n");
|
---|
83 | }
|
---|
84 | printf("%d\n", file_mbswidth("etc/magic"));
|
---|
85 |
|
---|
86 | return 0;
|
---|
87 | }
|
---|
88 |
|
---|