Imported Upstream version 3.2.0
[debian/amanda] / config / gnulib / 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_RUN_IFELSE(
51             [AC_LANG_SOURCE([[
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #define TESTFILE "conftest.tmp"
56 int
57 main (void)
58 {
59   FILE *fp;
60
61   /* Create a file with some contents.  */
62   fp = fopen (TESTFILE, "w");
63   if (fp == NULL)
64     return 70;
65   if (fwrite ("foogarsh", 1, 8, fp) < 8)
66     return 71;
67   if (fclose (fp))
68     return 72;
69
70   /* The file's contents is now "foogarsh".  */
71
72   /* Try writing after reading to EOF.  */
73   fp = fopen (TESTFILE, "r+");
74   if (fp == NULL)
75     return 73;
76   if (fseek (fp, -1, SEEK_END))
77     return 74;
78   if (!(getc (fp) == 'h'))
79     return 1;
80   if (!(getc (fp) == EOF))
81     return 2;
82   if (!(ftell (fp) == 8))
83     return 3;
84   if (!(ftell (fp) == 8))
85     return 4;
86   if (!(putc ('!', fp) == '!'))
87     return 5;
88   if (!(ftell (fp) == 9))
89     return 6;
90   if (!(fclose (fp) == 0))
91     return 7;
92   fp = fopen (TESTFILE, "r");
93   if (fp == NULL)
94     return 75;
95   {
96     char buf[10];
97     if (!(fread (buf, 1, 10, fp) == 9))
98       return 10;
99     if (!(memcmp (buf, "foogarsh!", 9) == 0))
100       return 11;
101   }
102   if (!(fclose (fp) == 0))
103     return 12;
104
105   /* The file's contents is now "foogarsh!".  */
106
107   return 0;
108 }]])],
109             [gl_cv_func_ftello_works=yes],
110             [gl_cv_func_ftello_works=no], [:])
111         ])
112       case "$gl_cv_func_ftello_works" in
113         *yes) ;;
114         *)
115           REPLACE_FTELLO=1
116           AC_DEFINE([FTELLO_BROKEN_AFTER_SWITCHING_FROM_READ_TO_WRITE], [1],
117             [Define to 1 if the system's ftello function has the Solaris bug.])
118           ;;
119       esac
120     fi
121   fi
122   if test $HAVE_FTELLO = 0 || test $REPLACE_FTELLO = 1; then
123     gl_REPLACE_FTELLO
124   fi
125 ])
126
127 AC_DEFUN([gl_REPLACE_FTELLO],
128 [
129   AC_LIBOBJ([ftello])
130 ])