1 | /* |
---|
2 | FUNCTION |
---|
3 | <<wctomb>>---minimal wide char to multibyte converter |
---|
4 | |
---|
5 | INDEX |
---|
6 | wctomb |
---|
7 | |
---|
8 | SYNOPSIS |
---|
9 | #include <stdlib.h> |
---|
10 | int wctomb(char *<[s]>, wchar_t <[wchar]>); |
---|
11 | |
---|
12 | DESCRIPTION |
---|
13 | When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming |
---|
14 | implementation of <<wctomb>>. The |
---|
15 | only ``wide characters'' recognized are single bytes, |
---|
16 | and they are ``converted'' to themselves. |
---|
17 | |
---|
18 | When _MB_CAPABLE is defined, this routine calls <<_wctomb_r>> to perform |
---|
19 | the conversion, passing a state variable to allow state dependent |
---|
20 | decoding. The result is based on the locale setting which may |
---|
21 | be restricted to a defined set of locales. |
---|
22 | |
---|
23 | Each call to <<wctomb>> modifies <<*<[s]>>> unless <[s]> is a null |
---|
24 | pointer or _MB_CAPABLE is defined and <[wchar]> is invalid. |
---|
25 | |
---|
26 | RETURNS |
---|
27 | This implementation of <<wctomb>> returns <<0>> if |
---|
28 | <[s]> is <<NULL>>; it returns <<-1>> if _MB_CAPABLE is enabled |
---|
29 | and the wchar is not a valid multi-byte character, it returns <<1>> |
---|
30 | if _MB_CAPABLE is not defined or the wchar is in reality a single |
---|
31 | byte character, otherwise it returns the number of bytes in the |
---|
32 | multi-byte character. |
---|
33 | |
---|
34 | PORTABILITY |
---|
35 | <<wctomb>> is required in the ANSI C standard. However, the precise |
---|
36 | effects vary with the locale. |
---|
37 | |
---|
38 | <<wctomb>> requires no supporting OS subroutines. |
---|
39 | */ |
---|
40 | |
---|
41 | #ifndef _REENT_ONLY |
---|
42 | |
---|
43 | #include <newlib.h> |
---|
44 | #include <stdlib.h> |
---|
45 | #include <errno.h> |
---|
46 | #include "local.h" |
---|
47 | |
---|
48 | int |
---|
49 | wctomb (char *s, |
---|
50 | wchar_t wchar) |
---|
51 | { |
---|
52 | #ifdef _MB_CAPABLE |
---|
53 | struct _reent *reent = _REENT; |
---|
54 | |
---|
55 | _REENT_CHECK_MISC(reent); |
---|
56 | |
---|
57 | return __WCTOMB (reent, s, wchar, &(_REENT_WCTOMB_STATE(reent))); |
---|
58 | #else /* not _MB_CAPABLE */ |
---|
59 | if (s == NULL) |
---|
60 | return 0; |
---|
61 | |
---|
62 | /* Verify that wchar is a valid single-byte character. */ |
---|
63 | if ((size_t)wchar >= 0x100) { |
---|
64 | errno = EILSEQ; |
---|
65 | return -1; |
---|
66 | } |
---|
67 | |
---|
68 | *s = (char) wchar; |
---|
69 | return 1; |
---|
70 | #endif /* not _MB_CAPABLE */ |
---|
71 | } |
---|
72 | |
---|
73 | #endif /* !_REENT_ONLY */ |
---|