source: trunk/libs/newlib/src/newlib/libc/stdlib/wcstombs.c @ 444

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

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

File size: 1.9 KB
Line 
1/*
2FUNCTION
3<<wcstombs>>---minimal wide char string to multibyte string converter
4
5INDEX
6        wcstombs
7
8SYNOPSIS
9        #include <stdlib.h>
10        size_t wcstombs(char *restrict <[s]>, const wchar_t *restrict <[pwc]>, size_t <[n]>);
11
12DESCRIPTION
13When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming
14implementation of <<wcstombs>>.  In this case,
15all wide-characters are expected to represent single bytes and so
16are converted simply by casting to char.
17
18When _MB_CAPABLE is defined, this routine calls <<_wcstombs_r>> to perform
19the conversion, passing a state variable to allow state dependent
20decoding.  The result is based on the locale setting which may
21be restricted to a defined set of locales.
22
23RETURNS
24This implementation of <<wcstombs>> returns <<0>> if
25<[s]> is <<NULL>> or is the empty string;
26it returns <<-1>> if _MB_CAPABLE and one of the
27wide-char characters does not represent a valid multi-byte character;
28otherwise it returns the minimum of: <<n>> or the
29number of bytes that are transferred to <<s>>, not including the
30nul terminator.
31
32If the return value is -1, the state of the <<pwc>> string is
33indeterminate.  If the input has a length of 0, the output
34string will be modified to contain a wchar_t nul terminator if
35<<n>> > 0.
36
37PORTABILITY
38<<wcstombs>> is required in the ANSI C standard.  However, the precise
39effects vary with the locale.
40
41<<wcstombs>> requires no supporting OS subroutines.
42*/
43
44#ifndef _REENT_ONLY
45
46#include <newlib.h>
47#include <stdlib.h>
48#include <wchar.h>
49
50size_t
51wcstombs (char          *__restrict s,
52        const wchar_t *__restrict pwcs,
53        size_t         n)
54{
55#ifdef _MB_CAPABLE
56  mbstate_t state;
57  state.__count = 0;
58 
59  return _wcstombs_r (_REENT, s, pwcs, n, &state);
60#else /* not _MB_CAPABLE */
61  int count = 0;
62 
63  if (n != 0) {
64    do {
65      if ((*s++ = (char) *pwcs++) == 0)
66        break;
67      count++;
68    } while (--n != 0);
69  }
70 
71  return count;
72#endif /* not _MB_CAPABLE */
73}
74
75#endif /* !_REENT_ONLY */
Note: See TracBrowser for help on using the repository browser.