Skip to content

Commit

Permalink
libc: minimal: stubs getc_unlocked & getchar_unlocked
Browse files Browse the repository at this point in the history
Add stub implementation and test for `getc_unlocked()` &
`getchar_unlocked()`.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
  • Loading branch information
ycsin committed Jun 18, 2024
1 parent cf04ada commit f69126e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
8 changes: 4 additions & 4 deletions doc/services/portability/posix/option_groups/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,8 @@ This table lists service support status in Zephyr for `POSIX_FD_MGMT`:
flockfile(), yes
ftrylockfile(), yes
funlockfile(), yes
getc_unlocked(),
getchar_unlocked(),
getc_unlocked(), yes :ref:`†<posix_undefined_behaviour>`
getchar_unlocked(), yes :ref:`†<posix_undefined_behaviour>`
putc_unlocked(), yes
putchar_unlocked(), yes

Expand Down Expand Up @@ -904,8 +904,8 @@ _POSIX_THREAD_SAFE_FUNCTIONS
flockfile(), yes
ftrylockfile(), yes
funlockfile(), yes
getc_unlocked(),
getchar_unlocked(),
getc_unlocked(), yes :ref:`†<posix_undefined_behaviour>`
getchar_unlocked(), yes :ref:`†<posix_undefined_behaviour>`
getgrgid_r(),
getgrnam_r(),
getpwnam_r(),
Expand Down
2 changes: 2 additions & 0 deletions lib/libc/minimal/include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ size_t fwrite(const void *ZRESTRICT ptr, size_t size, size_t nitems,
#define putchar(c) putc(c, stdout)
#define putc_unlocked(c, stream) putc(c, stream)
#define putchar_unlocked(c) putchar(c)
int getc_unlocked(FILE *stream);
int getchar_unlocked(void);

void flockfile(FILE *file);
int ftrylockfile(FILE *file);
Expand Down
15 changes: 15 additions & 0 deletions lib/posix/options/file_locking.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <errno.h>
#include <stdio.h>

#include <zephyr/sys/util.h>
Expand Down Expand Up @@ -47,3 +48,17 @@ int putchar_unlocked(int c)
{
return putchar(c);
}

#undef getc_unlocked
int getc_unlocked(FILE *stream)
{
ARG_UNUSED(stream);

return ENOSYS;
}

#undef getchar_unlocked
int getchar_unlocked(void)
{
return getc_unlocked(stdin);
}
13 changes: 13 additions & 0 deletions tests/posix/common/src/file_locking.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ int ftrylockfile(FILE *file);
void funlockfile(FILE *file);
int putc_unlocked(int c, FILE *file);
int putchar_unlocked(int c);
int getc_unlocked(FILE *stream);
int getchar_unlocked(void);
#endif

#include <zephyr/sys/fdtable.h>
Expand Down Expand Up @@ -123,4 +125,15 @@ ZTEST(file_locking, test_stdio)
ret = putc_unlocked('T', stdout);
zassert_equal(ret, 84, "putc \'T\' failed");
}

/**
* @brief Existence test for the stubs, these function are not implemented and will simply return
* `ENOSYS`.
*/
ZTEST(file_locking, test_stubs)
{
zassert_equal(getchar_unlocked(), ENOSYS);
zassert_equal(getc_unlocked(stdin), ENOSYS);
}

ZTEST_SUITE(file_locking, NULL, NULL, NULL, NULL, NULL);

0 comments on commit f69126e

Please sign in to comment.