Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp3355845rwl; Sun, 2 Apr 2023 06:04:19 -0700 (PDT) X-Google-Smtp-Source: AKy350YUYmAi9O4kGHaD9OWLdYtcYja4L/Lw0Dl4Kkdco5l9A2bNmMmpmtu9hPUdTKDwi6DKJQin X-Received: by 2002:a17:907:a04:b0:947:bf71:a548 with SMTP id bb4-20020a1709070a0400b00947bf71a548mr7527106ejc.4.1680440658951; Sun, 02 Apr 2023 06:04:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680440658; cv=none; d=google.com; s=arc-20160816; b=bHuxjdC1kPJYlmQTW1hR8QSJFcVg0jxl/dhH2K1ry1f+W7TIk4buMLIzv2pIvABg2i tt6ihwASfOOHYTzevVbDF0di37aev8/sJE8BjZB4v8S/PIrCi8N0C6xf+m3j9BSnffT+ qSdeaZ33YErFy3Q7xMoKLRXcZiZR2hMNvHUpOb0aLX1WxR46Dnh6KIkZnhb07HBriN7e yIsohfqLV8uj5qLNtZu9NTaVvyqdqH6q5HKWFZJYu8A8q7hfe/rHaDu0sI2PW3n1nGMZ dC7gvAEgLFa5ekNxzYyaiTO56rYpqLDMKvDiEpXU+Iw0z6EgJQoqnqbGzXUdGZfgQf9e 4uvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:dkim-signature :from; bh=0rySYs/uedDXbQ8lnc/wuGQO0NJwQlNc/qxuEYmFJow=; b=BB7vn8wm+sg+DZo3DDXm6M8gqw2CxH1RtR8b68FaLIHze1JWTF33aUFN2sKjZl6XcL 2vQTMrENmyB8X7SQH1x4xUJepc1yqTVAGR6xb8l3NAqrxicohhHuJmngrtjrRc/2Aesj mH2lbqX4BszSBS2ObjOr2ftLlyKOnHCozRjDOpv7HXjz/gYB04/sYIOEQUTx+lR5OIml jyTGhXvPbgM1NLzRKjumfOPKGZZC0H8oKSylbcGgxbvO4cbpc+gArBaPUZLslm+7Vndu ggNKpHfVIC6pOXiIiPcaAd4CXiZ/huLGLRKuGl2hvTNWA/xe+wnJeTl91qFojyYtWNi3 t2lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=IH8eEvhV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f7-20020a170906738700b0092a3709e877si1233319ejl.679.2023.04.02.06.03.52; Sun, 02 Apr 2023 06:04:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=IH8eEvhV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229681AbjDBNDA (ORCPT + 99 others); Sun, 2 Apr 2023 09:03:00 -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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit 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= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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); -- 2.40.0