newlib-cygwin/newlib/libc/stdlib/wctomb.c

80 lines
2.0 KiB
C
Raw Normal View History

2000-02-18 03:39:52 +08:00
/*
FUNCTION
<<wctomb>>---minimal wide char to multibyte converter
INDEX
wctomb
ANSI_SYNOPSIS
#include <stdlib.h>
int wctomb(char *<[s]>, wchar_t <[wchar]>);
TRAD_SYNOPSIS
#include <stdlib.h>
int wctomb(<[s]>, <[wchar]>)
char *<[s]>;
wchar_t <[wchar]>;
DESCRIPTION
When _MB_CAPABLE is not defined, this is a minimal ANSI-conforming
2000-02-18 03:39:52 +08:00
implementation of <<wctomb>>. The
only ``wide characters'' recognized are single bytes,
and they are ``converted'' to themselves.
When _MB_CAPABLE is defined, this routine calls <<_wctomb_r>> to perform
2000-02-18 03:39:52 +08:00
the conversion, passing a state variable to allow state dependent
decoding. The result is based on the locale setting which may
be restricted to a defined set of locales.
Each call to <<wctomb>> modifies <<*<[s]>>> unless <[s]> is a null
pointer or _MB_CAPABLE is defined and <[wchar]> is invalid.
2000-02-18 03:39:52 +08:00
RETURNS
This implementation of <<wctomb>> returns <<0>> if
<[s]> is <<NULL>>; it returns <<-1>> if _MB_CAPABLE is enabled
2000-02-18 03:39:52 +08:00
and the wchar is not a valid multi-byte character, it returns <<1>>
if _MB_CAPABLE is not defined or the wchar is in reality a single
2000-02-18 03:39:52 +08:00
byte character, otherwise it returns the number of bytes in the
multi-byte character.
PORTABILITY
<<wctomb>> is required in the ANSI C standard. However, the precise
effects vary with the locale.
<<wctomb>> requires no supporting OS subroutines.
*/
#ifndef _REENT_ONLY
#include <newlib.h>
2000-02-18 03:39:52 +08:00
#include <stdlib.h>
#include <errno.h>
#include "local.h"
2000-02-18 03:39:52 +08:00
int
_DEFUN (wctomb, (s, wchar),
char *s _AND
wchar_t wchar)
{
#ifdef _MB_CAPABLE
_REENT_CHECK_MISC(_REENT);
2000-02-18 03:39:52 +08:00
return __wctomb (_REENT, s, wchar, __locale_charset (),
&(_REENT_WCTOMB_STATE(_REENT)));
#else /* not _MB_CAPABLE */
2000-02-18 03:39:52 +08:00
if (s == NULL)
return 0;
/* Verify that wchar is a valid single-byte character. */
if ((size_t)wchar >= 0x100) {
errno = EILSEQ;
return -1;
}
2000-02-18 03:39:52 +08:00
*s = (char) wchar;
return 1;
#endif /* not _MB_CAPABLE */
2000-02-18 03:39:52 +08:00
}
#endif /* !_REENT_ONLY */