source: trunk/libs/newlib/src/newlib/libc/stdlib/mblen.c @ 620

Last change on this file since 620 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

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