86db59ecdc064d7c231e9f08f12944a19c84af8e
[gnulib.git] / m4 / lseek.m4
1 # lseek.m4 serial 8
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_PROG_CC])
11   AC_CHECK_HEADERS_ONCE([unistd.h])
12   AC_CACHE_CHECK([whether lseek detects pipes], [gl_cv_func_lseek_pipe],
13     [if test $cross_compiling = no; then
14        AC_LINK_IFELSE([AC_LANG_PROGRAM([[
15 #include <sys/types.h> /* for off_t */
16 #include <stdio.h> /* for SEEK_CUR */
17 #if HAVE_UNISTD_H
18 # include <unistd.h>
19 #else /* on Windows with MSVC */
20 # include <io.h>
21 #endif
22 ]], [[
23   /* Exit with success only if stdin is seekable.  */
24   return lseek (0, (off_t)0, SEEK_CUR) < 0;
25 ]])],
26          [if test -s conftest$ac_exeext \
27              && ./conftest$ac_exeext < conftest.$ac_ext \
28              && test 1 = "`echo hi \
29                | { ./conftest$ac_exeext; echo $?; cat >/dev/null; }`"; then
30             gl_cv_func_lseek_pipe=yes
31           else
32             gl_cv_func_lseek_pipe=no
33           fi],
34          [gl_cv_func_lseek_pipe=no])
35      else
36        AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
37 #if ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__) || defined __BEOS__
38 /* mingw and BeOS mistakenly return 0 when trying to seek on pipes.  */
39   Choke me.
40 #endif]])],
41          [gl_cv_func_lseek_pipe=yes], [gl_cv_func_lseek_pipe=no])
42      fi])
43   if test $gl_cv_func_lseek_pipe = no; then
44     REPLACE_LSEEK=1
45     AC_DEFINE([LSEEK_PIPE_BROKEN], [1],
46       [Define to 1 if lseek does not detect pipes.])
47   fi
48 ])