bug-gnulib
[Top][All Lists]
Advanced

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

workaround ftello/fseeko on cygwin 1.5.x


From: Eric Blake
Subject: workaround ftello/fseeko on cygwin 1.5.x
Date: Wed, 23 May 2007 10:49:14 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.10) Gecko/20070221 Thunderbird/1.5.0.10 Mnenhy/0.7.5.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Cygwin CVS is already fixed for the upcoming 1.7.0, but as 1.5.24 is the
current stable version, is this patch okay to apply?  It overcomes the
fact that cygwin was creating the three std streams in 32-bit mode instead
of 64-bit mode, such that fseeko/ftello failed because they expected the
stream to be in 64-bit mode.  I couldn't figure out how to guarantee a
seekable stdin for writing an AC_RUN_IFELSE test that could detect the
bug, so I used AC_LINK_IFELSE instead, using a function (asnprintf) that
will only link on cygwin if the CVS patch for the std streams is also present.

2007-05-23  Eric Blake  <address@hidden>

        Fix fseeko/ftello on cygwin 1.5.24.
        * tests/test-fseeko.c (main): Check for broken fseeko on cygwin
        prior to 1.7.0.
        * tests/test-ftello.c (main): Likewise for ftello.
        * tests/test-fseeko.sh: New file.
        * tests/test-ftello.sh: New file.
        * modules/fseeko-tests (Makefile.am): Ensure test-fseeko is run
        with seekable stdin.
        * modules/ftello-tests (Makefile.am): Likewise for test-ftello.
        * m4/fseeko.m4 (gl_FUNC_FSEEKO): Detect the cygwin bug.
        (gl_REPLACE_FSEEKO): New macro.
        * m4/ftello.m4 (gl_FUNC_FTELLO, gl_REPLACE_FTELLO): Likewise.
        * modules/fseeko (Files): Distribute fseeko.c.
        * modules/ftello (Files): Distribute ftello.c.
        * lib/fseeko.c (rpl_fseeko) [__CYGWIN__]: Convert stdin to 64-bit
        mode.
        * lib/ftello.c (rpl_ftello): New file.
        * m4/stdio_h.m4 (gl_STDIO_H_DEFAULTS): Allow replacement of
        fseeko, ftello.
        * modules/stdio (MAkefile.am): Perform the replacement.
        * lib/stdio_.h (rpl_fseeko, rpl_ftello): Define when needed.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGVHCJ84KuGfSFAYARArRSAJ9O3tB00PhN4Iau14GyuPzzQYGYWwCeNH5q
