tools/nolibc/stdio: add a minimal set of stdio functions
authorWilly Tarreau <w@1wt.eu>
Mon, 7 Feb 2022 16:23:30 +0000 (17:23 +0100)
committerPaul E. McKenney <paulmck@kernel.org>
Thu, 21 Apr 2022 00:05:44 +0000 (17:05 -0700)
This only provides getchar(), putchar(), and puts().

Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
tools/include/nolibc/nolibc.h
tools/include/nolibc/stdio.h [new file with mode: 0644]

index a349c88..7eaa09f 100644 (file)
@@ -88,6 +88,7 @@
 #include "types.h"
 #include "sys.h"
 #include "ctype.h"
+#include "stdio.h"
 #include "stdlib.h"
 #include "string.h"
 
diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
new file mode 100644 (file)
index 0000000..4c6af30
--- /dev/null
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+/*
+ * minimal stdio function definitions for NOLIBC
+ * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
+ */
+
+#ifndef _NOLIBC_STDIO_H
+#define _NOLIBC_STDIO_H
+
+#include "std.h"
+#include "arch.h"
+#include "types.h"
+#include "sys.h"
+#include "stdlib.h"
+#include "string.h"
+
+#ifndef EOF
+#define EOF (-1)
+#endif
+
+static __attribute__((unused))
+int getchar(void)
+{
+       unsigned char ch;
+
+       if (read(0, &ch, 1) <= 0)
+               return EOF;
+       return ch;
+}
+
+static __attribute__((unused))
+int putchar(int c)
+{
+       unsigned char ch = c;
+
+       if (write(1, &ch, 1) <= 0)
+               return EOF;
+       return ch;
+}
+
+static __attribute__((unused))
+int puts(const char *s)
+{
+       size_t len = strlen(s);
+       ssize_t ret;
+
+       while (len > 0) {
+               ret = write(1, s, len);
+               if (ret <= 0)
+                       return EOF;
+               s += ret;
+               len -= ret;
+       }
+       return putchar('\n');
+}
+
+#endif /* _NOLIBC_STDIO_H */