From 57caa4e12121fa4af736f172c03dd09c6223c671 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Tue, 2 Jun 2020 12:03:59 +0800 Subject: [PATCH] libc: Move MB_LEN_MAX from lib_wctob.c to limits.h Signed-off-by: Xiang Xiao Change-Id: I3a5addac99adb02f57aba05aa9fe2e6aaf31071d --- include/limits.h | 10 ++++++++++ libs/libc/wchar/lib_wcrtomb.c | 10 +++++----- libs/libc/wchar/lib_wctob.c | 12 ++---------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/include/limits.h b/include/limits.h index 88c70ccccc..752a587e18 100644 --- a/include/limits.h +++ b/include/limits.h @@ -49,7 +49,9 @@ /******************************************************************************** * Pre-processor Definitions ********************************************************************************/ + /* Default values for user configurable limits **********************************/ + /* Maximum number of bytes in a filename (not including terminating null). */ #ifndef CONFIG_NAME_MAX @@ -68,6 +70,13 @@ # endif #endif +/* Maximum length of any multibyte character in any locale. + * We define this value here since the gcc header does not define + * the correct value. + */ + +#define MB_LEN_MAX 1 + /* Configurable limits required by POSIX **************************************** * * Required for all implementations: @@ -303,6 +312,7 @@ #define SEM_VALUE_MAX _POSIX_SEM_VALUE_MAX /* Required for readv() and writev() */ + /* There really is no upper limit on the number of vectors */ #define IOV_MAX INT_MAX diff --git a/libs/libc/wchar/lib_wcrtomb.c b/libs/libc/wchar/lib_wcrtomb.c index c514a6299b..d28c49be73 100644 --- a/libs/libc/wchar/lib_wcrtomb.c +++ b/libs/libc/wchar/lib_wcrtomb.c @@ -55,14 +55,14 @@ * ****************************************************************************/ -size_t wcrtomb(FAR char *s, wchar_t wc, FAR mbstate_t * ps) +size_t wcrtomb(FAR char *s, wchar_t wc, FAR mbstate_t *ps) { int retval = 0; - char buf[10]; + char buf[MB_LEN_MAX]; if (s == NULL) { - retval = wctomb((char *)buf, L'\0'); + retval = wctomb(buf, L'\0'); } else { @@ -71,11 +71,11 @@ size_t wcrtomb(FAR char *s, wchar_t wc, FAR mbstate_t * ps) if (retval == -1) { - return (size_t) (-1); + return (size_t)(-1); } else { - return (size_t) retval; + return (size_t)retval; } } #endif diff --git a/libs/libc/wchar/lib_wctob.c b/libs/libc/wchar/lib_wctob.c index d1079a0095..5329ff8cbb 100644 --- a/libs/libc/wchar/lib_wctob.c +++ b/libs/libc/wchar/lib_wctob.c @@ -44,27 +44,19 @@ #ifdef CONFIG_LIBC_WCHAR -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#ifndef MB_LEN_MAX -# define MB_LEN_MAX 8 -#endif - /**************************************************************************** * Public Functions ****************************************************************************/ int wctob(wint_t wc) { - unsigned char pmb[MB_LEN_MAX]; + char pmb[MB_LEN_MAX]; if (wc == WEOF) { return EOF; } - return wctomb((char *)pmb, wc) == 1 ? (int)pmb[0] : EOF; + return wctomb(pmb, wc) == 1 ? (int)pmb[0] : EOF; } #endif