autoupdate
[gnulib.git] / m4 / ptsname_r.m4
blob286ccab8c9b70fb7a6e093a3ef8692e2a5fbcf2d
1 # ptsname_r.m4
2 # serial 10
3 dnl Copyright (C) 2010-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_PTSNAME_R],
10   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
12   dnl Persuade glibc <stdlib.h> to declare ptsname_r().
13   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
15   AC_CHECK_FUNCS_ONCE([ptsname_r])
16   if test $ac_cv_func_ptsname_r = no; then
17     HAVE_PTSNAME_R=0
18   else
19     dnl On OSF/1 5.1, the type of the third argument is 'int', not 'size_t',
20     dnl and the declaration is missing if _REENTRANT is not defined.
21     AC_CACHE_CHECK([whether ptsname_r has the same signature as in glibc],
22       [gl_cv_func_ptsname_r_signature_ok],
23       [AC_COMPILE_IFELSE(
24          [AC_LANG_PROGRAM(
25             [[#include <stddef.h>
26               #include <stdlib.h>
27               /* Test whether ptsname_r is declared at all.  */
28               int (*f) (int, char *, size_t) = ptsname_r;
29               /* Test whether it has the same declaration as in glibc.  */
30               #undef ptsname_r
31               extern
32               #ifdef __cplusplus
33               "C"
34               #endif
35               int ptsname_r (int, char *, size_t);
36             ]],
37             [[return f (0, NULL, 0);]])
38          ],
39          [gl_cv_func_ptsname_r_signature_ok=yes],
40          [gl_cv_func_ptsname_r_signature_ok=no])
41       ])
42     if test $gl_cv_func_ptsname_r_signature_ok = no; then
43       REPLACE_PTSNAME_R=1
44     else
45       AC_DEFINE([HAVE_ESSENTIALLY_WORKING_PTSNAME_R], [1],
46         [Define to 1 if ptsname_r() is essentially working.])
47       dnl On FreeBSD 13.0 and Android 4.3, when ptsname_r fails, it returns -1
48       dnl instead of the error code.
49       AC_REQUIRE([AC_CANONICAL_HOST])
50       AC_CACHE_CHECK([whether ptsname_r returns an error code],
51         [gl_cv_func_ptsname_r_retval_ok],
52         [AC_RUN_IFELSE(
53            [AC_LANG_SOURCE([[
54 #include <stdlib.h>
55 int
56 main (void)
58   char buf[80];
59   return ptsname_r (-1, buf, sizeof buf) == -1;
60 }]])],
61            [gl_cv_func_ptsname_r_retval_ok=yes],
62            [gl_cv_func_ptsname_r_retval_ok=no],
63            [case "$host_os" in
64               dnl Guess no on FreeBSD, Android.
65               freebsd* | dragonfly* | midnightbsd* | linux*-android*)
66                 gl_cv_func_ptsname_r_retval_ok="guessing no" ;;
67               dnl Guess yes otherwise.
68               *)
69                 gl_cv_func_ptsname_r_retval_ok="guessing yes" ;;
70             esac
71            ])
72         ])
73       case "$gl_cv_func_ptsname_r_retval_ok" in
74         *yes) ;;
75         *) REPLACE_PTSNAME_R=1 ;;
76       esac
77       dnl On NetBSD 10.0, when ptsname_r fails with ERANGE, it clobbers the
78       dnl result buffer. We don't use an AC_RUN_IFELSE test here, because
79       dnl while the bug exists on all platforms, only NetBSD/i386 has the
80       dnl files /dev/ptyp[01] on which the bug becomes apparent.
81       case "$host_os" in
82         netbsd*) REPLACE_PTSNAME_R=1 ;;
83       esac
84     fi
85   fi
87   AC_REQUIRE([AC_HEADER_MAJOR])
90 # Prerequisites of lib/ptsname.c.
91 AC_DEFUN([gl_PREREQ_PTSNAME_R], [
92   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
93   AC_CACHE_CHECK([whether isatty sets errno when it fails],
94     [gl_cv_func_isatty_sets_errno],
95     [AC_RUN_IFELSE(
96        [AC_LANG_PROGRAM(
97           [[#include <errno.h>
98             #if defined _WIN32 && ! defined __CYGWIN__
99             # include <io.h>
100             #else
101             # include <unistd.h>
102             #endif
103           ]GL_MDA_DEFINES],
104           [[errno = 0;
105             isatty (-1);
106             return errno == 0;
107           ]])
108        ],
109        [gl_cv_func_isatty_sets_errno=yes],
110        [gl_cv_func_isatty_sets_errno=no],
111        [case "$host_os" in
112           irix* | solaris*)
113             gl_cv_func_isatty_sets_errno="guessing no" ;;
114             # Guess yes on mingw, no on MSVC.
115           mingw* | windows*)
116             AC_EGREP_CPP([Known], [
117 #ifdef __MINGW32__
118  Known
119 #endif
120               ],
121               [gl_cv_func_isatty_sets_errno="guessing yes"],
122               [gl_cv_func_isatty_sets_errno="guessing no"])
123             ;;
124           *)
125             gl_cv_func_isatty_sets_errno="guessing yes" ;;
126         esac
127        ])
128     ])
129   case "$gl_cv_func_isatty_sets_errno" in
130     *yes) ;;
131     *)
132       AC_DEFINE([ISATTY_FAILS_WITHOUT_SETTING_ERRNO], [1],
133         [Define to 1 if isatty() may fail without setting errno.])
134       ;;
135   esac