From 1dea0e3d91da0ab9bb3f2173a5364f42d81d7eeb Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sat, 3 Apr 2010 14:25:24 +0200
Subject: [PATCH] ftello: Fix C++ test error on mingw.

---
 ChangeLog      | 11 +++++++++++
 lib/ftello.c   |  7 +++----
 lib/stdio.in.h | 28 ++++++++++++++++++----------
 m4/ftello.m4   | 14 +++++++++-----
 m4/stdio_h.m4  |  3 ++-
 modules/stdio  |  1 +
 6 files changed, 44 insertions(+), 20 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e4f9426101..52cac6da50 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2010-04-03  Bruno Haible  <bruno@clisp.org>
+
+	ftello: Fix C++ test error on mingw.
+	* lib/stdio.in.h (ftello): Use modern idiom.
+	* lib/ftello.c (ftello): Renamed from rpl_ftello.
+	* m4/ftello.m4 (gl_FUNC_FTELLO): Distinguish the case that the function
+	is missing and that it needs to be replaced.
+	(gl_REPLACE_FTELLO): Don't set REPLACE_FTELLO here.
+	* m4/stdio_h.m4 (gl_STDIO_H_DEFAULTS): Initialize HAVE_FTELLO.
+	* modules/stdio (Makefile.am): Substitute HAVE_FTELLO.
+
 2010-04-03  Bruno Haible  <bruno@clisp.org>
 
 	fseeko: Fix C++ test error on mingw.
diff --git a/lib/ftello.c b/lib/ftello.c
index 70cd3592e3..a15e26bc15 100644
--- a/lib/ftello.c
+++ b/lib/ftello.c
@@ -1,5 +1,5 @@
 /* An ftello() function that works around platform bugs.
-   Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc.
+   Copyright (C) 2007, 2009-2010 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
@@ -22,14 +22,13 @@
 /* Get lseek.  */
 #include <unistd.h>
 
+off_t
+ftello (FILE *fp)
 #undef ftello
 #if !HAVE_FTELLO
 # undef ftell
 # define ftello ftell
 #endif
-
-off_t
-rpl_ftello (FILE *fp)
 {
 #if LSEEK_PIPE_BROKEN
   /* mingw gives bogus answers rather than failure on non-seekable files.  */
diff --git a/lib/stdio.in.h b/lib/stdio.in.h
index 25e8bddb9a..58eb4f0b2a 100644
--- a/lib/stdio.in.h
+++ b/lib/stdio.in.h
@@ -406,22 +406,30 @@ _GL_CXXALIASWARN (ftell);
 #   define ftello rpl_ftello
 #  endif
 _GL_FUNCDECL_RPL (ftello, off_t, (FILE *fp) _GL_ARG_NONNULL ((1)));
-#  if !@GNULIB_FTELL@
-    /* In order to avoid that ftell gets defined as a macro here, the
-       developer can request the 'ftell' module.  */
-#   undef ftell
-#   define ftell rpl_ftell
+_GL_CXXALIAS_RPL (ftello, off_t, (FILE *fp));
+# else
+#  if ! @HAVE_FTELLO@
+_GL_FUNCDECL_SYS (ftello, off_t, (FILE *fp) _GL_ARG_NONNULL ((1)));
+#  endif
+_GL_CXXALIAS_SYS (ftello, off_t, (FILE *fp));
+# endif
+_GL_CXXALIASWARN (ftello);
+# if (@REPLACE_FTELLO@ || !@HAVE_FTELLO@) && !@GNULIB_FTELL@
+   /* Provide an ftell function that is consistent with ftello.  */
+   /* In order to avoid that ftell gets defined as a macro here, the
+      developer can request the 'ftell' module.  */
+#  undef ftell
+#  define ftell rpl_ftell
 static inline long _GL_ARG_NONNULL ((1))
 rpl_ftell (FILE *f)
 {
+#  if @REPLACE_FTELLO@
+  return rpl_ftello (f);
+#  else
   return ftello (f);
-}
 #  endif
-_GL_CXXALIAS_RPL (ftello, off_t, (FILE *fp));
-# else
-_GL_CXXALIAS_SYS (ftello, off_t, (FILE *fp));
+}
 # endif
