1 /*
2 Copyright (c) 1990 Regents of the University of California.
3 All rights reserved.
4 */
5 /*
6 FUNCTION
7 <<mblen>>---minimal multibyte length function
8
9 INDEX
10 mblen
11
12 SYNOPSIS
13 #include <stdlib.h>
14 int mblen(const char *<[s]>, size_t <[n]>);
15
16 DESCRIPTION
17 When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming
18 implementation of <<mblen>>. In this case, the
19 only ``multi-byte character sequences'' recognized are single bytes,
20 and thus <<1>> is returned unless <[s]> is the null pointer or
21 has a length of 0 or is the empty string.
22
23 When _MB_CAPABLE is defined, this routine calls <<__MBTOWC>> to perform
24 the conversion, passing a state variable to allow state dependent
25 decoding. The result is based on the locale setting which may
26 be restricted to a defined set of locales.
27
28 RETURNS
29 This implementation of <<mblen>> returns <<0>> if
30 <[s]> is <<NULL>> or the empty string; it returns <<1>> if not _MB_CAPABLE or
31 the character is a single-byte character; it returns <<-1>>
32 if the multi-byte character is invalid; otherwise it returns
33 the number of bytes in the multibyte character.
34
35 PORTABILITY
36 <<mblen>> is required in the ANSI C standard. However, the precise
37 effects vary with the locale.
38
39 <<mblen>> requires no supporting OS subroutines.
40 */
41
42 #ifndef _REENT_ONLY
43
44 #include <newlib.h>
45 #include <stdlib.h>
46 #include <wchar.h>
47 #include "local.h"
48
49 int
mblen(const char * s,size_t n)50 mblen (const char *s,
51 size_t n)
52 {
53 #ifdef _MB_CAPABLE
54 int retval = 0;
55 static NEWLIB_THREAD_LOCAL _mbstate_t _mblen_state;
56
57 retval = __MBTOWC (NULL, s, n, &_mblen_state);
58 if (retval < 0)
59 {
60 _mblen_state.__count = 0;
61 return -1;
62 }
63 else
64 return retval;
65
66 #else /* not _MB_CAPABLE */
67 if (s == NULL || *s == '\0')
68 return 0;
69 if (n == 0)
70 return -1;
71 return 1;
72 #endif /* not _MB_CAPABLE */
73 }
74
75 #endif /* !_REENT_ONLY */
76