Skip to content

Commit 778629e

Browse files
committed
libc: minimal: implement putc_unlocked & putchar_unlocked
Add a simple implementation for putc_unlocked() & putchar_unlocked(). Signed-off-by: Yong Cong Sin <ycsin@meta.com>
1 parent 87729ba commit 778629e

File tree

13 files changed

+132
-2
lines changed

13 files changed

+132
-2
lines changed

doc/services/portability/posix/option_groups/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,8 @@ This table lists service support status in Zephyr for `POSIX_FD_MGMT`:
569569
funlockfile(), yes
570570
getc_unlocked(),
571571
getchar_unlocked(),
572-
putc_unlocked(),
573-
putchar_unlocked(),
572+
putc_unlocked(), yes
573+
putchar_unlocked(), yes
574574

575575
.. _posix_option_group_memory_protection:
576576

lib/libc/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ config MINIMAL_LIBC
7676
imply COMMON_LIBC_CALLOC
7777
imply COMMON_LIBC_REALLOCARRAY
7878
select POSIX_FILE_LOCKING if POSIX_THREAD_SAFE_FUNCTIONS
79+
select COMMON_LIBC_PUTC_UNLOCKED if POSIX_THREAD_SAFE_FUNCTIONS
80+
select COMMON_LIBC_GETC_UNLOCKED if POSIX_THREAD_SAFE_FUNCTIONS
7981
help
8082
Build with minimal C library.
8183

lib/libc/common/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_THRD
1717
source/thrd/tss.c
1818
)
1919

20+
add_subdirectory(source)
21+
2022
# Prevent compiler from optimizing calloc into an infinite recursive call
2123
zephyr_library_compile_options($<TARGET_PROPERTY:compiler,no_builtin_malloc>)

lib/libc/common/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,5 @@ config COMMON_LIBC_THRD
8282
default y
8383
help
8484
Common implementation of C11 <threads.h> API.
85+
86+
rsource "source/Kconfig"

lib/libc/common/source/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) 2024 Meta Platforms
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
add_subdirectory_ifdef(CONFIG_COMMON_LIBC_STDIO stdio)

lib/libc/common/source/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) 2024 Meta Platforms
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
rsource "stdio/Kconfig"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) 2024 Meta Platforms
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_GETC_UNLOCKED getc_unlocked.c)
5+
zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_PUTC_UNLOCKED putc_unlocked.c)

lib/libc/common/source/stdio/Kconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (c) 2024 Meta Platforms
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config COMMON_LIBC_STDIO
5+
bool
6+
help
7+
common function implementation in stdio.h
8+
9+
config COMMON_LIBC_GETC_UNLOCKED
10+
bool
11+
select COMMON_LIBC_STDIO
12+
help
13+
common implementation of getc_unlocked() & getchar_unlocked().
14+
15+
config COMMON_LIBC_PUTC_UNLOCKED
16+
bool
17+
select COMMON_LIBC_STDIO
18+
help
19+
common implementation of putc_unlocked() & putchar_unlocked().
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2024 Meta Platforms
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <errno.h>
8+
#include <stdio.h>
9+
10+
#include <zephyr/sys/util.h>
11+
12+
int getc_unlocked(FILE *stream)
13+
{
14+
ARG_UNUSED(stream);
15+
16+
errno = ENOSYS;
17+
18+
return EOF;
19+
}
20+
21+
int getchar_unlocked(void)
22+
{
23+
return getc_unlocked(stdin);
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2024 Meta Platforms
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdio.h>
8+
9+
#include <zephyr/sys/libc-hooks.h>
10+
11+
int putc_unlocked(int c, FILE *stream)
12+
{
13+
return zephyr_fputc(c, stream);
14+
}
15+
16+
int putchar_unlocked(int c)
17+
{
18+
return putc_unlocked(c, stdout);
19+
}

0 commit comments

Comments
 (0)