-_GL_CXXALIASWARN (ftello);
 #elif defined GNULIB_POSIXCHECK
 # define _GL_FTELL_WARN /* Category 1, above.  */
 # undef ftell
diff --git a/m4/ftello.m4 b/m4/ftello.m4
index 2dff6ee9a8..fba549a4fc 100644
--- a/m4/ftello.m4
+++ b/m4/ftello.m4
@@ -1,4 +1,4 @@
-# ftello.m4 serial 5
+# ftello.m4 serial 6
 dnl Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -18,8 +18,14 @@ AC_DEFUN([gl_FUNC_FTELLO],
       AC_TRY_LINK([#include <stdio.h>], [ftello (stdin);],
         [gl_cv_func_ftello=yes], [gl_cv_func_ftello=no])
     ])
-  if test $gl_cv_func_ftello = no \
-      || test $gl_cv_var_stdin_large_offset = no; then
+  if test $gl_cv_func_ftello = no; then
+    HAVE_FTELLO=0
+  else
+    if test $gl_cv_var_stdin_large_offset = no; then
+      REPLACE_FTELLO=1
+    fi
+  fi
+  if test $HAVE_FTELLO = 0 || test $REPLACE_FTELLO = 1; then
     gl_REPLACE_FTELLO
   fi
 ])
@@ -27,6 +33,4 @@ AC_DEFUN([gl_FUNC_FTELLO],
 AC_DEFUN([gl_REPLACE_FTELLO],
 [
   AC_LIBOBJ([ftello])
-  AC_REQUIRE([gl_STDIO_H_DEFAULTS])
-  REPLACE_FTELLO=1
 ])
diff --git a/m4/stdio_h.m4 b/m4/stdio_h.m4
index 5b36f8b04a..7b7e9449e0 100644
--- a/m4/stdio_h.m4
+++ b/m4/stdio_h.m4
@@ -1,4 +1,4 @@
-# stdio_h.m4 serial 28
+# stdio_h.m4 serial 29
 dnl Copyright (C) 2007-2010 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -100,6 +100,7 @@ AC_DEFUN([gl_STDIO_H_DEFAULTS],
   HAVE_DECL_VSNPRINTF=1;         AC_SUBST([HAVE_DECL_VSNPRINTF])
   HAVE_DPRINTF=1;                AC_SUBST([HAVE_DPRINTF])
   HAVE_FSEEKO=1;                 AC_SUBST([HAVE_FSEEKO])
+  HAVE_FTELLO=1;                 AC_SUBST([HAVE_FTELLO])
   HAVE_RENAMEAT=1;               AC_SUBST([HAVE_RENAMEAT])
   HAVE_VASPRINTF=1;              AC_SUBST([HAVE_VASPRINTF])
   HAVE_VDPRINTF=1;               AC_SUBST([HAVE_VDPRINTF])
diff --git a/modules/stdio b/modules/stdio
index 50eae5087f..faa07ccc76 100644
--- a/modules/stdio
+++ b/modules/stdio
@@ -77,6 +77,7 @@ stdio.h: stdio.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	      -e 's|@''HAVE_DECL_VSNPRINTF''@|$(HAVE_DECL_VSNPRINTF)|g' \
 	      -e 's|@''HAVE_DPRINTF''@|$(HAVE_DPRINTF)|g' \
 	      -e 's|@''HAVE_FSEEKO''@|$(HAVE_FSEEKO)|g' \
+	      -e 's|@''HAVE_FTELLO''@|$(HAVE_FTELLO)|g' \
 	      -e 's|@''HAVE_RENAMEAT''@|$(HAVE_RENAMEAT)|g' \
 	      -e 's|@''HAVE_VASPRINTF''@|$(HAVE_VASPRINTF)|g' \
 	      -e 's|@''HAVE_VDPRINTF''@|$(HAVE_VDPRINTF)|g' \
-- 
2.30.2