0c51313e118771901b72954b4fb318a173ae18da
[gnulib.git] / m4 / lseek.m4
1 # lseek.m4 serial 9
2 dnl Copyright (C) 2007, 2009-2012 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_LSEEK],
8 [
9   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
10   AC_REQUIRE([AC_CANONICAL_HOST])
11   AC_REQUIRE([AC_PROG_CC])
12   AC_CHECK_HEADERS_ONCE([unistd.h])
13   AC_CACHE_CHECK([whether lseek detects pipes], [gl_cv_func_lseek_pipe],
14     [case "$host_os" in
15        mingw*)
16          dnl Native Windows.
17          dnl The result of lseek (fd, (off_t)0, SEEK_CUR) or
18          dnl SetFilePointer(handle, 0, NULL, FILE_CURRENT)
19          dnl for a pipe depends on the environment: In a Cygwin 1.5
20          dnl environment it succeeds (wrong); in a Cygwin 1.7 environment
21          dnl it fails with a wrong errno value.
22          gl_cv_func_lseek_pipe=no
23          ;;
24        *)
25          if test $cross_compiling = no; then
26            AC_LINK_IFELSE([AC_LANG_PROGRAM([[
27 #include <sys/types.h> /* for off_t */
28 #include <stdio.h> /* for SEEK_CUR */
29 #if HAVE_UNISTD_H
30 # include <unistd.h>
31 #else /* on Windows with MSVC */
32 # include <io.h>
33 #endif
34 ]], [[
35   /* Exit with success only if stdin is seekable.  */
36   return lseek (0, (off_t)0, SEEK_CUR) < 0;
37 ]])],
38              [if test -s conftest$ac_exeext \
39                  && ./conftest$ac_exeext < conftest.$ac_ext \
40                  && test 1 = "`echo hi \
41                    | { ./conftest$ac_exeext; echo $?; cat >/dev/null; }`"; then
42                 gl_cv_func_lseek_pipe=yes
43               else
44                 gl_cv_func_lseek_pipe=no
45               fi
46              ],
47              [gl_cv_func_lseek_pipe=no])
48          else
49            AC_COMPILE_IFELSE(
50              [AC_LANG_SOURCE([[
51 #if defined __BEOS__
52 /* BeOS mistakenly return 0 when trying to seek on pipes.  */
53   Choke me.
54 #endif]])],
55              [gl_cv_func_lseek_pipe=yes], [gl_cv_func_lseek_pipe=no])
56          fi
57          ;;
58      esac
59     ])
60   if test $gl_cv_func_lseek_pipe = no; then
61     REPLACE_LSEEK=1
62     AC_DEFINE([LSEEK_PIPE_BROKEN], [1],
63       [Define to 1 if lseek does not detect pipes.])
64   fi
65 ])