immutable: Add tests.
[gnulib.git] / m4 / getlogin_r.m4
blob3e8a2f0b21827cb31d94331fde41756c48f225da
1 #serial 13
3 # Copyright (C) 2005-2007, 2009-2021 Free Software Foundation, Inc.
5 # This file is free software; the Free Software Foundation
6 # gives unlimited permission to copy and/or distribute it,
7 # with or without modifications, as long as this notice is preserved.
9 dnl From Derek Price
10 dnl
11 dnl Provide getlogin_r when the system lacks it.
12 dnl
14 AC_DEFUN([gl_FUNC_GETLOGIN_R],
16   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
18   dnl Persuade glibc <unistd.h> to declare getlogin_r().
19   dnl Persuade Solaris <unistd.h> to provide the POSIX compliant declaration of
20   dnl getlogin_r().
21   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
23   AC_CHECK_DECLS_ONCE([getlogin_r])
24   if test $ac_cv_have_decl_getlogin_r = no; then
25     HAVE_DECL_GETLOGIN_R=0
26   fi
28   AC_CHECK_FUNCS_ONCE([getlogin_r])
29   if test $ac_cv_func_getlogin_r = no; then
30     HAVE_GETLOGIN_R=0
31   else
32     HAVE_GETLOGIN_R=1
33     dnl On Mac OS X 10.13 and OSF/1 5.1, getlogin_r returns a truncated result
34     dnl if the buffer is not large enough.
35     AC_REQUIRE([AC_CANONICAL_HOST])
36     AC_CACHE_CHECK([whether getlogin_r works with small buffers],
37       [gl_cv_func_getlogin_r_works],
38       [
39         dnl Initial guess, used when cross-compiling.
40 changequote(,)dnl
41         case "$host_os" in
42                           # Guess no on Mac OS X, OSF/1.
43           darwin* | osf*) gl_cv_func_getlogin_r_works="guessing no" ;;
44                           # Guess yes otherwise.
45           *)              gl_cv_func_getlogin_r_works="guessing yes" ;;
46         esac
47 changequote([,])dnl
48         AC_RUN_IFELSE(
49           [AC_LANG_SOURCE([[
50 #include <stddef.h>
51 #include <string.h>
52 #include <unistd.h>
53 #if !HAVE_DECL_GETLOGIN_R
54 extern
55 # ifdef __cplusplus
56 "C"
57 # endif
58 int getlogin_r (char *, size_t);
59 #endif
60 int
61 main (void)
63   int result = 0;
64   char buf[100];
66   if (getlogin_r (buf, 0) == 0)
67     result |= 1;
68   if (getlogin_r (buf, 1) == 0)
69     result |= 2;
70   if (getlogin_r (buf, 100) == 0)
71     {
72       size_t n = strlen (buf);
73       if (getlogin_r (buf, n) == 0)
74         result |= 4;
75     }
76   return result;
77 }]])],
78           [gl_cv_func_getlogin_r_works=yes],
79           [gl_cv_func_getlogin_r_works=no],
80           [:])
81       ])
82     case "$gl_cv_func_getlogin_r_works" in
83       *yes) ;;
84       *) REPLACE_GETLOGIN_R=1 ;;
85     esac
86   fi
89 AC_DEFUN([gl_PREREQ_GETLOGIN_R],
91   AC_CHECK_DECLS_ONCE([getlogin])