Merge pull request #2043 from RincewindsHat/cleanup/leftovers
[monitoring-plugins.git] / gl / m4 / lseek.m4
blob0bc3d65ec06d6848fd3b5c60151280abc78ed0f5
1 # lseek.m4
2 # serial 15
3 dnl Copyright (C) 2007, 2009-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 AC_DEFUN([gl_FUNC_LSEEK],
10   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
12   AC_REQUIRE([AC_CANONICAL_HOST])
13   AC_REQUIRE([AC_PROG_CC])
14   AC_CHECK_HEADERS_ONCE([unistd.h])
15   AC_CACHE_CHECK([whether lseek detects pipes], [gl_cv_func_lseek_pipe],
16     [case "$host_os" in
17        mingw* | windows*)
18          dnl Native Windows.
19          dnl The result of lseek (fd, (off_t)0, SEEK_CUR) or
20          dnl SetFilePointer(handle, 0, NULL, FILE_CURRENT)
21          dnl for a pipe depends on the environment:
22          dnl In a Cygwin 1.5 environment it succeeds (wrong);
23          dnl in a Cygwin 1.7 environment it fails with a wrong errno value;
24          dnl in a Cygwin 2.9.0 environment it fails correctly;
25          dnl in a Cygwin 3.4.6 environment it succeeds again (wrong).
26          gl_cv_func_lseek_pipe=no
27          ;;
28        *)
29          if test $cross_compiling = no; then
30            AC_LINK_IFELSE([AC_LANG_PROGRAM([[
31 #include <sys/types.h> /* for off_t */
32 #include <stdio.h> /* for SEEK_CUR */
33 #if HAVE_UNISTD_H
34 # include <unistd.h>
35 #else /* on Windows with MSVC */
36 # include <io.h>
37 #endif
38 ]GL_MDA_DEFINES],
40   /* Exit with success only if stdin is seekable.  */
41   return lseek (0, (off_t)0, SEEK_CUR) < 0;
42 ]])],
43              [if test -s conftest$ac_exeext \
44                  && ./conftest$ac_exeext < conftest.$ac_ext \
45                  && test 1 = "`echo hi \
46                    | { ./conftest$ac_exeext; echo $?; cat >/dev/null; }`"; then
47                 gl_cv_func_lseek_pipe=yes
48               else
49                 gl_cv_func_lseek_pipe=no
50               fi
51              ],
52              [gl_cv_func_lseek_pipe=no])
53          else
54            AC_COMPILE_IFELSE(
55              [AC_LANG_SOURCE([[
56 #if defined __BEOS__
57 /* BeOS mistakenly return 0 when trying to seek on pipes.  */
58   Choke me.
59 #endif]])],
60              [gl_cv_func_lseek_pipe=yes], [gl_cv_func_lseek_pipe=no])
61          fi
62          ;;
63      esac
64     ])
65   if test "$gl_cv_func_lseek_pipe" = no; then
66     REPLACE_LSEEK=1
67     AC_DEFINE([LSEEK_PIPE_BROKEN], [1],
68       [Define to 1 if lseek does not detect pipes.])
69   fi
71   AC_REQUIRE([gl_SYS_TYPES_H])
72   if test $WINDOWS_64_BIT_OFF_T = 1; then
73     REPLACE_LSEEK=1
74   fi
76   AS_IF([test $REPLACE_LSEEK = 0],
77     [AC_CACHE_CHECK([whether SEEK_DATA works but is incompatible with GNU],
78        [gl_cv_func_lseek_works_but_incompatible],
79        [AC_PREPROC_IFELSE(
80           [AC_LANG_SOURCE(
81              dnl Use macOS "9999" to stand for a future fixed macOS version.
82              dnl See ../lib/unistd.in.h and <https://bugs.gnu.org/61386>.
83              [[#include <unistd.h>
84                #if defined __APPLE__ && defined __MACH__ && defined SEEK_DATA
85                # ifdef __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
86                #  include <AvailabilityMacros.h>
87                # endif
88                # if 99990000 <= MAC_OS_X_VERSION_MIN_REQUIRED
89                #  define LSEEK_WORKS_BUT_IS_INCOMPATIBLE_WITH_GNU
90                # endif
91                #endif
92                #ifndef LSEEK_WORKS_BUT_IS_INCOMPATIBLE_WITH_GNU
93                 #error "No need to work around the bug"
94                #endif
95              ]])],
96           [gl_cv_func_lseek_works_but_incompatible=yes],
97           [gl_cv_func_lseek_works_but_incompatible=no])])
98      if test "$gl_cv_func_lseek_works_but_incompatible" = yes; then
99        REPLACE_LSEEK=1
100      fi])