From patchwork Fri Sep 5 17:01:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 238 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757091713; bh=h0tazrGizP5nRbtCF0HX0tCud7A9Bk0xe42G21nvxQQ=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=ky3B9LdfTJlq/EvtegL1NEh63rEbm9L0x9KkLdrdiwf+2fptW9WsSpaOmapMlvPoy xdbuZ73bDOnx3qmYQ6oOr/+w0qHcO+5hTktw9rfHcCX200m94csLfkPU1gckWhtBJu qy2NJFfvSjzznfcZdVLEsSMd3B219EiBYnJ5x2td+liQkNC/TeYFb+Cnl+aMyFpwaQ xPFiouJ3lSA7boVaJ92hRZ5RVKgHEi9dTmZaljY39fMUXI9ebsjWv/AQj0EN/btbpr 5tQUowFWgsQGC+v/Ovm8Knv3+Z3pER8y9fCLnQa839oap43HIznBxbmcObxUf/N5/2 8OOfe0BqAzOMw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 594BA678E7 for ; Fri, 5 Sep 2025 11:01:53 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id FYkKHmlj-Ebd for ; Fri, 5 Sep 2025 11:01:53 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757091713; bh=h0tazrGizP5nRbtCF0HX0tCud7A9Bk0xe42G21nvxQQ=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=ky3B9LdfTJlq/EvtegL1NEh63rEbm9L0x9KkLdrdiwf+2fptW9WsSpaOmapMlvPoy xdbuZ73bDOnx3qmYQ6oOr/+w0qHcO+5hTktw9rfHcCX200m94csLfkPU1gckWhtBJu qy2NJFfvSjzznfcZdVLEsSMd3B219EiBYnJ5x2td+liQkNC/TeYFb+Cnl+aMyFpwaQ xPFiouJ3lSA7boVaJ92hRZ5RVKgHEi9dTmZaljY39fMUXI9ebsjWv/AQj0EN/btbpr 5tQUowFWgsQGC+v/Ovm8Knv3+Z3pER8y9fCLnQa839oap43HIznBxbmcObxUf/N5/2 8OOfe0BqAzOMw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 4955767980 for ; Fri, 5 Sep 2025 11:01:53 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757091711; bh=tshyhzeCZRaDyeo6heiIlpe3OG+6F0KyTXhvRt6Bcs8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NA4uqNnOVDNnbQGRYN6rENADx2xrgY9zYBK4ZhefV5oa20cFJMRRXREugSgx28Q7g C9XdtS6/stv2dAHczDvTu8RkC0LF01Dl4j9TLQKeplZ9SJTaMS+wyxEAPE9CsoLEY0 ZVRLHHLHViH1CINVLXJlXkBV4vNi1q5ctYaSdsW/RKnMhLOqLmr4E5gl3jIUD7jM6r DQBF6QY7/uQ5ZnUt3j3QoepYYF/PNV6wHTD1rEwKB6mOtkDxK4mcybopuWI/aP0GX6 pZ5/Y2F1m049KxXVD+ThpOJ3s1+nrjrX9WKKUVNm2rEK9R5f3js3mRSlI04G+19GLN K3/Bhqbx0WFDA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 55CCB678AA; Fri, 5 Sep 2025 11:01:51 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id bRk1irR5XrC6; Fri, 5 Sep 2025 11:01:51 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757091707; bh=yQQwsF9MvoEKkZ46B4bv6OE+MazBprRyDfqZXFRaHcM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dzaTs/VwWmvtuFihu9g7MxY4fPOyg5ZuRIHr1GzwMpPXAaMQSSea6UI0v58HIzJ4g vJAjk4LJnHqJryfGoTL+r0MAtf87jyGyJ7EAdYJeh+QyDz/0oWupsK77J6tMTB8QzS no5Itbe5RAIfNROh7Id1b3UNFYQq0dbT9jqnI35ypGywzyjcRmMHgph1WKCH/VfwDw WivHDtpqdH1PIw5e5Iwfe8UcGbVQJChTWRssEvhsdItzgOcmHaYVfcaWiAmekgNuB+ 2mG9+Cvcb0zfL6XoL0+gqRYhnIzYW4ioq7n1doU7vgmM5onBCrIHZvUM77L77ySPb+ gXOzkPiuSqz+w== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 0B804678E6; Fri, 5 Sep 2025 11:01:46 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Fri, 5 Sep 2025 11:01:12 -0600 Message-ID: <20250905170132.182249-2-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250905170132.182249-1-sjg@u-boot.org> References: <20250905170132.182249-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: TTFNONYQCBH57EMPJORKDZUROMSL7DZU X-Message-ID-Hash: TTFNONYQCBH57EMPJORKDZUROMSL7DZU X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Simon Glass X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 01/15] sandbox: Add a function to read a line from a file List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Provide a version of fgets() which works on file descriptors. This can be used to read lines from a file, as will be needed for the ulib test-program. Signed-off-by: Simon Glass --- arch/sandbox/cpu/os.c | 32 ++++++++++++++++++++++++++++++++ include/os.h | 14 ++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c index 1c4e23cb4eb..891dfd9c9b8 100644 --- a/arch/sandbox/cpu/os.c +++ b/arch/sandbox/cpu/os.c @@ -150,6 +150,38 @@ int os_unlink(const char *pathname) return unlink(pathname); } +char *os_fgets(char *str, int size, int fd) +{ + char *s = str; + int n = size - 1; + int i; + + if (n <= 0 || !str) + return NULL; + + for (i = 0; i < n; i++) { + ssize_t ret; + char c; + + ret = read(fd, &c, 1); + if (ret <= 0) { + /* EOF or error */ + if (!i) + return NULL; + break; + } + + *s++ = c; + + if (c == '\n') + break; + } + + *s = '\0'; + + return str; +} + void os_exit(int exit_code) { exit(exit_code); diff --git a/include/os.h b/include/os.h index 3393acb435a..e5c524c57a4 100644 --- a/include/os.h +++ b/include/os.h @@ -106,6 +106,20 @@ int os_isatty(int fd); */ int os_unlink(const char *pathname); +/** + * os_fgets() - read a string from a file stream + * + * Reads at most @size - 1 characters from the stream and stores them in str. + * Reading stops after an EOF or a newline. If a newline is read, it is + * stored in str. A terminating nul byte is appended. + * + * @str: Buffer to store the string + * @size: Maximum number of characters to read (including null terminator) + * @fd: File descriptor to read from + * Return: str on success, NULL on error, or EOF with no characters read + */ +char *os_fgets(char *str, int size, int fd); + /** os_persistent_fname() - Find the path to a test file * * @buf: Buffer to hold path