From a391affca8dce75d51f487d43babb3551c03b0d1 Mon Sep 17 00:00:00 2001 From: Marco Schlumpp Date: Tue, 29 Mar 2022 17:32:46 +0200 Subject: [PATCH] lib/nolibc: Implement fputs and puts functions These functions were still missing from nolibc. Signed-off-by: Marco Schlumpp Reviewed-by: Razvan Deaconescu Reviewed-by: Marc Rittinghaus Approved-by: Marc Rittinghaus Tested-by: Unikraft CI GitHub-Closes: #627 --- lib/nolibc/exportsyms.uk | 2 ++ lib/nolibc/include/stdio.h | 2 ++ lib/nolibc/stdio.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/lib/nolibc/exportsyms.uk b/lib/nolibc/exportsyms.uk index 7fef8490a..c90f452e8 100644 --- a/lib/nolibc/exportsyms.uk +++ b/lib/nolibc/exportsyms.uk @@ -40,6 +40,8 @@ printf fflush fputc putchar +fputs +puts # stdlib abort diff --git a/lib/nolibc/include/stdio.h b/lib/nolibc/include/stdio.h index 1d5f360cc..1e5f9f2aa 100644 --- a/lib/nolibc/include/stdio.h +++ b/lib/nolibc/include/stdio.h @@ -85,6 +85,8 @@ void psignal(int sig, const char *s); int fputc(int _c, FILE *fp); int putchar(int c); +int fputs(const char *restrict s, FILE *restrict stream); +int puts(const char *s); #if CONFIG_LIBVFSCORE int rename(const char *oldpath, const char *newpath); diff --git a/lib/nolibc/stdio.c b/lib/nolibc/stdio.c index 4361b3aee..5c4c61c06 100644 --- a/lib/nolibc/stdio.c +++ b/lib/nolibc/stdio.c @@ -504,3 +504,40 @@ int putchar(int c) { return fputc(c, stdout); } + +static int +fputs_internal(const char *restrict s, FILE *restrict stream, int newline) +{ + int ret; + size_t len; + + len = strlen(s); + + if (stream == stdout) + ret = ukplat_coutk(s, len); + else if (stream == stderr) + ret = ukplat_coutd(s, len); + else + return EOF; + + /* If ukplat_cout{d,k} weren't able to write all characters, assume that + * an error happened and there is no point in retrying. + */ + if ((size_t)ret != len) + return EOF; + + if (newline) + return fputc('\n', stream); + else + return 1; +} + +int fputs(const char *restrict s, FILE *restrict stream) +{ + return fputs_internal(s, stream, 0); +} + +int puts(const char *s) +{ + return fputs_internal(s, stdout, 1); +} -- 2.39.5