Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / wctomb.c
blobf56dccf25aa87a751152d048a67036627111d0f2
1 /*
2 FUNCTION
3 <<wctomb>>---minimal wide char to multibyte converter
5 INDEX
6 wctomb
8 SYNOPSIS
9 #include <stdlib.h>
10 int wctomb(char *<[s]>, wchar_t <[wchar]>);
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.
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.
23 Each call to <<wctomb>> modifies <<*<[s]>>> unless <[s]> is a null
24 pointer or _MB_CAPABLE is defined and <[wchar]> is invalid.
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.
34 PORTABILITY
35 <<wctomb>> is required in the ANSI C standard. However, the precise
36 effects vary with the locale.
38 <<wctomb>> requires no supporting OS subroutines.
41 #ifndef _REENT_ONLY
43 #include <newlib.h>
44 #include <stdlib.h>
45 #include <errno.h>
46 #include "local.h"
48 #ifdef _REENT_THREAD_LOCAL
49 _Thread_local _mbstate_t _tls_wctomb_state;
50 #endif
52 int
53 wctomb (char *s,
54 wchar_t wchar)
56 #ifdef _MB_CAPABLE
57 struct _reent *reent = _REENT;
59 _REENT_CHECK_MISC(reent);
61 return __WCTOMB (reent, s, wchar, &(_REENT_WCTOMB_STATE(reent)));
62 #else /* not _MB_CAPABLE */
63 if (s == NULL)
64 return 0;
66 /* Verify that wchar is a valid single-byte character. */
67 if ((size_t)wchar >= 0x100) {
68 errno = EILSEQ;
69 return -1;
72 *s = (char) wchar;
73 return 1;
74 #endif /* not _MB_CAPABLE */
77 #endif /* !_REENT_ONLY */