Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / mbstowcs.c
blob253059cf871279fbf181eecac6c8914689a9c4d0
1 /*
2 FUNCTION
3 <<mbstowcs>>---minimal multibyte string to wide char converter
5 INDEX
6 mbstowcs
8 SYNOPSIS
9 #include <stdlib.h>
10 int mbstowcs(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 <<mbstowcs>>. In this case, the
15 only ``multi-byte character sequences'' recognized are single bytes,
16 and they are ``converted'' to wide-char versions simply by byte
17 extension.
19 When _MB_CAPABLE is defined, this routine calls <<_mbstowcs_r>> to perform
20 the conversion, passing a state variable to allow state dependent
21 decoding. The result is based on the locale setting which may
22 be restricted to a defined set of locales.
24 RETURNS
25 This implementation of <<mbstowcs>> returns <<0>> if
26 <[s]> is <<NULL>> or is the empty string;
27 it returns <<-1>> if _MB_CAPABLE and one of the
28 multi-byte characters is invalid or incomplete;
29 otherwise it returns the minimum of: <<n>> or the
30 number of multi-byte characters in <<s>> plus 1 (to
31 compensate for the nul character).
32 If the return value is -1, the state of the <<pwc>> string is
33 indeterminate. If the input has a length of 0, the output
34 string will be modified to contain a wchar_t nul terminator.
36 PORTABILITY
37 <<mbstowcs>> is required in the ANSI C standard. However, the precise
38 effects vary with the locale.
40 <<mbstowcs>> requires no supporting OS subroutines.
43 #ifndef _REENT_ONLY
45 #include <newlib.h>
46 #include <stdlib.h>
47 #include <wchar.h>
49 size_t
50 mbstowcs (wchar_t *__restrict pwcs,
51 const char *__restrict s,
52 size_t n)
54 #ifdef _MB_CAPABLE
55 mbstate_t state;
56 state.__count = 0;
58 return _mbstowcs_r (_REENT, pwcs, s, n, &state);
59 #else /* not _MB_CAPABLE */
61 int count = 0;
63 if (n != 0) {
64 do {
65 if ((*pwcs++ = (wchar_t) *s++) == 0)
66 break;
67 count++;
68 } while (--n != 0);
71 return count;
72 #endif /* not _MB_CAPABLE */
75 #endif /* !_REENT_ONLY */