Wsjr78+4k1YNXP7+TvhzmGM=
=ciWI
-----END PGP SIGNATURE-----
Index: lib/fseeko.c
===================================================================
RCS file: /sources/gnulib/gnulib/lib/fseeko.c,v
retrieving revision 1.3
diff -u -p -r1.3 fseeko.c
--- lib/fseeko.c        29 Apr 2007 12:16:55 -0000      1.3
+++ lib/fseeko.c        23 May 2007 16:44:57 -0000
@@ -44,6 +44,19 @@ rpl_fseeko (FILE *fp, off_t offset, int 
 # else                                         /* FreeBSD, MacOS X, Cygwin */
 #  define fp_ub fp->_ub
 # endif
+# if defined __SL64 && defined __SCLE /* Cygwin */
+  if ((fp->_flags & __SL64) == 0)
+    {
+      /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
+        mode; but has an fseeko that requires 64-bit mode.  */
+      FILE *tmp = fopen ("/dev/null", "r");
+      if (!tmp)
+       return -1;
+      fp->_flags |= __SL64;
+      fp->_seek64 = tmp->_seek64;
+      fclose (tmp);
+    }
+# endif
   if (fp->_p == fp->_bf._base
       && fp->_r == 0
       && fp->_w == ((fp->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully 
buffered and not currently reading? */
Index: lib/ftello.c
===================================================================
RCS file: lib/ftello.c
diff -N lib/ftello.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/ftello.c        23 May 2007 16:44:57 -0000
@@ -0,0 +1,45 @@
+/* An ftello() function that works around platform bugs.
+   Copyright (C) 2007 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License along
+   with this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <stdio.h>
+
+#undef ftello
+#if !HAVE_FTELLO
+# define ftello ftell
+#endif
+
+off_t
+rpl_ftello (FILE *fp)
+{
+#if defined __SL64 && defined __SCLE /* Cygwin */
+  if ((fp->_flags & __SL64) == 0)
+    {
+      /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
+        mode; but has an ftello that requires 64-bit mode.  */
+      FILE *tmp = fopen ("/dev/null", "r");
+      if (!tmp)
+       return -1;
+      fp->_flags |= __SL64;
+      fp->_seek64 = tmp->_seek64;
+      fclose (tmp);
+    }
+#endif
+  return ftello (fp);
+}
Index: lib/stdio_.h
===================================================================
RCS file: /sources/gnulib/gnulib/lib/stdio_.h,v
retrieving revision 1.24
diff -u -p -r1.24 stdio_.h
--- lib/stdio_.h        17 May 2007 06:14:29 -0000      1.24
+++ lib/stdio_.h        23 May 2007 16:44:57 -0000
@@ -42,7 +42,9 @@
 #include <stdarg.h>
 #include <stddef.h>
 
-#if (@GNULIB_FFLUSH@ && @REPLACE_FFLUSH@) || (@GNULIB_FSEEKO@ && 
address@hidden@) || (@GNULIB_FTELLO@ && address@hidden@)
+#if (@GNULIB_FFLUSH@ && @REPLACE_FFLUSH@) \
+  || (@GNULIB_FSEEKO@ && (address@hidden@ || @REPLACE_FSEEKO@)) \
+  || (@GNULIB_FTELLO@ && (address@hidden@ || @REPLACE_FTELLO@))
 /* Get off_t.  */
 # include <sys/types.h>
 #endif
@@ -216,7 +218,7 @@ extern int vsprintf (char *str, const ch
 # endif
 #endif
 
-#if @GNULIB_FFLUSH@ && @REPLACE_FFLUSH@
+#if (@GNULIB_FFLUSH@ && @REPLACE_FFLUSH@) || (@GNULIB_FSEEKO@ && 
@REPLACE_FSEEKO@)
 /* Provide fseek, fseeko functions that are aware of a preceding fflush().  */
 # define fseeko rpl_fseeko
 extern int fseeko (FILE *fp, off_t offset, int whence);
@@ -233,6 +235,11 @@ typedef int verify_fseeko_types[2 * (siz
    (GL_LINK_WARNING ("fseeko is unportable - " \
                      "use gnulib module fseeko for portability"), \
     fseeko (f, o, w))
+# undef fseek
+# define fseek(f,o,w) \
+   (GL_LINK_WARNING ("fseek is artificially limited - " \
+                     "use gnulib module fseeko for portability"), \
+    fseek (f, o, w))
 #endif
 
 #if defined GNULIB_POSIXCHECK
@@ -250,6 +257,9 @@ typedef int verify_fseeko_types[2 * (siz
 /* Assume 'off_t' is the same type as 'long'.  */
 typedef int verify_ftello_types[2 * (sizeof (off_t) == sizeof (long)) - 1];
 #  define ftello ftell
+# elif @REPLACE_FTELLO@
+#  define ftello rpl_ftello
+extern off_t ftello (FILE *fp);
 # endif
 #else
 # undef ftello
@@ -257,6 +267,11 @@ typedef int verify_ftello_types[2 * (siz
    (GL_LINK_WARNING ("ftello is unportable - " \
                      "use gnulib module ftello for portability"), \
     ftello (f))
+# undef ftell
+# define ftell(f) \
+   (GL_LINK_WARNING ("ftell is artificially limited - " \
+                     "use gnulib module ftello for portability"), \
+    ftell (f))
 #endif
 
 #if defined GNULIB_POSIXCHECK
Index: m4/stdio_h.m4
===================================================================
RCS file: /sources/gnulib/gnulib/m4/stdio_h.m4,v
retrieving revision 1.13
diff -u -p -r1.13 stdio_h.m4
--- m4/stdio_h.m4       25 Apr 2007 07:51:53 -0000      1.13
+++ m4/stdio_h.m4       23 May 2007 16:44:57 -0000
@@ -47,6 +47,8 @@ AC_DEFUN([gl_STDIO_H_DEFAULTS],
   HAVE_VASPRINTF=1;        AC_SUBST([HAVE_VASPRINTF])
   REPLACE_VASPRINTF=0;     AC_SUBST([REPLACE_VASPRINTF])
   HAVE_FSEEKO=1;           AC_SUBST([HAVE_FSEEKO])
+  REPLACE_FSEEKO=0;        AC_SUBST([REPLACE_FSEEKO])
   HAVE_FTELLO=1;           AC_SUBST([HAVE_FTELLO])
+  REPLACE_FTELLO=0;        AC_SUBST([REPLACE_FTELLO])
   REPLACE_FFLUSH=0;        AC_SUBST([REPLACE_FFLUSH])
 ])
Index: m4/fseeko.m4
===================================================================
RCS file: /sources/gnulib/gnulib/m4/fseeko.m4,v
retrieving revision 1.3
diff -u -p -r1.3 fseeko.m4
--- m4/fseeko.m4        26 Apr 2007 09:33:12 -0000      1.3
+++ m4/fseeko.m4        23 May 2007 16:44:57 -0000
@@ -1,4 +1,4 @@
-# fseeko.m4 serial 1
+# fseeko.m4 serial 2
 dnl Copyright (C) 2007 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -8,6 +8,7 @@ AC_DEFUN([gl_FUNC_FSEEKO],
 [
   AC_REQUIRE([gl_STDIO_H_DEFAULTS])
   AC_REQUIRE([AC_PROG_CC])
+  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
   AC_CACHE_CHECK([for fseeko], [gl_cv_func_fseeko],
     [
       AC_TRY_LINK([#include <stdio.h>], [fseeko (stdin, 0, 0);],
@@ -15,5 +16,32 @@ AC_DEFUN([gl_FUNC_FSEEKO],
     ])
   if test $gl_cv_func_fseeko = no; then
     HAVE_FSEEKO=0
+  else
+    AC_CACHE_CHECK([if fseeko(stdin) works], [gl_cv_func_fseeko_stdin],
+      [
+        AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <stdio.h>],
+          [
+#if defined __SL64 && defined __SCLE /* cygwin */
+   /* Cygwin 1.5.24 and earlier fail to put stdin in 64-bit mode, making
+      fseeko/ftello needlessly fail.  This bug was fixed at the same time
+      that cygwin started exporting asnprintf (cygwin 1.7.0), so we use
+      that as a link-time test for cross-compiles rather than building
+      a runtime test.  */
+   size_t s;
+   if (asnprintf (NULL, &s, ""))
+     return 0;
+#endif
+            ])],
+          [gl_cv_func_fseeko_stdin=yes], [gl_cv_func_fseeko_stdin=no])])
+    if test $gl_cv_func_fseeko_stdin = no; then
+      gl_REPLACE_FSEEKO
+    fi
   fi
 ])
+
+AC_DEFUN([gl_REPLACE_FSEEKO],
+[
+  AC_LIBOBJ([fseeko])
+  AC_REQUIRE([gl_STDIO_H_DEFAULTS])
+  REPLACE_FSEEKO=1
+])
Index: m4/ftello.m4
===================================================================
RCS file: /sources/gnulib/gnulib/m4/ftello.m4,v
retrieving revision 1.1
diff -u -p -r1.1 ftello.m4
--- m4/ftello.m4        25 Apr 2007 07:51:53 -0000      1.1
+++ m4/ftello.m4        23 May 2007 16:44:57 -0000
@@ -1,4 +1,4 @@
-# ftello.m4 serial 1
+# ftello.m4 serial 2
 dnl Copyright (C) 2007 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -15,5 +15,32 @@ AC_DEFUN([gl_FUNC_FTELLO],
     ])
   if test $gl_cv_func_ftello = no; then
     HAVE_FTELLO=0
+  else
+    AC_CACHE_CHECK([if ftello(stdin) works], [gl_cv_func_ftello_stdin],
+      [
+        AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <stdio.h>],
+          [
+#if defined __SL64 && defined __SCLE /* cygwin */
+   /* Cygwin 1.5.24 and earlier fail to put stdin in 64-bit mode, making
+      fseeko/ftello needlessly fail.  This bug was fixed at the same time
+      that cygwin started exporting asnprintf (cygwin 1.7.0), so we use
+      that as a link-time test for cross-compiles rather than building
+      a runtime test.  */
+   size_t s;
+   if (asnprintf (NULL, &s, ""))
+     return 0;
+#endif
+            ])],
+          [gl_cv_func_ftello_stdin=yes], [gl_cv_func_ftello_stdin=no])])
+    if test $gl_cv_func_ftello_stdin = no; then
+      gl_REPLACE_FTELLO
+    fi
   fi
 ])
