From 86489c71a7fa2376478672e3977719c7721a6d84 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Sat, 5 Jan 2013 01:05:48 +0200 Subject: [PATCH] wchar: allow missing mbsnrtowcs() --- m4/usual.m4 | 2 +- usual/wchar.c | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/m4/usual.m4 b/m4/usual.m4 index 734580b..7ae5de7 100644 --- a/m4/usual.m4 +++ b/m4/usual.m4 @@ -231,7 +231,7 @@ AC_CHECK_FUNCS(err errx warn warnx getprogname setprogname) AC_CHECK_FUNCS(posix_memalign memalign valloc) AC_CHECK_FUNCS(getopt getopt_long getopt_long_only) AC_CHECK_FUNCS(fls flsl flsll ffs ffsl ffsll) -AC_CHECK_FUNCS(fnmatch) +AC_CHECK_FUNCS(fnmatch mbsnrtowcs) ### Functions provided only on win32 AC_CHECK_FUNCS(localtime_r gettimeofday recvmsg sendmsg usleep getrusage) ### Functions used by libusual itself diff --git a/usual/wchar.c b/usual/wchar.c index f0d2afc..fbed966 100644 --- a/usual/wchar.c +++ b/usual/wchar.c @@ -24,7 +24,7 @@ wchar_t *mbstr_decode(const char *str, int str_len, int *wlen_p, wchar_t *wbuf, int wbuf_len, bool allow_invalid) { mbstate_t ps; - int clen, wcnt; + int clen; wchar_t *dst, *w, *wend; const char *s; const char *str_end; @@ -44,15 +44,17 @@ wchar_t *mbstr_decode(const char *str, int str_len, int *wlen_p, return NULL; } +#ifdef HAVE_MBSNRTOWCS /* try full decode at once */ s = str; memset(&ps, 0, sizeof(ps)); - wcnt = mbsnrtowcs(dst, &s, str_len, wmax, &ps); - if (wcnt > 0 && s == NULL) { + clen = mbsnrtowcs(dst, &s, str_len, wmax, &ps); + if (clen > 0 && s == NULL) { if (wlen_p) - *wlen_p = wcnt; + *wlen_p = clen; return dst; } +#endif /* full decode failed, decode chars one-by-one */ s = str; -- 2.39.5