linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Willy Tarreau <w@1wt.eu>
To: "Paul E . McKenney" <paulmck@kernel.org>
Cc: Mark Brown <broonie@kernel.org>,
	linux-kernel@vger.kernel.org, Willy Tarreau <w@1wt.eu>
Subject: [PATCH 19/42] tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions
Date: Mon,  7 Feb 2022 17:23:31 +0100	[thread overview]
Message-ID: <20220207162354.14293-20-w@1wt.eu> (raw)
In-Reply-To: <20220207162354.14293-1-w@1wt.eu>

The standard puts() function always emits the trailing LF which makes it
unconvenient for small string concatenation. fputs() ought to be used
instead but it requires a FILE*.

This adds 3 dummy FILE* values (stdin, stdout, stderr) which are in fact
pointers to struct FILE of one byte. We reserve 3 pointer values for them,
-3, -2 and -1, so that they are ordered, easing the tests and mapping to
integer.

From this, fgetc(), fputc(), fgets() and fputs() were implemented, and
the previous putchar() and getchar() now remap to these. The standard
getc() and putc() macros were also implemented as pointing to these
ones.

There is absolutely no buffering, fgetc() and fgets() read one byte at
a time, fputc() writes one byte at a time, and only fputs() which knows
the string's length writes all of it at once.

Signed-off-by: Willy Tarreau <w@1wt.eu>
---
 tools/include/nolibc/stdio.h | 95 +++++++++++++++++++++++++++++++++---
 1 file changed, 89 insertions(+), 6 deletions(-)

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 4c6af3016e2e..149c5ca59aad 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -18,40 +18,123 @@
 #define EOF (-1)
 #endif
 
+/* just define FILE as a non-empty type */
+typedef struct FILE {
+	char dummy[1];
+} FILE;
+
+/* We define the 3 common stdio files as constant invalid pointers that
+ * are easily recognized.
+ */
+static __attribute__((unused)) FILE* const stdin  = (FILE*)-3;
+static __attribute__((unused)) FILE* const stdout = (FILE*)-2;
+static __attribute__((unused)) FILE* const stderr = (FILE*)-1;
+
+/* getc(), fgetc(), getchar() */
+
+#define getc(stream) fgetc(stream)
+
 static __attribute__((unused))
-int getchar(void)
+int fgetc(FILE* stream)
 {
 	unsigned char ch;
+	int fd;
 
-	if (read(0, &ch, 1) <= 0)
+	if (stream < stdin || stream > stderr)
+		return EOF;
+
+	fd = 3 + (long)stream;
+
+	if (read(fd, &ch, 1) <= 0)
 		return EOF;
 	return ch;
 }
 
 static __attribute__((unused))
-int putchar(int c)
+int getchar(void)
+{
+	return fgetc(stdin);
+}
+
+
+/* putc(), fputc(), putchar() */
+
+#define putc(c, stream) fputc(c, stream)
+
+static __attribute__((unused))
+int fputc(int c, FILE* stream)
 {
 	unsigned char ch = c;
+	int fd;
 
-	if (write(1, &ch, 1) <= 0)
+	if (stream < stdin || stream > stderr)
+		return EOF;
+
+	fd = 3 + (long)stream;
+
+	if (write(fd, &ch, 1) <= 0)
 		return EOF;
 	return ch;
 }
 
 static __attribute__((unused))
-int puts(const char *s)
+int putchar(int c)
+{
+	return fputc(c, stdout);
+}
+
+
+/* puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
+
+static __attribute__((unused))
+int fputs(const char *s, FILE *stream)
 {
 	size_t len = strlen(s);
 	ssize_t ret;
+	int fd;
+
+	if (stream < stdin || stream > stderr)
+		return EOF;
+
+	fd = 3 + (long)stream;
 
 	while (len > 0) {
-		ret = write(1, s, len);
+		ret = write(fd, s, len);
 		if (ret <= 0)
 			return EOF;
 		s += ret;
 		len -= ret;
 	}
+	return 0;
+}
+
+static __attribute__((unused))
+int puts(const char *s)
+{
+	if (fputs(s, stdout) == EOF)
+		return EOF;
 	return putchar('\n');
 }
 
+
+/* fgets() */
+static __attribute__((unused))
+char *fgets(char *s, int size, FILE *stream)
+{
+	int ofs;
+	int c;
+
+	for (ofs = 0; ofs + 1 < size;) {
+		c = fgetc(stream);
+		if (c == EOF)
+			break;
+		s[ofs++] = c;
+		if (c == '\n')
+			break;
+	}
+	if (ofs < size)
+		s[ofs] = 0;
+	return ofs ? s : NULL;
+}
+
 #endif /* _NOLIBC_STDIO_H */
