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