error: Avoid "function declaration isn't a prototype" warning.
[gnulib.git] / tests / test-getgroups.c
blobaa555ef775e8cd2e54c419607dd898cdb4d491eb
1 /* Tests of getgroups.
2 Copyright (C) 2009-2017 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. */
19 #include <config.h>
21 #include <unistd.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (getgroups, int, (int, gid_t[]));
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
31 #include "macros.h"
33 int
34 main (int argc, char **argv _GL_UNUSED)
36 int result;
37 gid_t *groups;
39 errno = 0;
40 result = getgroups (0, NULL);
41 if (result == -1 && errno == ENOSYS)
43 fputs ("skipping test: no support for groups\n", stderr);
44 return 77;
46 ASSERT (0 <= result);
47 ASSERT (result + 1 < SIZE_MAX / sizeof *groups);
48 groups = malloc ((result + 1) * sizeof *groups);
49 ASSERT (groups);
50 groups[result] = -1;
51 /* Check for EINVAL handling. Not all processes have supplemental
52 groups, and getgroups does not have to return the effective gid,
53 so a result of 0 is reasonable. Also, we can't test for EINVAL
54 if result is 1, because of how getgroups treats 0. */
55 if (1 < result)
57 errno = 0;
58 ASSERT (getgroups (result - 1, groups) == -1);
59 ASSERT (errno == EINVAL);
61 ASSERT (getgroups (result, groups) == result);
62 ASSERT (getgroups (result + 1, groups) == result);
63 ASSERT (groups[result] == -1);
64 errno = 0;
65 ASSERT (getgroups (-1, NULL) == -1);
66 ASSERT (errno == EINVAL);
68 /* The automated unit test, with no arguments, ends here. However,
69 for debugging purposes, you can pass a command-line argument to
70 list the returned groups. */
71 if (1 < argc)
73 int i;
74 for (i = 0; i < result; i++)
75 printf ("%d\n", (int) groups[i]);
77 free (groups);
78 return 0;