2 Copyright (C) 2009-2025 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 <https://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
[]));
33 /* Tell GCC not to warn about the specific edge cases tested here.
34 GCC >= 10 with glibc >= 2.32 would otherwise trigger warnings, even without
35 any -W options, because getgroups() is declared with
36 __attribute__ ((__access__ (__write_only__, 2, 1)))
38 #if _GL_GNUC_PREREQ (7, 0)
39 # pragma GCC diagnostic ignored "-Wstringop-overflow"
43 main (int argc
, _GL_UNUSED
char **argv
)
49 result
= getgroups (0, NULL
);
50 if (result
== -1 && errno
== ENOSYS
)
52 fputs ("skipping test: no support for groups\n", stderr
);
56 ASSERT (result
+ 1 < SIZE_MAX
/ sizeof *groups
);
57 groups
= malloc ((result
+ 1) * sizeof *groups
);
60 /* Check for EINVAL handling. Not all processes have supplemental
61 groups, and getgroups does not have to return the effective gid,
62 so a result of 0 is reasonable. Also, we can't test for EINVAL
63 if result is 1, because of how getgroups treats 0. */
67 ASSERT (getgroups (result
- 1, groups
) == -1);
68 ASSERT (errno
== EINVAL
);
70 ASSERT (getgroups (result
, groups
) == result
);
71 ASSERT (getgroups (result
+ 1, groups
) == result
);
72 ASSERT (groups
[result
] == -1);
74 ASSERT (getgroups (-1, NULL
) == -1);
75 ASSERT (errno
== EINVAL
);
77 /* The automated unit test, with no arguments, ends here. However,
78 for debugging purposes, you can pass a command-line argument to
79 list the returned groups. */
83 for (i
= 0; i
< result
; i
++)
84 printf ("%d\n", (int) groups
[i
]);
87 return test_exit_status
;