+
+AC_DEFUN([gl_REPLACE_FTELLO],
+[
+  AC_LIBOBJ([ftello])
+  AC_REQUIRE([gl_STDIO_H_DEFAULTS])
+  REPLACE_FTELLO=1
+])
Index: modules/stdio
===================================================================
RCS file: /sources/gnulib/gnulib/modules/stdio,v
retrieving revision 1.14
diff -u -p -r1.14 stdio
--- modules/stdio       17 May 2007 06:14:30 -0000      1.14
+++ modules/stdio       23 May 2007 16:44:57 -0000
@@ -47,7 +47,9 @@ stdio.h: stdio_.h
              -e 's|@''HAVE_VASPRINTF''@|$(HAVE_VASPRINTF)|g' \
              -e 's|@''REPLACE_VASPRINTF''@|$(REPLACE_VASPRINTF)|g' \
              -e 's|@''HAVE_FSEEKO''@|$(HAVE_FSEEKO)|g' \
+             -e 's|@''REPLACE_FSEEKO''@|$(REPLACE_FSEEKO)|g' \
              -e 's|@''HAVE_FTELLO''@|$(HAVE_FTELLO)|g' \
+             -e 's|@''REPLACE_FTELLO''@|$(REPLACE_FTELLO)|g' \
              -e 's|@''REPLACE_FFLUSH''@|$(REPLACE_FFLUSH)|g' \
              -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \
              < $(srcdir)/stdio_.h; \
