New upstream version 1.8
[debian/gzip] / m4 / ftello.m4
1 # ftello.m4 serial 11
2 dnl Copyright (C) 2007-2016 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_FUNC_FTELLO],
8 [
9   AC_REQUIRE([gl_STDIO_H_DEFAULTS])
10   AC_REQUIRE([AC_PROG_CC])
11   AC_REQUIRE([gl_STDIN_LARGE_OFFSET])
12   AC_REQUIRE([gl_SYS_TYPES_H])
13
14   dnl Persuade glibc <stdio.h> to declare ftello().
15   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
16
17   AC_CHECK_DECLS_ONCE([ftello])
18   if test $ac_cv_have_decl_ftello = no; then
19     HAVE_DECL_FTELLO=0
20   fi
21
22   AC_CACHE_CHECK([for ftello], [gl_cv_func_ftello],
23     [
24       AC_LINK_IFELSE(
25         [AC_LANG_PROGRAM(
26            [[#include <stdio.h>]],
27            [[ftello (stdin);]])],
28         [gl_cv_func_ftello=yes],
29         [gl_cv_func_ftello=no])
30     ])
31   if test $gl_cv_func_ftello = no; then
32     HAVE_FTELLO=0
33   else
34     if test $WINDOWS_64_BIT_OFF_T = 1; then
35       REPLACE_FTELLO=1
36     fi
37     if test $gl_cv_var_stdin_large_offset = no; then
38       REPLACE_FTELLO=1
39     fi
40     if test $REPLACE_FTELLO = 0; then
41       dnl Detect bug on Solaris.
42       dnl ftell and ftello produce incorrect results after putc that followed a
43       dnl getc call that reached EOF on Solaris. This is because the _IOREAD
44       dnl flag does not get cleared in this case, even though _IOWRT gets set,
45       dnl and ftell and ftello look whether the _IOREAD flag is set.
46       AC_REQUIRE([AC_CANONICAL_HOST])
47       AC_CACHE_CHECK([whether ftello works],
48         [gl_cv_func_ftello_works],
49         [
50           dnl Initial guess, used when cross-compiling or when /dev/tty cannot
51           dnl be opened.
52 changequote(,)dnl
53           case "$host_os" in
54                       # Guess no on Solaris.
55             solaris*) gl_cv_func_ftello_works="guessing no" ;;
56                       # Guess yes otherwise.
57             *)        gl_cv_func_ftello_works="guessing yes" ;;
58           esac
59 changequote([,])dnl
60           AC_RUN_IFELSE(
61             [AC_LANG_SOURCE([[
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #define TESTFILE "conftest.tmp"
66 int
67 main (void)
68 {
69   FILE *fp;
70
71   /* Create a file with some contents.  */
72   fp = fopen (TESTFILE, "w");
73   if (fp == NULL)
74     return 70;
75   if (fwrite ("foogarsh", 1, 8, fp) < 8)
76     return 71;
77   if (fclose (fp))
78     return 72;
79
80   /* The file's contents is now "foogarsh".  */
81
82   /* Try writing after reading to EOF.  */
83   fp = fopen (TESTFILE, "r+");
84   if (fp == NULL)
85     return 73;
86   if (fseek (fp, -1, SEEK_END))
87     return 74;
88   if (!(getc (fp) == 'h'))
89     return 1;
90   if (!(getc (fp) == EOF))
91     return 2;
92   if (!(ftell (fp) == 8))
93     return 3;
94   if (!(ftell (fp) == 8))
95     return 4;
96   if (!(putc ('!', fp) == '!'))
97     return 5;
98   if (!(ftell (fp) == 9))
99     return 6;
100   if (!(fclose (fp) == 0))
101     return 7;
102   fp = fopen (TESTFILE, "r");
103   if (fp == NULL)
104     return 75;
105   {
106     char buf[10];
107     if (!(fread (buf, 1, 10, fp) == 9))
108       return 10;
109     if (!(memcmp (buf, "foogarsh!", 9) == 0))
110       return 11;
111   }
112   if (!(fclose (fp) == 0))
113     return 12;
114
115   /* The file's contents is now "foogarsh!".  */
116
117   return 0;
118 }]])],
119             [gl_cv_func_ftello_works=yes],
120             [gl_cv_func_ftello_works=no], [:])
121         ])
122       case "$gl_cv_func_ftello_works" in
123         *yes) ;;
124         *)
125           REPLACE_FTELLO=1
126           AC_DEFINE([FTELLO_BROKEN_AFTER_SWITCHING_FROM_READ_TO_WRITE], [1],
127             [Define to 1 if the system's ftello function has the Solaris bug.])
128           ;;
129       esac
130     fi
131   fi
132 ])
133
134 # Prerequisites of lib/ftello.c.
135 AC_DEFUN([gl_PREREQ_FTELLO],
136 [
137   dnl Native Windows has the function _ftelli64. mingw hides it, but mingw64
138   dnl makes it usable again.
139   AC_CHECK_FUNCS([_ftelli64])
140 ])