From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5AB9632AABB for ; Mon, 5 Jan 2026 09:27:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767605260; cv=none; b=WhjDfiFbjaiKg8ODkL1J6xer0dJlFLWD48/g5nlnxCBkNNzlRXq3Q6VlKoFWIhSmjJBFNT+PCQuBvt++a8Yek7trXkiA2pqWgG6Cg1q31xCNV8ct+JrzZB7WzYqFd0X2A4Hk1X9xWF1vdGS+/VRz20Zm21TFuaWMjyWK/SB/BlU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767605260; c=relaxed/simple; bh=SlMB3/90UyALEA5ORdKel9fFfpLl+5yqRbEfSKnnF4g=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=q2yeKVA1sT1XgqbB8wcGlyHymFPN3Ix5ngk0nwnP819dPw3KWoIKlc9WBj/YpAf6ElQl+5KdJuecqXoEPycUDA9TrxbGT5p8fRy6DvqsVVvQa4WnWbEU8ygvYj0smlVJb1hyS20US33gRMOZQ3/9Gn/UzkL+JBkR2t8AscpdXzY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MErUqmfk; arc=none smtp.client-ip=209.85.128.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MErUqmfk" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4779cc419b2so108419625e9.3 for ; Mon, 05 Jan 2026 01:27:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767605251; x=1768210051; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=yRnIyCcTGatUFDkC0J2yq4jTqhNofq6kpujJBtNd2EQ=; b=MErUqmfkgPc2mVTLGdQ5W4ScuhXIj3YMw/OhmgxWrCznQ3DW0SWQP2cYDUBoCrcwKH /GrnO1CgIkaYPOtMf0mNNL5QZSGGfLDAiEzn9llgtDtapVKEjsFtNWuF7ZVWUrinH9y9 NXoPpqsGWpINJPZg2JC+6GJBxa71bEjQJSiVhEpl6Nj2tqvHNSrXdyHeICSElJd+7Waq HSpADg/SE7aZLeN6R1lSvRj7nCTomCL+POPqYUxtDjWcw5bb5uFRzzOU4tZhGZXqVD3r hMmxi3AWC5MrUiee4Xi7xo3siDuPmXb6eH7R+T7bKRMPvL9+GyZRaAO//oXROj7dRTdI iQhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767605251; x=1768210051; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=yRnIyCcTGatUFDkC0J2yq4jTqhNofq6kpujJBtNd2EQ=; b=J0zn74JvfTX60F7coKE62kMnN02tQruE7NtyybvZDJCLZIqJ9kyI0HE+IsUgo7g3iF tFZUcj2PxTNyt/Zq9qzAXBTGEdsb9ZVQZx8T9rHMCKEE9J+Tw537EOqoawr13faOS9BJ maW7Jq3Bq3HmQLLpwnohiAj/TAnDE/VknIKIK8FG39EFn1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f17b1804b1-47d1958b091mr522266245e9.20.1767605250972; Mon, 05 Jan 2026 01:27:30 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47d6d1451f8sm145684845e9.5.2026.01.05.01.27.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jan 2026 01:27:30 -0800 (PST) Date: Mon, 5 Jan 2026 09:27:29 +0000 From: David Laight To: Daniel Palmer Cc: w@1wt.eu, linux@weissschuh.net, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h Message-ID: <20260105092729.386bac33@pumpkin> In-Reply-To: References: <20260104083837.1390041-1-daniel@thingy.jp> <20260104083837.1390041-2-daniel@thingy.jp> <20260104183452.57213367@pumpkin> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Mon, 5 Jan 2026 09:54:10 +0900 Daniel Palmer wrote: > Hi David, > > On Mon, 5 Jan 2026 at 03:34, David Laight wrote: > > > +static __attribute__((unused)) > > > +int _fread(void *buf, size_t size, FILE *stream) > > > +{ > > > + ssize_t ret; > > > + int fd = fileno(stream); > > > + > > > + while (size) { > > > + ret = read(fd, buf, size); > > > + if (ret <= 0) > > > + return EOF; > > > > You need to return a partial length if some data was read before EOF. > > According to the man page: > > On success, fread() and fwrite() return the number of items read > or written. This number equals the number of bytes transferred > only when size is 1. If an error occurs, or the end of the file > is reached, the return value is a short item count (or zero). > > So I think the current logic is correct? If you have a file that has a > length that is not a multiple of the item size and try to read more > items than possible (i.e. you have 3 bytes in the file, you try to > read 2 2 byte items) we read and count the items that are possible, > the partial data is read but the read loop returns EOF, the partial > item isn't counted and fread() returns the number of fully read items. But you've deleted the partial bytes from the input. I'm sure that isn't right. Normally a FILE is buffered and the bytes are saved for the next read. Remember you can be reading from a pipe that is being written using 'block buffering' - so it is valid for only a partial 'item' be read. (I'm sure non-blocking IO is also valid...) > _fread() could return the amount that was partially read but fread() > is only checking for non-zero so there wouldn't be any difference. > > Maybe I'm missing something? What you are doing is equivalent to a long read and then dividing the number of bytes read by the item size. So you might as well do a single read system call. David > > Cheers, > > Daniel