immutable: Add tests.
[gnulib.git] / m4 / ttyname_r.m4
blob80bb49b7e8827d2ffcb202fa894072e4629afcd9
1 # ttyname_r.m4 serial 11
2 dnl Copyright (C) 2010-2021 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.
7 AC_DEFUN([gl_FUNC_TTYNAME_R],
9   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
11   dnl Persuade Solaris <unistd.h> to provide the POSIX compliant declaration of
12   dnl ttyname_r().
13   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15   AC_CHECK_DECLS_ONCE([ttyname_r])
16   if test $ac_cv_have_decl_ttyname_r = no; then
17     HAVE_DECL_TTYNAME_R=0
18   fi
20   AC_CHECK_FUNCS([ttyname_r])
21   if test $ac_cv_func_ttyname_r = no; then
22     HAVE_TTYNAME_R=0
23   else
24     HAVE_TTYNAME_R=1
25     dnl On Mac OS X 10.4 (and Solaris 10 without gl_USE_SYSTEM_EXTENSIONS)
26     dnl the return type is 'char *', not 'int'.
27     AC_CACHE_CHECK([whether ttyname_r is compatible with its POSIX signature],
28       [gl_cv_func_ttyname_r_posix],
29       [AC_COMPILE_IFELSE(
30          [AC_LANG_PROGRAM(
31             [[#include <stddef.h>
32               #include <unistd.h>]],
33             [[*ttyname_r (0, NULL, 0);]])
34          ],
35          [gl_cv_func_ttyname_r_posix=no],
36          [gl_cv_func_ttyname_r_posix=yes])
37       ])
38     if test $gl_cv_func_ttyname_r_posix = no; then
39       REPLACE_TTYNAME_R=1
40     else
41       AC_DEFINE([HAVE_POSIXDECL_TTYNAME_R], [1],
42         [Define if the ttyname_r function has a POSIX compliant declaration.])
43       dnl On Solaris 10, both ttyname_r functions (the one with the non-POSIX
44       dnl declaration and the one with the POSIX declaration) refuse to do
45       dnl anything when the output buffer is less than 128 bytes large.
46       dnl On OSF/1 5.1, ttyname_r ignores the buffer size and assumes the
47       dnl buffer is large enough.
48       dnl On Android 4.3, ttyname_r is a stub that prints
49       dnl "int ttyname_r(int, char*, size_t)(3) is not implemented on Android"
50       dnl on stderr and returns -ERANGE.
51       AC_REQUIRE([AC_CANONICAL_HOST])
52       case "$host_os" in
53         linux*-android*)
54           AC_CACHE_CHECK([whether ttyname_r works at least minimally],
55             [gl_cv_func_ttyname_r_not_stub],
56             [AC_RUN_IFELSE(
57                [AC_LANG_SOURCE([[
58 #include <errno.h>
59 #include <unistd.h>
60 ]GL_MDA_DEFINES[
61 int
62 main (void)
64   char buf[80];
65   close(2);
66   return ttyname_r (-1, buf, sizeof (buf)) == -ERANGE;
67 }]])],
68                [gl_cv_func_ttyname_r_not_stub=yes],
69                [gl_cv_func_ttyname_r_not_stub=no],
70                [# Guess no on Android.
71                 gl_cv_func_ttyname_r_not_stub="guessing no"
72                ])
73             ])
74           case "$gl_cv_func_ttyname_r_not_stub" in
75             *yes) ;;
76             *) REPLACE_TTYNAME_R=1 ;;
77           esac
78           ;;
79       esac
80       if test $REPLACE_TTYNAME_R = 0; then
81         AC_CACHE_CHECK([whether ttyname_r works with small buffers],
82           [gl_cv_func_ttyname_r_works],
83           [
84             dnl Initial guess, used when cross-compiling or when /dev/tty cannot
85             dnl be opened.
86 changequote(,)dnl
87             case "$host_os" in
88                         # Guess no on Solaris.
89               solaris*) gl_cv_func_ttyname_r_works="guessing no" ;;
90                         # Guess no on OSF/1.
91               osf*)     gl_cv_func_ttyname_r_works="guessing no" ;;
92                         # Guess yes otherwise.
93               *)        gl_cv_func_ttyname_r_works="guessing yes" ;;
94             esac
95 changequote([,])dnl
96             AC_RUN_IFELSE(
97               [AC_LANG_SOURCE([[
98 #include <fcntl.h>
99 #include <unistd.h>
100 ]GL_MDA_DEFINES[
102 main (void)
104   int result = 0;
105   int fd;
106   char buf[31]; /* use any size < 128 here */
108   fd = open ("/dev/tty", O_RDONLY);
109   if (fd < 0)
110     result |= 16;
111   else if (ttyname_r (fd, buf, sizeof (buf)) != 0)
112     result |= 17;
113   else if (ttyname_r (fd, buf, 1) == 0)
114     result |= 18;
115   return result;
116 }]])],
117               [gl_cv_func_ttyname_r_works=yes],
118               [case $? in
119                  17 | 18) gl_cv_func_ttyname_r_works=no ;;
120                esac],
121               [:])
122           ])
123         case "$gl_cv_func_ttyname_r_works" in
124           *yes) ;;
125           *) REPLACE_TTYNAME_R=1 ;;
126         esac
127       fi
128     fi
129   fi
132 # Prerequisites of lib/ttyname_r.c.
133 AC_DEFUN([gl_PREREQ_TTYNAME_R], [
134   AC_CHECK_FUNCS([ttyname])