re-mark 1.29b-2 as not yet uploaded (merge madness!)
[debian/tar] / m4 / ttyname_r.m4
1 # ttyname_r.m4 serial 9
2 dnl Copyright (C) 2010-2014 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.
6
7 AC_DEFUN([gl_FUNC_TTYNAME_R],
8 [
9   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
10
11   dnl Persuade Solaris <unistd.h> to provide the POSIX compliant declaration of
12   dnl ttyname_r().
13   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
14
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
19
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       AC_REQUIRE([AC_CANONICAL_HOST])
49       AC_CACHE_CHECK([whether ttyname_r works with small buffers],
50         [gl_cv_func_ttyname_r_works],
51         [
52           dnl Initial guess, used when cross-compiling or when /dev/tty cannot
53           dnl be opened.
54 changequote(,)dnl
55           case "$host_os" in
56                       # Guess no on Solaris.
57             solaris*) gl_cv_func_ttyname_r_works="guessing no" ;;
58                       # Guess no on OSF/1.
59             osf*)     gl_cv_func_ttyname_r_works="guessing no" ;;
60                       # Guess yes otherwise.
61             *)        gl_cv_func_ttyname_r_works="guessing yes" ;;
62           esac
63 changequote([,])dnl
64           AC_RUN_IFELSE(
65             [AC_LANG_SOURCE([[
66 #include <fcntl.h>
67 #include <unistd.h>
68 int
69 main (void)
70 {
71   int result = 0;
72   int fd;
73   char buf[31]; /* use any size < 128 here */
74
75   fd = open ("/dev/tty", O_RDONLY);
76   if (fd < 0)
77     result |= 16;
78   else if (ttyname_r (fd, buf, sizeof (buf)) != 0)
79     result |= 17;
80   else if (ttyname_r (fd, buf, 1) == 0)
81     result |= 18;
82   return result;
83 }]])],
84             [gl_cv_func_ttyname_r_works=yes],
85             [case $? in
86                17 | 18) gl_cv_func_ttyname_r_works=no ;;
87              esac],
88             [:])
89         ])
90       case "$gl_cv_func_ttyname_r_works" in
91         *yes) ;;
92         *) REPLACE_TTYNAME_R=1 ;;
93       esac
94     fi
95   fi
96 ])
97
98 # Prerequisites of lib/ttyname_r.c.
99 AC_DEFUN([gl_PREREQ_TTYNAME_R], [
100   AC_CHECK_FUNCS([ttyname])
101 ])