Avoid a potential hang when --remove-*-files is active.
[rsync/rsync.git] / getgroups.c
CommitLineData
4acbfa2a 1/*
0f78b815
WD
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.
4 *
5 * Copyright (C) 2002 Martin Pool
d3d07a5e 6 * Copyright (C) 2003-2008 Wayne Davison
0f78b815 7 *
4acbfa2a 8 * This program is free software; you can redistribute it and/or modify
4fd842f9 9 * it under the terms of the GNU General Public License version 3 as
ba2133d6 10 * published by the Free Software Foundation.
0f78b815 11 *
4acbfa2a
MP
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.
0f78b815 16 *
e7c67065 17 * You should have received a copy of the GNU General Public License along
4fd842f9 18 * with this program; if not, visit the http://fsf.org website.
4acbfa2a
MP
19 */
20
4acbfa2a
MP
21#include "rsync.h"
22
f69204ad
WD
23int
24main(UNUSED(int argc), UNUSED(char *argv[]))
4acbfa2a
MP
25{
26 int n, i;
40ae4f93 27 gid_t *list;
670d8abf 28 gid_t gid = MY_GID();
58743a87 29 int gid_in_list = 0;
4acbfa2a 30
4f5b0756 31#ifdef HAVE_GETGROUPS
40ae4f93 32 if ((n = getgroups(0, NULL)) < 0) {
4acbfa2a
MP
33 perror("getgroups");
34 return 1;
35 }
1df395f7
WD
36#else
37 n = 0;
38#endif
4acbfa2a 39
40ae4f93
WD
40 list = (gid_t*)malloc(sizeof (gid_t) * (n + 1));
41 if (!list) {
42 fprintf(stderr, "out of memory!\n");
43 exit(1);
44 }
45
4f5b0756 46#ifdef HAVE_GETGROUPS
40ae4f93
WD
47 if (n > 0)
48 n = getgroups(n, list);
49#endif
50
58743a87 51 for (i = 0; i < n; i++) {
f358487f 52 printf("%lu ", (unsigned long)list[i]);
58743a87
WD
53 if (list[i] == gid)
54 gid_in_list = 1;
55 }
56 /* The default gid might not be in the list on some systems. */
57 if (!gid_in_list)
58 printf("%lu", (unsigned long)gid);
4acbfa2a 59 printf("\n");
0f78b815 60
4acbfa2a
MP
61 return 0;
62}