]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/nolibc: Implement fputs and puts functions
authorMarco Schlumpp <marco.schlumpp@gmail.com>
Tue, 29 Mar 2022 15:32:46 +0000 (17:32 +0200)
committerUnikraft <monkey@unikraft.io>
Thu, 4 May 2023 15:50:59 +0000 (15:50 +0000)
These functions were still missing from nolibc.

Signed-off-by: Marco Schlumpp <marco.schlumpp@gmail.com>
Reviewed-by: Razvan Deaconescu <razvand@unikraft.io>
Reviewed-by: Marc Rittinghaus <marc.rittinghaus@unikraft.io>
Approved-by: Marc Rittinghaus <marc.rittinghaus@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #627

lib/nolibc/exportsyms.uk
lib/nolibc/include/stdio.h
lib/nolibc/stdio.c

index 7fef8490ae37d1270bad307bbe6802e9e1076e03..c90f452e81a1fbdb9f8bbd06a36b5245a39047cb 100644 (file)
@@ -40,6 +40,8 @@ printf
 fflush
 fputc
 putchar
+fputs
+puts
 
 # stdlib
 abort
index 1d5f360cc43ef2e14ad4e58909e2e78adbe69e38..1e5f9f2aa1f0ab60ca17b291712138161671c4b2 100644 (file)
@@ -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);
index 4361b3aee18d9427ab6ee08b107646c64982ae90..5c4c61c06d99eac8a4d8c301057cec6b70127339 100644 (file)
@@ -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);
+}