Merge branch 'stable'
[gnulib.git] / m4 / ttyname_r.m4
1 # ttyname_r.m4 serial 4
2 dnl Copyright (C) 2010 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_FUNCS([ttyname_r])
16   if test $ac_cv_func_ttyname_r = no; then
17     HAVE_TTYNAME_R=0
18   else
19     dnl On MacOS X 10.4 (and Solaris 10 without gl_USE_SYSTEM_EXTENSIONS)
20     dnl the return type is 'char *', not 'int'.
21     AC_CACHE_CHECK([whether ttyname_r is compatible with its POSIX signature],
22       [gl_cv_func_ttyname_r_posix],
23       [AC_COMPILE_IFELSE(
24          [AC_LANG_PROGRAM(
25             [[#include <stddef.h>
26               #include <unistd.h>]],
27             [[*ttyname_r (0, NULL, 0);]])
28          ],
29          [gl_cv_func_ttyname_r_posix=no],
30          [gl_cv_func_ttyname_r_posix=yes])
31       ])
32     if test $gl_cv_func_ttyname_r_posix = no; then
33       REPLACE_TTYNAME_R=1
34     else
35       AC_DEFINE([HAVE_POSIXDECL_TTYNAME_R], [1],
36         [Define if the ttyname_r function has a POSIX compliant declaration.])
37       dnl On Solaris 10, both ttyname_r functions (the one with the non-POSIX
38       dnl declaration and the one with the POSIX declaration) refuse to do
39       dnl anything when the output buffer is less than 128 bytes large.
40       AC_REQUIRE([AC_CANONICAL_HOST])
41       AC_CACHE_CHECK([whether ttyname_r works with small buffers],
42         [gl_cv_func_ttyname_r_works],
43         [
44           dnl Initial guess, used when cross-compiling or when /dev/tty cannot
45           dnl be opened.
46 changequote(,)dnl
47           case "$host_os" in
48                       # Guess no on Solaris.
49             solaris*) gl_cv_func_ttyname_r_works="guessing no" ;;
50                       # Guess yes otherwise.
51             *)        gl_cv_func_ttyname_r_works="guessing yes" ;;
52           esac
53 changequote([,])dnl
54           AC_RUN_IFELSE(
55             [AC_LANG_SOURCE([[
56 #include <fcntl.h>
57 #include <unistd.h>
58 int
59 main (void)
60 {
61   int fd;
62   char buf[31]; /* use any size < 128 here */
63
64   fd = open ("/dev/tty", O_RDONLY);
65   if (fd < 0)
66     return 1;
67   if (ttyname_r (fd, buf, sizeof (buf)) != 0)
68     return 1;
69   return 0;
70 }]])],
71             [gl_cv_func_ttyname_r_works=yes],
72             [:],
73             [:])
74         ])
75       case "$gl_cv_func_ttyname_r_works" in
76         *yes) ;;
77         *) REPLACE_TTYNAME_R=1 ;;
78       esac
79     fi
80   fi
81   if test $HAVE_TTYNAME_R = 0 || test $REPLACE_TTYNAME_R = 1; then
82     AC_LIBOBJ([ttyname_r])
83     gl_PREREQ_TTYNAME_R
84   fi
85 ])
86
87 # Prerequisites of lib/ttyname_r.c.
88 AC_DEFUN([gl_PREREQ_TTYNAME_R], [
89   AC_CHECK_FUNCS([ttyname])
90 ])