Detect UNIX domain sockets supported by recent Windows versions.
[gnulib.git] / m4 / getgroups.m4
blob5457275e9e154c478877c916cc8a7fc8965d7740
1 # getgroups.m4
2 # serial 25
3 dnl Copyright (C) 1996-1997, 1999-2004, 2008-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 dnl From Jim Meyering.
9 dnl A wrapper around AC_FUNC_GETGROUPS.
11 # This is taken from the following Autoconf patch:
12 # https://git.savannah.gnu.org/gitweb/?p=autoconf.git;a=commitdiff;h=7fbb553727ed7e0e689a17594b58559ecf3ea6e9
13 AC_DEFUN([AC_FUNC_GETGROUPS],
15   AC_REQUIRE([AC_TYPE_GETGROUPS])dnl
16   AC_REQUIRE([AC_TYPE_SIZE_T])dnl
17   AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
18   AC_CHECK_FUNC([getgroups])
20   # If we don't yet have getgroups, see if it's in -lbsd.
21   # This is reported to be necessary on an ITOS 3000WS running SEIUX 3.1.
22   gl_saved_LIBS=$LIBS
23   if test $ac_cv_func_getgroups = no; then
24     AC_CHECK_LIB(bsd, getgroups, [GETGROUPS_LIB=-lbsd])
25   fi
27   # Run the program to test the functionality of the system-supplied
28   # getgroups function only if there is such a function.
29   if test $ac_cv_func_getgroups = yes; then
30     AC_CACHE_CHECK([for working getgroups], [ac_cv_func_getgroups_works],
31       [AC_RUN_IFELSE(
32          [AC_LANG_PROGRAM(
33             [AC_INCLUDES_DEFAULT],
34             [[/* On NeXTstep 3.2, getgroups (0, 0) always fails.  */
35               return getgroups (0, 0) == -1;]])
36          ],
37          [ac_cv_func_getgroups_works=yes],
38          [ac_cv_func_getgroups_works=no],
39          [case "$host_os" in # ((
40                            # Guess yes on glibc systems.
41             *-gnu* | gnu*) ac_cv_func_getgroups_works="guessing yes" ;;
42                            # Guess yes on musl systems.
43             *-musl*)       ac_cv_func_getgroups_works="guessing yes" ;;
44                            # If we don't know, obey --enable-cross-guesses.
45             *)             ac_cv_func_getgroups_works="$gl_cross_guess_normal" ;;
46           esac
47          ])
48       ])
49   else
50     ac_cv_func_getgroups_works=no
51   fi
52   case "$ac_cv_func_getgroups_works" in
53     *yes)
54       AC_DEFINE([HAVE_GETGROUPS], [1],
55         [Define to 1 if your system has a working `getgroups' function.])
56       ;;
57   esac
58   LIBS=$gl_saved_LIBS
59 ])# AC_FUNC_GETGROUPS
61 AC_DEFUN([gl_FUNC_GETGROUPS],
63   AC_REQUIRE([AC_TYPE_GETGROUPS])
64   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
65   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
67   AC_FUNC_GETGROUPS
68   if test $ac_cv_func_getgroups != yes; then
69     HAVE_GETGROUPS=0
70   else
71     if test "$ac_cv_type_getgroups" != gid_t \
72        || { case "$ac_cv_func_getgroups_works" in
73               *yes) false;;
74               *) true;;
75             esac
76           }; then
77       REPLACE_GETGROUPS=1
78       AC_DEFINE([GETGROUPS_ZERO_BUG], [1], [Define this to 1 if
79         getgroups(0,NULL) does not return the number of groups.])
80     else
81       dnl Detect Mac OS X and FreeBSD bug; POSIX requires getgroups(-1,ptr)
82       dnl to fail.
83       AC_CACHE_CHECK([whether getgroups handles negative values],
84         [gl_cv_func_getgroups_works],
85         [AC_RUN_IFELSE([AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
86           [[int size = getgroups (0, 0);
87             gid_t *list = malloc (size * sizeof *list);
88             int result = getgroups (-1, list) != -1;
89             free (list);
90             return result;]])],
91           [gl_cv_func_getgroups_works=yes],
92           [gl_cv_func_getgroups_works=no],
93           [case "$host_os" in
94                             # Guess yes on glibc systems.
95              *-gnu* | gnu*) gl_cv_func_getgroups_works="guessing yes" ;;
96                             # Guess yes on musl systems.
97              *-musl*)       gl_cv_func_getgroups_works="guessing yes" ;;
98                             # If we don't know, obey --enable-cross-guesses.
99              *)             gl_cv_func_getgroups_works="$gl_cross_guess_normal" ;;
100            esac
101           ])])
102       case "$gl_cv_func_getgroups_works" in
103         *yes) ;;
104         *) REPLACE_GETGROUPS=1 ;;
105       esac
106     fi
107   fi
108   test -n "$GETGROUPS_LIB" && LIBS="$GETGROUPS_LIB $LIBS"