bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gnulib portability issues


From: Paul Eggert
Subject: Re: gnulib portability issues
Date: Mon, 11 Jun 2012 03:51:22 -0700
User-agent: Mozilla/5.0 (X11; Linux i686; rv:12.0) Gecko/20120430 Thunderbird/12.0.1

On 06/10/2012 07:47 PM, Rich Felker wrote:
> If you think it matters, __freading could be used instead.

That doesn't sound right, since Solaris/glibc __freading returns 1
on read-only streams, whereas we want to know whether
the stream has a nonempty input buffer; this is not
the same thing.

> They're purposefully opaque so that they can be changed if an
> alternate implementation proves better.

Dragonfly BSD attacked this problem by supplying a
function __sreadahead that returns the value in question.
Perhaps musl could do something similar?  That would fix
the problem while preserving opacity.

Another possibility is something like the following patch,
which may fix this particular problem, though I expect that there are
other problems with the opacity that would still be in
there somewhere.

Also, I don't like this patch because it adds runtime overhead
in the non-musl case, but presumably this could be fixed.
(Right now I'm just trying to think through possible solutions.)

diff --git a/lib/freadahead.c b/lib/freadahead.c
index 2ba8b34..db6b68b 100644
--- a/lib/freadahead.c
+++ b/lib/freadahead.c
@@ -84,10 +84,7 @@ freadahead (FILE *fp)
   if (fp->state == 4 /* WR */ || fp->rp >= fp->wp)
     return 0;
   return fp->wp - fp->rp;
-#elif defined SLOW_BUT_NO_HACKS     /* users can define this */
-  abort ();
-  return 0;
 #else
- #error "Please port gnulib freadahead.c to your platform! Look at the 
definition of fflush, fread, ungetc on your system, then report this to 
bug-gnulib."
+  return -1;
 #endif
 }
diff --git a/lib/freadahead.h b/lib/freadahead.h
index d874602..ea0aac7 100644
--- a/lib/freadahead.h
+++ b/lib/freadahead.h
@@ -26,6 +26,8 @@ extern "C" {
    This includes both the bytes that have been read from the underlying input
    source and the bytes that have been pushed back through 'ungetc'.
 
+   Return (size_t) -1 if the number of bytes is not known.
+
    If this number is 0 and the stream is not currently writing,
    fflush (STREAM) is known to be a no-op.
 
diff --git a/lib/freadseek.c b/lib/freadseek.c
index 4145173..66318f7 100644
--- a/lib/freadseek.c
+++ b/lib/freadseek.c
@@ -78,6 +78,8 @@ freadseek (FILE *fp, size_t offset)
   /* Seek over the already read and buffered input as quickly as possible,
      without doing any system calls.  */
   total_buffered = freadahead (fp);
+  if (total_buffered == (size_t) -1)
+    total_buffered = 0;
   /* This loop is usually executed at most twice: once for ungetc buffer (if
      present) and once for the main buffer.  */
   while (total_buffered > 0)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]