On 4/11/22 15:41, Rich Felker wrote: >> But I can't find a portable way to do this? > > To give some context to this question, the __freadahead function > present in musl libc was created in 2012 to resolve a conflict between > gnulib, which has traditionally used an #ifdef jungle to provide > "freadahead" and other functionality by poking at FILE internals for > each known target they support, and musl, which explicitly makes FILE > an opaque non-ABI type. The idea was to let them implement the > function in a way that keeps the private member accesses on the stdio > implementation side. > > While I don't like this interface, gnulib is longstanding historical > precedent for its existence ~somewhere~ (just not as part of the > implementation), and it's historical precedent for major software > wanting this kind of access to stdio.
I just emailed the chair of the C standard group and 90% of his reply was about text vs binary mode with FILE * (which is not present in Linux, MacOS, Android, iOS, Solaris, any embedded OS I've encountered...) I'm personally fine with fileno() returning -1 when the FILE * is in text mode, let alone freadahead(). Even the coreutils developers are noping out of support for things like cygwin now that Windows Subsystem for Linux exists: https://lists.gnu.org/archive/html/coreutils/2022-04/msg00038.html The C committee chair then said: > File descriptors are outside the scope of the C standard, so any > support for switching back and forth between streams and file > descriptors belongs elsewhere. I.E. ANSI C doesn't have read(), write(), or open(). They don't do ANYTHING with file descriptors. That's why fileno() and fdopen() are only in posix, not in ANSI C. And the issues I'm currently trying to solve are a result of getline() showing up in posix-2008, which also does not exist in ANSI C. Thus an freadahead() function to encapsulate the horrible #ifdef staircase people are already repeatedly reinventing belongs in Posix, not ANSI. The function is needed to make Posix's existing fileno() reliable, and in the absence of a standard this has already been reimplemented multiple times. GNU has attempted to centralize its workaround collection in gnulib: https://github.com/digitalocean/gnulib/blob/master/lib/freadahead.c (Leading to a bunch of patches for m4 and glib coming up when you google for freadahead because said staircase breaks a lot.) But even IBM Z/OS implemented __freadahead(): https://www.ibm.com/docs/en/zos/2.4.0?topic=lf-freadahead-retrieve-number-bytes-remaining-in-input-buffer#freadahead The lack of this function derailed a port to VMS a few years back: https://sourceforge.net/p/vms-ports/tickets/61/ Here's somebody trying to update i386 support in buildroot and guess what they needed to patch: https://www.zephray.me/post/create_coremark_boot_disk_for_386/#:~:text=freadahead *shrug* This is a thing people are reimplementing a lot. Rob