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