Index: modules/fseeko
===================================================================
RCS file: /sources/gnulib/gnulib/modules/fseeko,v
retrieving revision 1.2
diff -u -p -r1.2 fseeko
--- modules/fseeko      26 Apr 2007 09:33:12 -0000      1.2
+++ modules/fseeko      23 May 2007 16:44:57 -0000
@@ -2,6 +2,7 @@ Description:
 fseeko() function: Reposition a FILE stream.
 
 Files:
+lib/fseeko.c
 m4/fseeko.m4
 
 Depends-on:
Index: modules/fseeko-tests
===================================================================
RCS file: /sources/gnulib/gnulib/modules/fseeko-tests,v
retrieving revision 1.1
diff -u -p -r1.1 fseeko-tests
--- modules/fseeko-tests        25 Apr 2007 07:40:58 -0000      1.1
+++ modules/fseeko-tests        23 May 2007 16:44:57 -0000
@@ -1,10 +1,14 @@
 Files:
 tests/test-fseeko.c
+tests/test-fseeko.sh
 
 Depends-on:
 
 configure.ac:
 
 Makefile.am:
-TESTS += test-fseeko
+TESTS += test-fseeko.sh
+TESTS_ENVIRONMENT += EXEEXT='@EXEEXT@' srcdir='$(srcdir)'
 check_PROGRAMS += test-fseeko
