From a9b285775417b384c0e02ef4dbe2b2ff62d521d5 Mon Sep 17 00:00:00 2001 From: guoshichao Date: Sun, 17 Sep 2023 16:02:35 +0800 Subject: [PATCH] libs/libc/wchar: add putwchar implementation Signed-off-by: guoshichao --- libs/libc/stdio/CMakeLists.txt | 3 +- libs/libc/stdio/Make.defs | 2 +- libs/libc/stdio/lib_putwchar.c | 69 ++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 libs/libc/stdio/lib_putwchar.c diff --git a/libs/libc/stdio/CMakeLists.txt b/libs/libc/stdio/CMakeLists.txt index d454537bf2..b89cc148da 100644 --- a/libs/libc/stdio/CMakeLists.txt +++ b/libs/libc/stdio/CMakeLists.txt @@ -44,7 +44,8 @@ set(SRCS lib_remove.c lib_tempnam.c lib_tmpnam.c - lib_ultoa_invert.c) + lib_ultoa_invert.c + lib_putwchar.c) if(CONFIG_LIBC_FLOATINGPOINT) list(APPEND SRCS lib_dtoa_engine.c lib_dtoa_data.c) diff --git a/libs/libc/stdio/Make.defs b/libs/libc/stdio/Make.defs index 02fcb15264..c40252eefe 100644 --- a/libs/libc/stdio/Make.defs +++ b/libs/libc/stdio/Make.defs @@ -28,7 +28,7 @@ CSRCS += lib_perror.c lib_putchar.c lib_getchar.c lib_puts.c CSRCS += lib_gets_s.c lib_gets.c lib_libdgets.c CSRCS += lib_sscanf.c lib_vsscanf.c lib_libvscanf.c lib_libvsprintf.c CSRCS += lib_remove.c lib_tempnam.c lib_tmpnam.c lib_ultoa_invert.c -CSRCS += lib_renameat.c +CSRCS += lib_renameat.c lib_putwchar.c ifeq ($(CONFIG_LIBC_FLOATINGPOINT),y) CSRCS += lib_dtoa_engine.c lib_dtoa_data.c diff --git a/libs/libc/stdio/lib_putwchar.c b/libs/libc/stdio/lib_putwchar.c new file mode 100644 index 0000000000..6906cb8200 --- /dev/null +++ b/libs/libc/stdio/lib_putwchar.c @@ -0,0 +1,69 @@ +/**************************************************************************** + * libs/libc/stdio/lib_putwchar.c + * + * Copyright © 2005-2014 Rich Felker, et al. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: putwchar + * + * Description: + * Write wide character to stdout + * + * Input Parameters: + * c - the wide character to write + * + * Returned Value: + * Return the wide character that written in to the stdout on success, + * return WEOF on fail write to the stdout + * + ****************************************************************************/ + +wint_t putwchar(wchar_t c) +{ +#ifdef CONFIG_FILE_STREAM + return fputwc(c, stdout); +#else + char mbc[MB_LEN_MAX]; + int l; + l = wctomb(mbc, c); + if (l < 0) + { + return WEOF; + } + + return write(STDOUT_FILENO, mbc, l) == l ? c : WEOF; +#endif +}