Skip to content

Commit

Permalink
lib: posix: add perror() implementation
Browse files Browse the repository at this point in the history
Add a trivial implementation of `perror()`.

Fixes zephyrproject-rtos#46100

Signed-off-by: Christopher Friedt <[email protected]>
  • Loading branch information
cfriedt authored and carlescufi committed Jul 4, 2022
1 parent e08f84a commit b391993
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions doc/services/portability/posix.rst
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ This is implemented as part of the minimal C library available in Zephyr.
strcoll(),
strcpy(),yes
strcspn(),
strerror(),
strerror_r(),
strerror(),yes
strerror_r(),yes
strftime(),
strlen(),yes
strncat(),yes
Expand Down Expand Up @@ -412,7 +412,7 @@ POSIX_DEVICE_IO
getchar(),
gets(),
open(),yes
perror(),
perror(),yes
printf(),yes
putc(),yes
putchar(),
Expand Down
1 change: 1 addition & 0 deletions lib/libc/minimal/include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ int __printf_like(2, 0) vfprintf(FILE *ZRESTRICT stream,
const char *ZRESTRICT format,
va_list ap);

void perror(const char *s);
int puts(const char *s);

int fputc(int c, FILE *stream);
Expand Down
1 change: 1 addition & 0 deletions lib/posix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if(CONFIG_POSIX_API)
endif()

zephyr_library()
zephyr_library_sources(perror.c)
zephyr_library_sources(pthread_common.c)
zephyr_library_sources_ifdef(CONFIG_PTHREAD_IPC pthread_cond.c)
zephyr_library_sources_ifdef(CONFIG_PTHREAD_IPC pthread_mutex.c)
Expand Down
18 changes: 18 additions & 0 deletions lib/posix/perror.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2022 Meta
*
* SPDX-License-Identifier: Apache-2.0
*/

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

/*
* See https://pubs.opengroup.org/onlinepubs/9699919799/functions/perror.html
*/
void perror(const char *s)
{
fprintf(stderr, "%s%s%s\n", s == NULL ? "" : s, s == NULL ? "" : ": ",
strerror(errno));
}

0 comments on commit b391993

Please sign in to comment.