Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / mblen.c
blob24df6151944926b4d3c1b46c11495982ff488c84
1 /*
2 FUNCTION
3 <<mblen>>---minimal multibyte length function
5 INDEX
6 mblen
8 SYNOPSIS
9 #include <stdlib.h>
10 int mblen(const char *<[s]>, size_t <[n]>);
12 DESCRIPTION
13 When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming
14 implementation of <<mblen>>. In this case, the
15 only ``multi-byte character sequences'' recognized are single bytes,
16 and thus <<1>> is returned unless <[s]> is the null pointer or
17 has a length of 0 or is the empty string.
19 When _MB_CAPABLE is defined, this routine calls <<_mbtowc_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 <<mblen>> returns <<0>> if
26 <[s]> is <<NULL>> or the empty string; it returns <<1>> if not _MB_CAPABLE or
27 the character is a single-byte character; it returns <<-1>>
28 if the multi-byte character is invalid; otherwise it returns
29 the number of bytes in the multibyte character.
31 PORTABILITY
32 <<mblen>> is required in the ANSI C standard. However, the precise
33 effects vary with the locale.
35 <<mblen>> requires no supporting OS subroutines.
38 #ifndef _REENT_ONLY
40 #include <newlib.h>
41 #include <stdlib.h>
42 #include <wchar.h>
43 #include "local.h"
45 #ifdef _REENT_THREAD_LOCAL
46 _Thread_local _mbstate_t _tls_mblen_state;
47 #endif
49 int
50 mblen (const char *s,
51 size_t n)
53 #ifdef _MB_CAPABLE
54 int retval = 0;
55 struct _reent *reent = _REENT;
56 mbstate_t *state;
58 _REENT_CHECK_MISC(reent);
59 state = &(_REENT_MBLEN_STATE(reent));
60 retval = __MBTOWC (reent, NULL, s, n, state);
61 if (retval < 0)
63 state->__count = 0;
64 return -1;
66 else
67 return retval;
69 #else /* not _MB_CAPABLE */
70 if (s == NULL || *s == '\0')
71 return 0;
72 if (n == 0)
73 return -1;
74 return 1;
75 #endif /* not _MB_CAPABLE */
78 #endif /* !_REENT_ONLY */