From patchwork Sun Apr 2 13:02:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13197389 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D4A4C77B62 for ; Sun, 2 Apr 2023 13:02:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230407AbjDBNCz (ORCPT ); Sun, 2 Apr 2023 09:02:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230381AbjDBNCy (ORCPT ); Sun, 2 Apr 2023 09:02:54 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8F1F11159; Sun, 2 Apr 2023 06:02:52 -0700 (PDT) From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1680440570; bh=T8RVOs4azHVELdWLvY+5zv0X2y22kGN/1wJMaJIVLtc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=IH8eEvhVyAcAtKynq+H8IW/Krqlmz4GRaD9yr3ys4uDrIJvRBYJo/zXE8FHlIQULA ilMRHtpqjFUrIUge4slsizPDekitgtUo45FoQ6Vn9NVXBQJHu7W3z3jbwUZbc/X2lF vp0qC4To3HcnJs7sttaBEUDhWnTlM1ByU7DuKaew= Date: Sun, 02 Apr 2023 13:02:46 +0000 Subject: [PATCH v2 2/3] tools/nolibc: implement fd-based FILE streams MIME-Version: 1.0 Message-Id: <20230328-nolibc-printf-test-v2-2-f72bdf210190@weissschuh.net> References: <20230328-nolibc-printf-test-v2-0-f72bdf210190@weissschuh.net> In-Reply-To: <20230328-nolibc-printf-test-v2-0-f72bdf210190@weissschuh.net> To: Willy Tarreau , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.12.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680440570; l=3061; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=T8RVOs4azHVELdWLvY+5zv0X2y22kGN/1wJMaJIVLtc=; b=5iR9MA+v57ECoqD2l9JxLhY8lvr1vpmRApDWKYWF44Ra2kojXz7Zb+78WA3ZY0UoFxngpL6un BvpTdEDrFI9BdC182hzgWdJK01UvDSj38Poi7mvwaqsIU270Wrf3Bdt X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This enables the usage of the stream APIs with arbitrary filedescriptors. It will be used by a future testcase. Signed-off-by: Thomas Weißschuh --- Willy: This uses intptr_t instead of uintptr_t as proposed because uintptr_t can not be negative. --- tools/include/nolibc/stdio.h | 60 ++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 96ac8afc5aee..edf1009e9203 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -21,17 +21,40 @@ #define EOF (-1) #endif -/* just define FILE as a non-empty type */ +/* just define FILE as a non-empty type. The value of the pointer gives + * the FD: FILE=~fd for fd>=0 or NULL for fd<0. This way positive FILE + * are immediately identified as abnormal entries (i.e. possible copies + * of valid pointers to something else). + */ 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; +static __attribute__((unused)) FILE* const stdin = (FILE*)(intptr_t)~STDIN_FILENO; +static __attribute__((unused)) FILE* const stdout = (FILE*)(intptr_t)~STDOUT_FILENO; +static __attribute__((unused)) FILE* const stderr = (FILE*)(intptr_t)~STDERR_FILENO; + +/* provides a FILE* equivalent of fd. The mode is ignored. */ +static __attribute__((unused)) +FILE *fdopen(int fd, const char *mode __attribute__((unused))) +{ + if (fd < 0) + return NULL; + return (FILE*)(intptr_t)~fd; +} + +/* provides the fd from of stream. */ +static __attribute__((unused)) +int fileno(FILE *stream) +{ + intptr_t i = (intptr_t)stream; + + if (i > 0) { + SET_ERRNO(EBADF); + return -1; + } + return ~i; +} /* getc(), fgetc(), getchar() */ @@ -41,14 +64,8 @@ static __attribute__((unused)) int fgetc(FILE* stream) { unsigned char ch; - int fd; - if (stream < stdin || stream > stderr) - return EOF; - - fd = 3 + (long)stream; - - if (read(fd, &ch, 1) <= 0) + if (read(fileno(stream), &ch, 1) <= 0) return EOF; return ch; } @@ -68,14 +85,8 @@ static __attribute__((unused)) int fputc(int c, FILE* stream) { unsigned char ch = c; - int fd; - - if (stream < stdin || stream > stderr) - return EOF; - - fd = 3 + (long)stream; - if (write(fd, &ch, 1) <= 0) + if (write(fileno(stream), &ch, 1) <= 0) return EOF; return ch; } @@ -96,12 +107,7 @@ static __attribute__((unused)) int _fwrite(const void *buf, size_t size, FILE *stream) { ssize_t ret; - int fd; - - if (stream < stdin || stream > stderr) - return EOF; - - fd = 3 + (long)stream; + int fd = fileno(stream); while (size) { ret = write(fd, buf, size);