From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-6.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by gnuweeb.org (Postfix) with ESMTPS id D34077E3D4 for ; Tue, 19 Apr 2022 00:42:30 +0000 (UTC) Authentication-Results: gnuweeb.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=HBrdvSpy; dkim-atps=neutral Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2DE4F613EA; Tue, 19 Apr 2022 00:42:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0EB62C385CB; Tue, 19 Apr 2022 00:42:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1650328948; bh=U8Bdhhgm4XkhuZa/4y0o4tKotWCm1yK5jlD2daXT+eQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HBrdvSpyJoOkiJ049s5jVwiI+iA7oSIZUQQlNn2SbTO+0XImSMzKwcyioCzCwN7ol l3rHZoyOk3LTz0CVyp7S6hVU0FQXP/8DicPV4VsOt0IsOfEsmN7E/0/qRfAZQbSg5v g03YhDTlyMDq0tvW3jeYvPtYz/2LDsr5kGjAvRANUAe7hD8TQcp5Bqs551QPB6pvEm NxSLeWbRJZ8z7AysyezvJv9Z9zeu0KR/DrCOLFKfMDimDAiNyLkgrefXoPXL6I/SCV zbcgfefAff1zLibaw2sXa8IRNsUsoPn/rrTN4/HXBa4wGDNUCr5cnSxWakOm4DbmSP 7cPXIQkdsKSUQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 29F9D5C30E1; Mon, 18 Apr 2022 17:42:27 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: gwml@vger.gnuweeb.org, kernel-team@fb.com, w@lwt.eu, Willy Tarreau , "Paul E . McKenney" Subject: [PATCH nolibc 19/61] tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions Date: Mon, 18 Apr 2022 17:41:43 -0700 Message-Id: <20220419004225.3952530-19-paulmck@kernel.org> X-Mailer: git-send-email 2.31.1.189.g2e36527f23 In-Reply-To: <20220419004219.GA3952301@paulmck-ThinkPad-P17-Gen-1> References: <20220419004219.GA3952301@paulmck-ThinkPad-P17-Gen-1> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: From: Willy Tarreau 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 Signed-off-by: Paul E. McKenney --- 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.31.1.189.g2e36527f23