2 Copyright (C) 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
23 #include "signature.h"
24 SIGNATURE_CHECK (getgroups, int, (int, gid_t[]));
31 #define ASSERT(expr) \
36 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
44 main (int argc, char **argv _GL_UNUSED)
50 result = getgroups (0, NULL);
51 if (result == -1 && errno == ENOSYS)
53 fputs ("skipping test: no support for groups\n", stderr);
57 ASSERT (result + 1 < SIZE_MAX / sizeof *groups);
58 groups = malloc ((result + 1) * sizeof *groups);
61 /* Check for EINVAL handling. Not all processes have supplemental
62 groups, and getgroups does not have to return the effective gid,
63 so a result of 0 is reasonable. Also, we can't test for EINVAL
64 if result is 1, because of how getgroups treats 0. */
68 ASSERT (getgroups (result - 1, groups) == -1);
69 ASSERT (errno == EINVAL);
71 ASSERT (getgroups (result, groups) == result);
72 ASSERT (getgroups (result + 1, groups) == result);
73 ASSERT (groups[result] == -1);
75 ASSERT (getgroups (-1, NULL) == -1);
76 ASSERT (errno == EINVAL);
78 /* The automated unit test, with no arguments, ends here. However,
79 for debugging purposes, you can pass a command-line argument to
80 list the returned groups. */
84 for (i = 0; i < result; i++)
85 printf ("%d\n", (int) groups[i]);