-- 
2.35.1


  parent reply	other threads:[~2022-02-07 16:45 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-07 16:23 [PATCH 00/42] nolibc: update to resync with out-of-tree project Willy Tarreau
2022-02-07 16:23 ` [PATCH 01/42] tools/nolibc: use pselect6 on RISCV Willy Tarreau
2022-02-07 16:23 ` [PATCH 02/42] tools/nolibc: guard the main file against multiple inclusion Willy Tarreau
2022-02-07 16:23 ` [PATCH 03/42] tools/nolibc/std: move the standard type definitions to std.h Willy Tarreau
2022-02-07 16:23 ` [PATCH 04/42] tools/nolibc/types: split syscall-specific definitions into their own files Willy Tarreau
2022-02-07 16:23 ` [PATCH 05/42] tools/nolibc/arch: split arch-specific code into individual files Willy Tarreau
2022-02-07 16:23 ` [PATCH 06/42] tools/nolibc/sys: split the syscall definitions into their own file Willy Tarreau
2022-02-07 16:23 ` [PATCH 07/42] tools/nolibc/stdlib: extract the stdlib-specific functions to " Willy Tarreau
2022-02-07 16:23 ` [PATCH 08/42] tools/nolibc/string: split the string functions into string.h Willy Tarreau
2022-02-07 16:23 ` [PATCH 09/42] tools/nolibc/ctype: split the is* functions to ctype.h Willy Tarreau
2022-02-07 16:23 ` [PATCH 10/42] tools/nolibc/ctype: add the missing is* functions Willy Tarreau
2022-02-07 16:23 ` [PATCH 11/42] tools/nolibc/types: move the FD_* functions to macros in types.h Willy Tarreau
2022-02-07 17:05   ` David Laight
2022-02-08  5:14     ` Willy Tarreau
2022-02-13  8:52   ` [PATCH v2 " Willy Tarreau
2022-02-07 16:23 ` [PATCH 12/42] tools/nolibc/types: make FD_SETSIZE configurable Willy Tarreau
2022-02-13  8:53   ` [PATCH v2 " Willy Tarreau
2022-02-14 20:17     ` Paul E. McKenney
2022-02-07 16:23 ` [PATCH 13/42] tools/nolibc/types: move makedev to types.h and make it a macro Willy Tarreau
2022-02-07 16:23 ` [PATCH 14/42] tools/nolibc/stdlib: move ltoa() to stdlib.h Willy Tarreau
2022-02-07 16:23 ` [PATCH 15/42] tools/nolibc/stdlib: replace the ltoa() function with more efficient ones Willy Tarreau
2022-02-07 16:23 ` [PATCH 16/42] tools/nolibc/stdlib: add i64toa() and u64toa() Willy Tarreau
2022-02-07 16:23 ` [PATCH 17/42] tools/nolibc/stdlib: add utoh() and u64toh() Willy Tarreau
2022-02-07 16:23 ` [PATCH 18/42] tools/nolibc/stdio: add a minimal set of stdio functions Willy Tarreau
2022-02-07 16:23 ` Willy Tarreau [this message]
2022-02-07 16:23 ` [PATCH 20/42] tools/nolibc/stdio: add fwrite() to stdio Willy Tarreau
2022-02-07 16:23 ` [PATCH 21/42] tools/nolibc/stdio: add a minimal [vf]printf() implementation Willy Tarreau
2022-02-07 16:23 ` [PATCH 22/42] tools/nolibc/types: define EXIT_SUCCESS and EXIT_FAILURE Willy Tarreau
2022-02-07 16:23 ` [PATCH 23/42] tools/nolibc/stdio: add perror() to report the errno value Willy Tarreau
2022-02-07 16:23 ` [PATCH 24/42] tools/nolibc/sys: make open() take a vararg on the 3rd argument Willy Tarreau
2022-02-07 16:23 ` [PATCH 25/42] tools/nolibc/stdlib: avoid a 64-bit shift in u64toh_r() Willy Tarreau
2022-02-07 16:23 ` [PATCH 26/42] tools/nolibc/stdlib: make raise() use the lower level syscalls only Willy Tarreau
2022-02-07 16:23 ` [PATCH 27/42] tools/nolibc/sys: make getpgrp(), getpid(), gettid() not set errno Willy Tarreau
2022-02-07 16:23 ` [PATCH 28/42] tools/nolibc/string: use unidirectional variants for memcpy() Willy Tarreau
2022-02-07 16:23 ` [PATCH 29/42] tools/nolibc/string: slightly simplify memmove() Willy Tarreau
2022-02-07 16:23 ` [PATCH 30/42] tools/nolibc/string: add strncpy() and strlcpy() Willy Tarreau
2022-02-07 16:23 ` [PATCH 31/42] tools/nolibc/string: add tiny versions of strncat() and strlcat() Willy Tarreau
2022-02-07 16:23 ` [PATCH 32/42] tools/nolibc: move exported functions to their own section Willy Tarreau
2022-02-07 16:23 ` [PATCH 33/42] tools/nolibc/arch: mark the _start symbol as weak Willy Tarreau
2022-02-07 16:23 ` [PATCH 34/42] tools/nolibc/types: define PATH_MAX and MAXPATHLEN Willy Tarreau
2022-02-07 16:23 ` [PATCH 35/42] tools/nolibc/string: export memset() and memmove() Willy Tarreau
2022-02-07 16:23 ` [PATCH 36/42] tools/nolibc/errno: extract errno.h from sys.h Willy Tarreau
2022-02-07 16:23 ` [PATCH 37/42] tools/nolibc/unistd: extract msleep(), sleep(), tcsetpgrp() to unistd.h Willy Tarreau
2022-02-07 16:23 ` [PATCH 38/42] tools/nolibc/unistd: add usleep() Willy Tarreau
2022-02-07 16:23 ` [PATCH 39/42] tools/nolibc/signal: move raise() to signal.h Willy Tarreau
2022-02-07 16:23 ` [PATCH 40/42] tools/nolibc/time: create time.h with time() Willy Tarreau
2022-02-07 16:23 ` [PATCH 41/42] tools/nolibc: also mention how to build by just setting the include path Willy Tarreau
2022-02-07 16:23 ` [PATCH 42/42] tools/nolibc/stdlib: implement abort() Willy Tarreau
2022-02-08  0:00 ` [PATCH 00/42] nolibc: update to resync with out-of-tree project Paul E. McKenney
2022-02-08  4:41   ` Willy Tarreau
2022-02-08  5:10     ` Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220207162354.14293-20-w@1wt.eu \
    --to=w@1wt.eu \
    --cc=broonie@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paulmck@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).