stat: Update regarding AIX.
[gnulib.git] / m4 / ftello.m4
1 # ftello.m4 serial 7
2 dnl Copyright (C) 2007-2010 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
13   dnl Persuade glibc <stdio.h> to declare ftello().
14   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
15
16   AC_CACHE_CHECK([for ftello], [gl_cv_func_ftello],
17     [
18       AC_TRY_LINK([#include <stdio.h>], [ftello (stdin);],
19         [gl_cv_func_ftello=yes], [gl_cv_func_ftello=no])
20     ])
21   if test $gl_cv_func_ftello = no; then
22     HAVE_FTELLO=0
23   else
24     if test $gl_cv_var_stdin_large_offset = no; then
25       REPLACE_FTELLO=1
26     else
27       dnl Detect bug on Solaris.
28       dnl ftell and ftello produce incorrect results after putc that followed a
29       dnl getc call that reached EOF on Solaris. This is because the _IOREAD
30       dnl flag does not get cleared in this case, even though _IOWRT gets set,
31       dnl and ftell and ftello look whether the _IOREAD flag is set.
32       AC_REQUIRE([AC_CANONICAL_HOST])
33       AC_CACHE_CHECK([whether ftello works],
34         [gl_cv_func_ftello_works],
35         [
36           dnl Initial guess, used when cross-compiling or when /dev/tty cannot
37           dnl be opened.
38 changequote(,)dnl
39           case "$host_os" in
40                       # Guess no on Solaris.
41             solaris*) gl_cv_func_ftello_works="guessing no" ;;
42                       # Guess yes otherwise.
43             *)        gl_cv_func_ftello_works="guessing yes" ;;
44           esac
45 changequote([,])dnl
46           AC_TRY_RUN([
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #define TESTFILE "conftest.tmp"
51 int
52 main (void)
53 {
54   FILE *fp;
55
56   /* Create a file with some contents.  */
57   fp = fopen (TESTFILE, "w");
58   if (fp == NULL)
59     return 70;
60   if (fwrite ("foogarsh", 1, 8, fp) < 8)
61     return 71;
62   if (fclose (fp))
63     return 72;
64
65   /* The file's contents is now "foogarsh".  */
66
67   /* Try writing after reading to EOF.  */
68   fp = fopen (TESTFILE, "r+");
69   if (fp == NULL)
70     return 73;
71   if (fseek (fp, -1, SEEK_END))
72     return 74;
73   if (!(getc (fp) == 'h'))
74     return 1;
75   if (!(getc (fp) == EOF))
76     return 2;
77   if (!(ftell (fp) == 8))
78     return 3;
79   if (!(ftell (fp) == 8))
80     return 4;
81   if (!(putc ('!', fp) == '!'))
82     return 5;
83   if (!(ftell (fp) == 9))
84     return 6;
85   if (!(fclose (fp) == 0))
86     return 7;
87   fp = fopen (TESTFILE, "r");
88   if (fp == NULL)
89     return 75;
90   {
91     char buf[10];
92     if (!(fread (buf, 1, 10, fp) == 9))
93       return 10;
94     if (!(memcmp (buf, "foogarsh!", 9) == 0))
95       return 11;
96   }
97   if (!(fclose (fp) == 0))
98     return 12;
99
100   /* The file's contents is now "foogarsh!".  */
101
102   return 0;
103 }], [gl_cv_func_ftello_works=yes], [gl_cv_func_ftello_works=no], [:])
104         ])
105       case "$gl_cv_func_ftello_works" in
106         *yes) ;;
107         *)
108           REPLACE_FTELLO=1
109           AC_DEFINE([FTELLO_BROKEN_AFTER_SWITCHING_FROM_READ_TO_WRITE], [1],
110             [Define to 1 if the system's ftello function has the Solaris bug.])
111           ;;
112       esac
113     fi
114   fi
115   if test $HAVE_FTELLO = 0 || test $REPLACE_FTELLO = 1; then
116     gl_REPLACE_FTELLO
117   fi
118 ])
119
120 AC_DEFUN([gl_REPLACE_FTELLO],
121 [
122   AC_LIBOBJ([ftello])
123 ])