f582b8d81373f6d5e3f304bc201fd39a95aff455
[gnulib.git] / m4 / getgroups.m4
1 # serial 17
2
3 dnl From Jim Meyering.
4 dnl A wrapper around AC_FUNC_GETGROUPS.
5
6 # Copyright (C) 1996-1997, 1999-2004, 2008-2012 Free Software Foundation, Inc.
7 #
8 # This file is free software; the Free Software Foundation
9 # gives unlimited permission to copy and/or distribute it,
10 # with or without modifications, as long as this notice is preserved.
11
12 AC_DEFUN([gl_FUNC_GETGROUPS],
13 [
14   AC_REQUIRE([AC_FUNC_GETGROUPS])
15   AC_REQUIRE([AC_TYPE_GETGROUPS])
16   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
17   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
18   if test "$ac_cv_func_getgroups" != yes; then
19     HAVE_GETGROUPS=0
20   elif test "$ac_cv_func_getgroups_works.$ac_cv_type_getgroups" != yes.gid_t
21   then
22     REPLACE_GETGROUPS=1
23     AC_DEFINE([GETGROUPS_ZERO_BUG], [1], [Define this to 1 if
24       getgroups(0,NULL) does not return the number of groups.])
25   else
26     dnl Detect FreeBSD bug; POSIX requires getgroups(-1,ptr) to fail.
27     AC_CACHE_CHECK([whether getgroups handles negative values],
28       [gl_cv_func_getgroups_works],
29       [AC_RUN_IFELSE([AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
30         [[int size = getgroups (0, 0);
31           gid_t *list = malloc (size * sizeof *list);
32           return getgroups (-1, list) != -1;]])],
33         [gl_cv_func_getgroups_works=yes],
34         [gl_cv_func_getgroups_works=no],
35         [case "$host_os" in
36                    # Guess yes on glibc systems.
37            *-gnu*) gl_cv_func_getgroups_works="guessing yes" ;;
38                    # If we don't know, assume the worst.
39            *)      gl_cv_func_getgroups_works="guessing no" ;;
40          esac
41         ])])
42     case "$gl_cv_func_getgroups_works" in
43       *yes) ;;
44       *) REPLACE_GETGROUPS=1 ;;
45     esac
46   fi
47   test -n "$GETGROUPS_LIB" && LIBS="$GETGROUPS_LIB $LIBS"
48 ])