source: trunk/libs/newlib/src/newlib/libc/stdlib/mbtowc.c @ 471

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

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

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