+EXTRA_DIST += test-fseeko.sh
+
Index: modules/ftello
===================================================================
RCS file: /sources/gnulib/gnulib/modules/ftello,v
retrieving revision 1.2
diff -u -p -r1.2 ftello
--- modules/ftello      26 Apr 2007 09:33:12 -0000      1.2
+++ modules/ftello      23 May 2007 16:44:57 -0000
@@ -2,6 +2,7 @@ Description:
 ftello() function: Retrieve the position of a FILE stream.
 
 Files:
+lib/ftello.c
 m4/ftello.m4
 
 Depends-on:
Index: modules/ftello-tests
===================================================================
RCS file: /sources/gnulib/gnulib/modules/ftello-tests,v
retrieving revision 1.1
diff -u -p -r1.1 ftello-tests
--- modules/ftello-tests        25 Apr 2007 07:52:28 -0000      1.1
+++ modules/ftello-tests        23 May 2007 16:44:57 -0000
@@ -1,10 +1,13 @@
 Files:
 tests/test-ftello.c
+tests/test-ftello.sh
 
 Depends-on:
 
 configure.ac:
 
 Makefile.am:
-TESTS += test-ftello
+TESTS += test-ftello.sh
+TESTS_ENVIRONMENT += EXEEXT='@EXEEXT@' srcdir='$(srcdir)'
 check_PROGRAMS += test-ftello
+EXTRA_DIST += test-ftello.sh
Index: tests/test-fseeko.c
===================================================================
RCS file: /sources/gnulib/gnulib/tests/test-fseeko.c,v
retrieving revision 1.1
diff -u -p -r1.1 test-fseeko.c
--- tests/test-fseeko.c 25 Apr 2007 07:40:58 -0000      1.1
+++ tests/test-fseeko.c 23 May 2007 16:44:57 -0000
@@ -27,8 +27,6 @@
 int
 main ()
 {
-  off_t pos = fseeko (stdin, (off_t)0, SEEK_CUR);
-  (void)pos;
-
-  return 0;
+  /* This test assumes it was invoked with stdin as a seekable file.  */
+  return fseeko (stdin, (off_t)0, SEEK_SET) != 0;
 }
Index: tests/test-fseeko.sh
===================================================================
RCS file: tests/test-fseeko.sh
diff -N tests/test-fseeko.sh
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ tests/test-fseeko.sh        23 May 2007 16:44:57 -0000
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+./test-fseeko${EXEEXT} < "$srcdir/test-fseeko.sh"
Index: tests/test-ftello.c
===================================================================
RCS file: /sources/gnulib/gnulib/tests/test-ftello.c,v
retrieving revision 1.1
diff -u -p -r1.1 test-ftello.c
--- tests/test-ftello.c 25 Apr 2007 07:52:28 -0000      1.1
+++ tests/test-ftello.c 23 May 2007 16:44:57 -0000
@@ -27,8 +27,7 @@
 int
 main ()
 {
+  /* This test assumes stdin is seekable.  */
   off_t pos = ftello (stdin);
-  (void)pos;
-
-  return 0;
+  return pos < 0;
 }
Index: tests/test-ftello.sh
===================================================================
RCS file: tests/test-ftello.sh
diff -N tests/test-ftello.sh
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ tests/test-ftello.sh        23 May 2007 16:44:57 -0000
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+./test-ftello${EXEEXT} < "$srcdir/test-ftello.sh"

reply via email to

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