On 4/17/22 23:53, Rob Landley wrote:
> On 4/17/22 18:10, Chet Ramey wrote:
>> On 4/16/22 2:58 PM, Rob Landley via austin-group-l at The Open Group wrote:
>>> Q) "How do I switch from FILE * to fd via fileno() without losing data."
>>> 
>>> A) "Don't use FILE *"
>>> 
>>> That's not the question I asked?
>> 
>> The answer is correct, but incomplete. The missing piece is that if you
>> want to use FILE *, the operation you want, and the information you need to
>> implement it, are not part of the public API.
> 
> Which is a fixable problem.
> 
>> Other than using a strategy like Geoff suggested early on, or trying
>> something like setvbuf to turn off buffering on the FILE * completely, the
>> buffer associated with a FILE * and the indexes into it that say how much
>> data you've consumed from the underlying source are opaque.
> 
> https://github.com/coreutils/gnulib/blob/master/lib/freadahead.c
> 
> https://sources.debian.org/src/m4/1.4.18-2/lib/freadahead.c

And now it's in bionic:

https://android-review.googlesource.com/c/platform/bionic/+/2227544

Backstory: protocols like http use mbox style data formats with lines of text
followed by a blob of data. I want to use getline() to read the lines of text
(which among other things tells me the size of the ensuing data), and then
sendfile() the data afterwards.

Posix provides fileno(FILE) to hand off an fd from getline() context to
sendfile() context, but data is lost in the transition when the FILE * has read
and buffered extra data. The API to ask how much data is in the buffer (so I can
fread() it and be sure I have all of it, without triggering additional reads
from the underlying fd) is __freadahead(), which posix does not yet standardize.

The C standards committee does not use file descriptors: fileno() is posix.

Rob

Reply via email to