Honour HAVE_POPT*
[rsync/qnx.git] / getgroups.c
blob0fa1aa8f8288be6bbe02ddbdb3df3d61cb2b5cb2
1 /*
2 * Print out the gids of all groups for the current user. This is like
3 * `id -G` on Linux, but it's too hard to find a portable equivalent.
5 * Copyright (C) 2002 Martin Pool
6 * Copyright (C) 2003-2008 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 3 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, visit the http://fsf.org website.
21 #include "rsync.h"
23 int
24 main(UNUSED(int argc), UNUSED(char *argv[]))
26 int n, i;
27 gid_t *list;
28 gid_t gid = MY_GID();
29 int gid_in_list = 0;
31 #ifdef HAVE_GETGROUPS
32 if ((n = getgroups(0, NULL)) < 0) {
33 perror("getgroups");
34 return 1;
36 #else
37 n = 0;
38 #endif
40 list = (gid_t*)malloc(sizeof (gid_t) * (n + 1));
41 if (!list) {
42 fprintf(stderr, "out of memory!\n");
43 exit(1);
46 #ifdef HAVE_GETGROUPS
47 if (n > 0)
48 n = getgroups(n, list);
49 #endif
51 for (i = 0; i < n; i++) {
52 printf("%lu ", (unsigned long)list[i]);
53 if (list[i] == gid)
54 gid_in_list = 1;
56 /* The default gid might not be in the list on some systems. */
57 if (!gid_in_list)
58 printf("%lu", (unsigned long)gid);
59 printf("\n");
61 return 0;