From fd800709659d3e5ffce3b6ec2affead742ffdebb 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/posix/options/file_locking.c | 12 ++++++++++++ tests/posix/common/src/file_locking.c | 9 +++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/doc/services/portability/posix/option_groups/index.rst b/doc/services/portability/posix/option_groups/index.rst index 35d1e99883ef757..3c9e8472c0c00fc 100644 --- a/doc/services/portability/posix/option_groups/index.rst +++ b/doc/services/portability/posix/option_groups/index.rst @@ -569,8 +569,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_memory_protection: 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/posix/options/file_locking.c b/lib/posix/options/file_locking.c index 755d1194828b596..0c3dcc610615b42 100644 --- a/lib/posix/options/file_locking.c +++ b/lib/posix/options/file_locking.c @@ -35,3 +35,15 @@ void funlockfile(FILE *file) #ifdef CONFIG_POSIX_FD_MGMT_ALIAS_FUNLOCKFILE FUNC_ALIAS(funlockfile, _funlockfile, void); #endif /* CONFIG_POSIX_FD_MGMT_ALIAS_FUNLOCKFILE */ + +#undef putc_unlocked +int putc_unlocked(int c, FILE *stream) +{ + return putc(c, stream); +} + +#undef putchar_unlocked +int putchar_unlocked(int c) +{ + return putchar(c); +} diff --git a/tests/posix/common/src/file_locking.c b/tests/posix/common/src/file_locking.c index 90066b37a7767ec..1f933466d4a7fe4 100644 --- a/tests/posix/common/src/file_locking.c +++ b/tests/posix/common/src/file_locking.c @@ -11,6 +11,8 @@ void flockfile(FILE *file); int ftrylockfile(FILE *file); void funlockfile(FILE *file); +int putc_unlocked(int, FILE *); +int putchar_unlocked(int); #endif #include @@ -114,4 +116,11 @@ ZTEST(file_locking, test_file_locking) zassert_not_ok(ftrylockfile(file)); } +ZTEST(file_locking, test_stdio) +{ + int ret; + + ret = putc_unlocked('T', stdout); + zassert_equal(ret, 84, "putc \'T\' failed"); +} ZTEST_SUITE(file_locking, NULL, NULL, NULL, NULL, NULL);