Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / mbtowc.c
blobfbd8df61de971cefc7495384c75fd73f1377c900
1 /*
2 FUNCTION
3 <<mbtowc>>---minimal multibyte to wide char converter
5 INDEX
6 mbtowc
8 SYNOPSIS
9 #include <stdlib.h>
10 int mbtowc(wchar_t *restrict <[pwc]>, const char *restrict <[s]>, size_t <[n]>);
12 DESCRIPTION
13 When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming
14 implementation of <<mbtowc>>. In this case,
15 only ``multi-byte character sequences'' recognized are single bytes,
16 and they are ``converted'' to themselves.
17 Each call to <<mbtowc>> copies one character from <<*<[s]>>> to
18 <<*<[pwc]>>>, unless <[s]> is a null pointer. The argument n
19 is ignored.
21 When _MB_CAPABLE is defined, this routine calls <<_mbtowc_r>> to perform
22 the conversion, passing a state variable to allow state dependent
23 decoding. The result is based on the locale setting which may
24 be restricted to a defined set of locales.
26 RETURNS
27 This implementation of <<mbtowc>> returns <<0>> if
28 <[s]> is <<NULL>> or is the empty string;
29 it returns <<1>> if not _MB_CAPABLE or
30 the character is a single-byte character; it returns <<-1>>
31 if n is <<0>> or the multi-byte character is invalid;
32 otherwise it returns the number of bytes in the multibyte character.
33 If the return value is -1, no changes are made to the <<pwc>>
34 output string. If the input is the empty string, a wchar_t nul
35 is placed in the output string and 0 is returned. If the input
36 has a length of 0, no changes are made to the <<pwc>> output string.
38 PORTABILITY
39 <<mbtowc>> is required in the ANSI C standard. However, the precise
40 effects vary with the locale.
42 <<mbtowc>> requires no supporting OS subroutines.
45 #ifndef _REENT_ONLY
47 #include <newlib.h>
48 #include <stdlib.h>
49 #include <wchar.h>
50 #include "local.h"
52 #ifdef _REENT_THREAD_LOCAL
53 _Thread_local _mbstate_t _tls_mbtowc_state;
54 #endif
56 int
57 mbtowc (wchar_t *__restrict pwc,
58 const char *__restrict s,
59 size_t n)
61 #ifdef _MB_CAPABLE
62 int retval = 0;
63 struct _reent *reent = _REENT;
64 mbstate_t *ps;
66 _REENT_CHECK_MISC(reent);
67 ps = &(_REENT_MBTOWC_STATE(reent));
69 retval = __MBTOWC (reent, pwc, s, n, ps);
71 if (retval < 0)
73 ps->__count = 0;
74 return -1;
76 return retval;
77 #else /* not _MB_CAPABLE */
78 if (s == NULL)
79 return 0;
80 if (n == 0)
81 return -1;
82 if (pwc)
83 *pwc = (wchar_t) *s;
84 return (*s != '\0');
85 #endif /* not _MB_CAPABLE */
88 #endif /* !_REENT_ONLY */