From 0266adb20b069147a080827004c26eec03188bc9 Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Fri, 14 Jun 2024 19:55:55 +0800 Subject: [PATCH] libc: minimal: implement putc_unlocked & putchar_unlocked Add a simple implementation for putc_unlocked() & putchar_unlocked(). Signed-off-by: Yong Cong Sin --- .../portability/posix/option_groups/index.rst | 4 ++-- lib/libc/minimal/include/stdio.h | 2 ++ lib/libc/minimal/source/stdout/stdout_console.c | 12 ++++++++++++ tests/lib/sprintf/src/main.c | 3 +++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/doc/services/portability/posix/option_groups/index.rst b/doc/services/portability/posix/option_groups/index.rst index ee66a5386e4584c..2aade0b1c9dc684 100644 --- a/doc/services/portability/posix/option_groups/index.rst +++ b/doc/services/portability/posix/option_groups/index.rst @@ -556,8 +556,8 @@ This table lists service support status in Zephyr for `POSIX_FD_MGMT`: funlockfile(), yes getc_unlocked(), getchar_unlocked(), - putc_unlocked(), - putchar_unlocked(), + putc_unlocked(), yes + putchar_unlocked(), yes .. _posix_option_group_multi_process: diff --git a/lib/libc/minimal/include/stdio.h b/lib/libc/minimal/include/stdio.h index 243a6c7471bd1e2..4fd0588d42e8f31 100644 --- a/lib/libc/minimal/include/stdio.h +++ b/lib/libc/minimal/include/stdio.h @@ -62,6 +62,8 @@ size_t fwrite(const void *ZRESTRICT ptr, size_t size, size_t nitems, FILE *ZRESTRICT stream); #define putc(c, stream) fputc(c, stream) #define putchar(c) putc(c, stdout) +#define putc_unlocked(c, stream) putc(c, stream) +#define putchar_unlocked(c) putchar(c) void flockfile(FILE *file); int ftrylockfile(FILE *file); diff --git a/lib/libc/minimal/source/stdout/stdout_console.c b/lib/libc/minimal/source/stdout/stdout_console.c index e3d5a374a53d485..ee064188f58ba91 100644 --- a/lib/libc/minimal/source/stdout/stdout_console.c +++ b/lib/libc/minimal/source/stdout/stdout_console.c @@ -59,12 +59,24 @@ int putc(int c, FILE *stream) return zephyr_fputc(c, stream); } +#undef putc_unlocked +int putc_unlocked(int c, FILE *stream) +{ + return putc(c, stream); +} + #undef putchar int putchar(int c) { return zephyr_fputc(c, stdout); } +#undef putchar_unlocked +int putchar_unlocked(int c) +{ + return putchar(c); +} + size_t z_impl_zephyr_fwrite(const void *ZRESTRICT ptr, size_t size, size_t nitems, FILE *ZRESTRICT stream) { diff --git a/tests/lib/sprintf/src/main.c b/tests/lib/sprintf/src/main.c index af22d97fadd8c88..0c5c4f1572525af 100644 --- a/tests/lib/sprintf/src/main.c +++ b/tests/lib/sprintf/src/main.c @@ -890,6 +890,9 @@ ZTEST(sprintf, test_put) ret = fputc('T', stderr); zassert_equal(ret, 84, "fputc \'T\' failed"); + ret = putc_unlocked('T', stdout); + zassert_equal(ret, 84, "putc \'T\' failed"); + ret = fputc('T', stdin); zassert_equal(ret, EOF, "fputc to stdin"); }