bug-gnulib
[Top][All Lists]
Advanced

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

Re: [PATCH] fflush: avoid warnings on modern systems


From: Jim Meyering
Subject: Re: [PATCH] fflush: avoid warnings on modern systems
Date: Mon, 26 Jan 2009 22:33:03 +0100

Eric Blake <address@hidden> wrote:

> According to Jim Meyering on 1/26/2009 10:34 AM:
>> Annoyed that portability cruft is evoking warnings,
>> I propose to add even more cruft ;-)
>> What do you think?
>>
>> Is it worth reindenting for the outer (pos-enclosing) braces I added?
>> I presume C99 decl-after-code is not an option.
>
> The idea looks fine, and you are correct that gnulib should stick with
> C89.  Go ahead and reindent, then commit.

I've also moved the declaration of "pos" down
to the first use, and avoided introducing a bug (I nearly
left a use of "result" outside of its new, more-limited scope).

>From 02a5e81cd5159cf787b725fdf4bf8e712b67ccff Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Mon, 26 Jan 2009 18:32:23 +0100
Subject: [PATCH] fflush: avoid warnings on modern systems

* lib/fflush.c (rpl_fflush): Move declarations of locals,
pos and result, into scopes where they're used.
---
 lib/fflush.c |  117 +++++++++++++++++++++++++++++-----------------------------
 1 files changed, 58 insertions(+), 59 deletions(-)

diff --git a/lib/fflush.c b/lib/fflush.c
index 1292a1b..9f75ccd 100644
--- a/lib/fflush.c
+++ b/lib/fflush.c
@@ -102,9 +102,6 @@ update_fpos_cache (FILE *fp, off_t pos)
 int
 rpl_fflush (FILE *stream)
 {
-  int result;
-  off_t pos;
-
   /* When stream is NULL, POSIX and C99 only require flushing of "output
      streams and update streams in which the most recent operation was not
      input", and all implementations do this.
@@ -134,72 +131,74 @@ rpl_fflush (FILE *stream)
   return fflush (stream);

 #else
-
-  /* Notes about the file-position indicator:
-     1) The file position indicator is incremented by fgetc() and decremented
-        by ungetc():
-        <http://www.opengroup.org/susv3/functions/fgetc.html>
-          "... the fgetc() function shall ... advance the associated file
-           position indicator for the stream ..."
-        <http://www.opengroup.org/susv3/functions/ungetc.html>
-          "The file-position indicator is decremented by each successful
-           call to ungetc()..."
-     2) <http://www.opengroup.org/susv3/functions/ungetc.html> says:
-          "The value of the file-position indicator for the stream after
-           reading or discarding all pushed-back bytes shall be the same
-           as it was before the bytes were pushed back."
-        Here we are discarding all pushed-back bytes.  But more specifically,
-     3) <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> says:
-          "[After fflush(),] the file offset of the underlying open file
-           description shall be set to the file position of the stream, and
-           any characters pushed back onto the stream by ungetc() ... shall
-           be discarded."  */
-
-  /* POSIX does not specify fflush behavior for non-seekable input
-     streams.  Some implementations purge unread data, some return
-     EBADF, some do nothing.  */
-  pos = ftello (stream);
-  if (pos == -1)
+  {
+    /* Notes about the file-position indicator:
+       1) The file position indicator is incremented by fgetc() and decremented
+          by ungetc():
+          <http://www.opengroup.org/susv3/functions/fgetc.html>
+            "... the fgetc() function shall ... advance the associated file
+             position indicator for the stream ..."
+          <http://www.opengroup.org/susv3/functions/ungetc.html>
+            "The file-position indicator is decremented by each successful
+             call to ungetc()..."
+       2) <http://www.opengroup.org/susv3/functions/ungetc.html> says:
+            "The value of the file-position indicator for the stream after
+             reading or discarding all pushed-back bytes shall be the same
+             as it was before the bytes were pushed back."
+          Here we are discarding all pushed-back bytes.  But more specifically,
+       3) <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> says:
+            "[After fflush(),] the file offset of the underlying open file
+             description shall be set to the file position of the stream, and
+             any characters pushed back onto the stream by ungetc() ... shall
+             be discarded."  */
+
+    /* POSIX does not specify fflush behavior for non-seekable input
+       streams.  Some implementations purge unread data, some return
+       EBADF, some do nothing.  */
+    off_t pos = ftello (stream);
+    if (pos == -1)
+      {
+        errno = EBADF;
+        return EOF;
+      }
+
+    /* Clear the ungetc buffer.  */
+    clear_ungetc_buffer (stream);
+
+    /* To get here, we must be flushing a seekable input stream, so the
+       semantics of fpurge are now appropriate to clear the buffer.  To
+       avoid losing data, the lseek is also necessary.  */
     {
-      errno = EBADF;
-      return EOF;
+      int result = fpurge (stream);
+      if (result != 0)
+        return result;
     }

-  /* Clear the ungetc buffer.  */
-  clear_ungetc_buffer (stream);
-
-  /* To get here, we must be flushing a seekable input stream, so the
-     semantics of fpurge are now appropriate to clear the buffer.  To
-     avoid losing data, the lseek is also necessary.  */
-  result = fpurge (stream);
-  if (result != 0)
-    return result;
-
 # if (defined __sferror || defined __DragonFly__) && defined __SNPT /* 
FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */

-  {
-    /* Disable seek optimization for the next fseeko call.  This tells the
-       following fseeko call to seek to the desired position directly, rather
-       than to seek to a block-aligned boundary.  */
-    int saved_flags = disable_seek_optimization (stream);
-
-    result = fseeko (stream, pos, SEEK_SET);
-
-    restore_seek_optimization (stream, saved_flags);
-  }
-  return result;
+    {
+      /* Disable seek optimization for the next fseeko call.  This tells the
+         following fseeko call to seek to the desired position directly, rather
+         than to seek to a block-aligned boundary.  */
+      int saved_flags = disable_seek_optimization (stream);
+      int result = fseeko (stream, pos, SEEK_SET);
+
+      restore_seek_optimization (stream, saved_flags);
+      return result;
+    }

 # else

-  pos = lseek (fileno (stream), pos, SEEK_SET);
-  if (pos == -1)
-    return EOF;
-  /* After a successful lseek, update the file descriptor's position cache
-     in the stream.  */
-  update_fpos_cache (stream, pos);
+    pos = lseek (fileno (stream), pos, SEEK_SET);
+    if (pos == -1)
+      return EOF;
+    /* After a successful lseek, update the file descriptor's position cache
+       in the stream.  */
+    update_fpos_cache (stream, pos);

-  return 0;
+    return 0;

 # endif
+  }
 #endif
 }
--
1.6.1.1.347.g3f81d




reply via email to

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