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

79 lines
1.8 KiB
C
Raw Normal View History

2000-02-18 03:39:52 +08:00
/*
FUNCTION
<<mblen>>---minimal multibyte length function
INDEX
mblen
ANSI_SYNOPSIS
#include <stdlib.h>
int mblen(const char *<[s]>, size_t <[n]>);
TRAD_SYNOPSIS
#include <stdlib.h>
int mblen(<[s]>, <[n]>)
const char *<[s]>;
size_t <[n]>;
DESCRIPTION
When MB_CAPABLE is not defined, this is a minimal ANSI-conforming
implementation of <<mblen>>. In this case, the
only ``multi-byte character sequences'' recognized are single bytes,
and thus <<1>> is returned unless <[s]> is the null pointer or
has a length of 0 or is the empty string.
When MB_CAPABLE is defined, this routine calls <<_mbtowc_r>> to perform
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.
RETURNS
This implementation of <<mblen>> returns <<0>> if
<[s]> is <<NULL>> or the empty string; it returns <<1>> if not MB_CAPABLE or
the character is a single-byte character; it returns <<-1>>
if the multi-byte character is invalid; otherwise it returns
the number of bytes in the multibyte character.
PORTABILITY
<<mblen>> is required in the ANSI C standard. However, the precise
effects vary with the locale.
<<mblen>> requires no supporting OS subroutines.
*/
#ifndef _REENT_ONLY
#include <stdlib.h>
* libc/include/langinfo.h: New file. * libc/include/wchar.h: Likewise. * libc/include/sys/syslimits.h: Likewise. * libc/locale/fix_grouping.c: Likewise. * libc/locale/ldpart.c: Likewise. * libc/locale/ldpart.h: Likewise. * libc/locale/lmessages.c: Likewise. * libc/locale/lmessages.h: Likewise. * libc/locale/lmonetary.c: Likewise. * libc/locale/lmonetary.h: Likewise. * libc/locale/lnumeric.c: Likewise. * libc/locale/lnumeric.h: Likewise. * libc/locale/nl_langinfo.3: Likewise. * libc/locale/nl_langinfo.c: Likewise. * libc/locale/timelocal.c: Likewise. * libc/locale/timelocal.h: Likewise. * libc/stdlib/btowc.c: Likewise. * libc/stdlib/mbrlen.c: Likewise. * libc/stdlib/mbrtowc.c: Likewise. * libc/stdlib/mbsinit.c: Likewise. * libc/stdlib/mbsrtowcs.c: Likewise. * libc/stdlib/wcrtomb.c: Likewise. * libc/stdlib/wcsrtombs.c: Likewise. * libc/stdlib/wctob.c: Likewise. * libc/sys/linux/prof-freq.c: Likewise. * libc/sys/linux/profile.c: Likewise. * libc/sys/linux/machine/i386/dl-procinfo.c: Likewise. * libc/sys/linux/machine/i386/dl-procinfo.h: Likewise. * libc/include/stdlib.h: Change re-entrant functions to take mbstate_t pointers. * libc/include/sys/_types.h: Define _mbstate_t. * libc/include/sys/config.h (MB_LEN_MAX): New macro. * libc/include/sys/errno.h (EILSEQ): New error code. * libc/include/sys/reent.h: Include wchar.h. Change reentrant structure to use mbstate_t. * libc/locale/Makefile.am (LIB_SOURCES): Add new files. * libc/machine/powerpc/vfprintf.c: Use mbstate_t. * libc/machine/powerpc/vfscanf.c: Likewise. * libc/stdio/getdelim.c: Reallocate buffer only when necessary. * libc/stdio/vfprintf.c: Likewise. * libc/stdio/vfscanf.c: Likewise. * libc/stdlib/Makefile.am (LIB_SOURCES): Add new files. * libc/stdlib/mblen.c: Use mbstate_t. * libc/stdlib/mblen_r.c: Likewise. * libc/stdlib/mbstowcs.c: Likewise. * libc/stdlib/mbstowcs_r.c: Likewise. * libc/stdlib/mbtowc.c: Likewise. * libc/stdlib/mbtowc_r.c: Likewise. * libc/stdlib/wcstombs.c: Likewise. * libc/stdlib/wcstombs_r.c: Likewise. * libc/stdlib/wctomb_r.c: Likewise. * libc/sys/linux/Makefile.am (LIB_SOURCES): Add prof-freq.c and profile.c. * libc/sys/linux/machine/i386/Makefile.am (LIB_SOURCES): Add dl-procinfo.c. * libc/sys/linux/sys/errno.h (EILSEQ): New error code. * libc/sys/linux/sys/types.h (off_t): Define type. * testsuite/newlib.locale/UTF-8.c: Change locale name from UTF-8 to C-UTF-8. * testsuite/newlib.locale/UTF-8.exp: Likewise.
2002-08-23 09:56:05 +08:00
#include <wchar.h>
2000-02-18 03:39:52 +08:00
int
_DEFUN (mblen, (s, n),
const char *s _AND
size_t n)
{
#ifdef MB_CAPABLE
int retval = 0;
mbstate_t *state;
_REENT_CHECK_MISC(_REENT);
state = &(_REENT_MBLEN_STATE(_REENT));
retval = _mbtowc_r (_REENT, NULL, s, n, state);
if (retval < 0)
{
state->__count = 0;
return -1;
}
else
return retval;
2000-02-18 03:39:52 +08:00
#else /* not MB_CAPABLE */
if (s == NULL || *s == '\0')
return 0;
if (n == 0)
return -1;
return 1;
2000-02-18 03:39:52 +08:00
#endif /* not MB_CAPABLE */
}
#endif /* !_REENT_ONLY */