source: trunk/libs/newlib/src/newlib/libc/stdlib/wctomb.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.8 KB
Line 
1/*
2FUNCTION
3<<wctomb>>---minimal wide char to multibyte converter
4
5INDEX
6        wctomb
7
8SYNOPSIS
9        #include <stdlib.h>
10        int wctomb(char *<[s]>, wchar_t <[wchar]>);
11
12DESCRIPTION
13When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming
14implementation of <<wctomb>>.  The
15only ``wide characters'' recognized are single bytes,
16and they are ``converted'' to themselves. 
17
18When _MB_CAPABLE is defined, this routine calls <<_wctomb_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
23Each call to <<wctomb>> modifies <<*<[s]>>> unless <[s]> is a null
24pointer or _MB_CAPABLE is defined and <[wchar]> is invalid.
25
26RETURNS
27This implementation of <<wctomb>> returns <<0>> if
28<[s]> is <<NULL>>; it returns <<-1>> if _MB_CAPABLE is enabled
29and the wchar is not a valid multi-byte character, it returns <<1>>
30if _MB_CAPABLE is not defined or the wchar is in reality a single
31byte character, otherwise it returns the number of bytes in the
32multi-byte character.
33
34PORTABILITY
35<<wctomb>> is required in the ANSI C standard.  However, the precise
36effects vary with the locale.
37
38<<wctomb>> requires no supporting OS subroutines.
39*/
40
41#ifndef _REENT_ONLY
42
43#include <newlib.h>
44#include <stdlib.h>
45#include <errno.h>
46#include "local.h"
47
48int
49wctomb (char *s,
50        wchar_t wchar)
51{
52#ifdef _MB_CAPABLE
53        struct _reent *reent = _REENT;
54
55        _REENT_CHECK_MISC(reent);
56
57        return __WCTOMB (reent, s, wchar, &(_REENT_WCTOMB_STATE(reent)));
58#else /* not _MB_CAPABLE */
59        if (s == NULL)
60                return 0;
61
62        /* Verify that wchar is a valid single-byte character.  */
63        if ((size_t)wchar >= 0x100) {
64                errno = EILSEQ;
65                return -1;
66        }
67
68        *s = (char) wchar;
69        return 1;
70#endif /* not _MB_CAPABLE */
71}
72
73#endif /* !_REENT_ONLY */
Note: See TracBrowser for help on using the repository browser.