On 4/18/22 12:53 AM, 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.

Sure, everything's fixable. It's not what you asked, though.


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

So the gnulib folks looked at a bunch of different stdio implementations
and used non-public (or at least non-standard) portions of the
implementation to agument the stdio API.

If that's what you want to do, propose adding freadahead to the standard.

Or reimplement the gnulib work and accept that the stdio implementation
can potentially change out from under you. Current POSIX provides no help
here.


If you want to
manipulate that information, or expose it to a caller, you can't use FILE *
(or, if you want a direct answer, "you can't").

The if/else staircase in m4 and gnulib and so on says I can.

Not in a way that protects you against changes to one of the underlying
stdio implementations. And isn't that the point? You can always offer that
functionality if you have stable access to stdio internals, but it's not in
the standard.


I was just wondering if there was a _clean_ way to do it.

OK. Do you think you've gotten an answer to that?


The C99 guys point out they haven't got file descriptors and thus this would
logically belong in posix, for the same reason fileno() does. "But FILE *
doesn't have a way to fetch the file descriptor" was answered by adding
fileno(). That is ALSO grabbing an integer out of the guts of FILE *.

Sure. And adding that to the standard would require the usual things, for
which there's a process.

This exists. It would be nice if it got standardized.

Maybe it would. But that's a different question.


--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    c...@case.edu    http://tiswww.cwru.edu/~chet/

Reply via email to