Debian Codegraph @

m4/dup2.m4

raw
1 # dup2.m4
2 # serial 28
3 dnl Copyright (C) 2002, 2005, 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.
7 dnl This file is offered as-is, without any warranty.
8
9 AC_DEFUN([gl_FUNC_DUP2],
10 [
11 AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
12 AC_REQUIRE([AC_CANONICAL_HOST])
13 AC_CACHE_CHECK([whether dup2 works], [gl_cv_func_dup2_works],
14 [AC_RUN_IFELSE([
15 AC_LANG_PROGRAM(
16 [[#include <errno.h>
17 #include <fcntl.h>
18 #include <limits.h>
19 #include <sys/resource.h>
20 #include <unistd.h>
21 ]GL_MDA_DEFINES[
22 #ifndef RLIM_SAVED_CUR
23 # define RLIM_SAVED_CUR RLIM_INFINITY
24 #endif
25 #ifndef RLIM_SAVED_MAX
26 # define RLIM_SAVED_MAX RLIM_INFINITY
27 #endif
28 ]],
29 [[int result = 0;
30 int bad_fd = INT_MAX;
31 struct rlimit rlim;
32 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
33 && 0 <= rlim.rlim_cur && rlim.rlim_cur <= INT_MAX
34 && rlim.rlim_cur != RLIM_INFINITY
35 && rlim.rlim_cur != RLIM_SAVED_MAX
36 && rlim.rlim_cur != RLIM_SAVED_CUR)
37 bad_fd = rlim.rlim_cur;
38 #ifdef FD_CLOEXEC
39 if (fcntl (1, F_SETFD, FD_CLOEXEC) == -1)
40 result |= 1;
41 #endif
42 if (dup2 (1, 1) != 1)
43 result |= 2;
44 #ifdef FD_CLOEXEC
45 if (fcntl (1, F_GETFD) != FD_CLOEXEC)
46 result |= 4;
47 #endif
48 close (0);
49 if (dup2 (0, 0) != -1)
50 result |= 8;
51 /* Many gnulib modules require POSIX conformance of EBADF. */
52 if (dup2 (2, bad_fd) == -1 && errno != EBADF)
53 result |= 16;
54 /* Flush out some cygwin core dumps. */
55 if (dup2 (2, -1) != -1 || errno != EBADF)
56 result |= 32;
57 dup2 (2, 255);
58 dup2 (2, 256);
59 /* On OS/2 kLIBC, dup2() does not work on a directory fd. */
60 {
61 int fd = open (".", O_RDONLY);
62 if (fd == -1)
63 result |= 64;
64 else if (dup2 (fd, fd + 1) == -1)
65 result |= 128;
66 close (fd);
67 }
68 return result;]])
69 ],
70 [gl_cv_func_dup2_works=yes], [gl_cv_func_dup2_works=no],
71 [case "$host_os" in
72 mingw* | windows*) # on this platform, dup2 always returns 0 for success
73 gl_cv_func_dup2_works="guessing no" ;;
74 cygwin*) # on cygwin 1.5.x, dup2(1,1) returns 0
75 gl_cv_func_dup2_works="guessing no" ;;
76 aix* | freebsd*)
77 # on AIX 7.1 and FreeBSD 6.1, dup2 (1,toobig) gives EMFILE,
78 # not EBADF.
79 gl_cv_func_dup2_works="guessing no" ;;
80 haiku*) # on Haiku alpha 2, dup2(1, 1) resets FD_CLOEXEC.
81 gl_cv_func_dup2_works="guessing no" ;;
82 *-android*) # implemented using dup3(), which fails if oldfd == newfd
83 gl_cv_func_dup2_works="guessing no" ;;
84 os2*) # on OS/2 kLIBC, dup2() does not work on a directory fd.
85 gl_cv_func_dup2_works="guessing no" ;;
86 *) gl_cv_func_dup2_works="guessing yes" ;;
87 esac])
88 ])
89 case "$gl_cv_func_dup2_works" in
90 *yes) ;;
91 *)
92 REPLACE_DUP2=1
93 AC_CHECK_FUNCS([setdtablesize])
94 ;;
95 esac
96 dnl Replace dup2() for supporting the gnulib-defined fchdir() function,
97 dnl to keep fchdir's bookkeeping up-to-date.
98 m4_ifdef([gl_FUNC_FCHDIR], [
99 gl_TEST_FCHDIR
100 if test $HAVE_FCHDIR = 0; then
101 REPLACE_DUP2=1
102 fi
103 ])
104 ])
105
106 # Prerequisites of lib/dup2.c.
107 AC_DEFUN([gl_PREREQ_DUP2], [])