fflush: avoid compilation error on NetBSD
authorEric Blake <ebb9@byu.net>
Mon, 14 Dec 2009 22:42:13 +0000 (15:42 -0700)
committerEric Blake <ebb9@byu.net>
Wed, 16 Dec 2009 12:59:58 +0000 (05:59 -0700)
On NetBSD, the system <stdio.h> header contains:
|#if (!defined(_ANSI_SOURCE) && !defined(__STRICT_ANSI__)) || defined(_LIBC)
|typedef __off_t fpos_t;
|#else
|typedef struct __sfpos {
|    __off_t _pos;
|} fpos_t;
|#endif

Thus, based on compiler flags (such as using 'gcc -ansi' or the
Intel compiler), it is an error to directly set fpos_t=off_t.

* lib/fflush.c (update_fpos_cache): Use a union to safely convert
between off_t and fpos_t, since the latter is sometimes a struct.
* lib/fseeko.c (rpl_fseeko): Likewise.
Reported by Alexander Nasonov <alnsn@yandex.ru>.

Signed-off-by: Eric Blake <ebb9@byu.net>
ChangeLog
lib/fflush.c
lib/fseeko.c

index efa426c..fe1b6f8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2009-12-16  Eric Blake  <ebb9@byu.net>
+
+       fflush: avoid compilation error on NetBSD
+       * lib/fflush.c (update_fpos_cache): Use a union to safely convert
+       between off_t and fpos_t, since the latter is sometimes a struct.
+       * lib/fseeko.c (rpl_fseeko): Likewise.
+       Reported by Alexander Nasonov <alnsn@yandex.ru>.
+
 2009-12-15  Eric Blake  <ebb9@byu.net>
 
        fcntl-h, stdio, sys_ioctl: fix declarations
index 0af1703..d823a34 100644 (file)
@@ -91,7 +91,16 @@ static inline void
 update_fpos_cache (FILE *fp, off_t pos)
 {
 #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
-  fp_->_offset = pos;
+  /* Use a union, since on NetBSD, the compilation flags determine
+     whether fpos_t is typedef'd to off_t or a struct containing a
+     single off_t member.  */
+  union
+    {
+      fpos_t f;
+      off_t o;
+    } u;
+  u.o = pos;
+  fp_->_offset = u.f;
   fp_->_flags |= __SOFF;
 #endif
 }
index 8887f24..91b853d 100644 (file)
@@ -110,7 +110,18 @@ rpl_fseeko (FILE *fp, off_t offset, int whence)
 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
       fp->_flags &= ~_IO_EOF_SEEN;
 #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
-      fp_->_offset = pos;
+      {
+        /* Use a union, since on NetBSD, the compilation flags
+           determine whether fpos_t is typedef'd to off_t or a struct
+           containing a single off_t member.  */
+        union
+          {
+            fpos_t f;
+            off_t o;
+          } u;
+        u.o = pos;
+        fp_->_offset = u.f;
+      }
       fp_->_flags |= __SOFF;
       fp_->_flags &= ~__SEOF;
 #elif defined __EMX__               /* emx+gcc */