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 A84837E3D2 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=cDbWFR3Z; 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 06C5F6145C; Tue, 19 Apr 2022 00:42:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0B1FAC385AF; 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=Y3XHIWYLEBNlv6oGyGJ6J19GcR7wY2z46Qs+JlzYLko=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cDbWFR3Z/NaB0H1WRkivj8c71u+Lxxpxk0e2HSw698XSPPNIRKEdiTAufHDZexj54 FKv7MPCJVdpH3DVgvStuQnyG0fjzzDYNFdudnhOWOypH9yc9Hgi5euAU4T66GXko+p 1KrqXZY9OqP018gr3yVVFyggz+CmReg4mrlS4llxKqtSGllwVL7yu9gGWwvxZtxSZn DCy74GEo+HWI1FZoY4WnHNLKHEYniIWqZqW19d9+3vVKKtAxHnXMC1NnJph4tqbkaM 01haleXk5SuNNwfd2u8YwLK5Ja5YrohLVzY1EyKJnW7z0Gw3n5AexENkLkKK0SkEey GXqj4YYW6bcGA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 2BB895C30E3; 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 20/61] tools/nolibc/stdio: add fwrite() to stdio Date: Mon, 18 Apr 2022 17:41:44 -0700 Message-Id: <20220419004225.3952530-20-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 We'll use it to write substrings. It relies on a simpler _fwrite() that only takes one size. fputs() was also modified to rely on it. Signed-off-by: Willy Tarreau Signed-off-by: Paul E. McKenney --- tools/include/nolibc/stdio.h | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 149c5ca59aad..996bf89a30d2 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -84,12 +84,14 @@ int putchar(int c) } -/* puts(), fputs(). Note that puts() emits '\n' but not fputs(). */ +/* fwrite(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */ +/* internal fwrite()-like function which only takes a size and returns 0 on + * success or EOF on error. It automatically retries on short writes. + */ static __attribute__((unused)) -int fputs(const char *s, FILE *stream) +int _fwrite(const void *buf, size_t size, FILE *stream) { - size_t len = strlen(s); ssize_t ret; int fd; @@ -98,16 +100,35 @@ int fputs(const char *s, FILE *stream) fd = 3 + (long)stream; - while (len > 0) { - ret = write(fd, s, len); + while (size) { + ret = write(fd, buf, size); if (ret <= 0) return EOF; - s += ret; - len -= ret; + size -= ret; + buf += ret; } return 0; } +static __attribute__((unused)) +size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream) +{ + size_t written; + + for (written = 0; written < nmemb; written++) { + if (_fwrite(s, size, stream) != 0) + break; + s += size; + } + return written; +} + +static __attribute__((unused)) +int fputs(const char *s, FILE *stream) +{ + return _fwrite(s, strlen(s), stream); +} + static __attribute__((unused)) int puts(const char *s) { -- 2.31.1.189.g2e36527f23