Debian Codegraph @

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