blob: a3a6f0948a457622e6d5872b3d862fdc7c8d6804 [file] [log] [blame]
Martin Pool4acbfa22002-03-25 02:55:52 +00001/*
Wayne Davison0f78b812006-04-25 20:23:34 +00002 * 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
6 * Copyright (C) 2003, 2004 Wayne Davison
7 *
Martin Pool4acbfa22002-03-25 02:55:52 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
Wayne Davison0f78b812006-04-25 20:23:34 +000012 *
Martin Pool4acbfa22002-03-25 02:55:52 +000013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Wayne Davison0f78b812006-04-25 20:23:34 +000017 *
Wayne Davisone7c67062006-04-25 23:51:12 +000018 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
Martin Pool4acbfa22002-03-25 02:55:52 +000021 */
22
Martin Pool4acbfa22002-03-25 02:55:52 +000023#include "rsync.h"
24
Wayne Davisonf69204a2003-09-10 08:27:34 +000025int
26main(UNUSED(int argc), UNUSED(char *argv[]))
Martin Pool4acbfa22002-03-25 02:55:52 +000027{
28 int n, i;
Wayne Davison40ae4f92004-02-20 17:01:33 +000029 gid_t *list;
Wayne Davison670d8ab2004-02-04 17:05:44 +000030 gid_t gid = MY_GID();
Wayne Davison58743a82004-01-22 09:13:36 +000031 int gid_in_list = 0;
Martin Pool4acbfa22002-03-25 02:55:52 +000032
Wayne Davison4f5b0752005-02-14 00:53:43 +000033#ifdef HAVE_GETGROUPS
Wayne Davison40ae4f92004-02-20 17:01:33 +000034 if ((n = getgroups(0, NULL)) < 0) {
Martin Pool4acbfa22002-03-25 02:55:52 +000035 perror("getgroups");
36 return 1;
37 }
Wayne Davison1df395f2004-02-04 17:49:36 +000038#else
39 n = 0;
40#endif
Martin Pool4acbfa22002-03-25 02:55:52 +000041
Wayne Davison40ae4f92004-02-20 17:01:33 +000042 list = (gid_t*)malloc(sizeof (gid_t) * (n + 1));
43 if (!list) {
44 fprintf(stderr, "out of memory!\n");
45 exit(1);
46 }
47
Wayne Davison4f5b0752005-02-14 00:53:43 +000048#ifdef HAVE_GETGROUPS
Wayne Davison40ae4f92004-02-20 17:01:33 +000049 if (n > 0)
50 n = getgroups(n, list);
51#endif
52
Wayne Davison58743a82004-01-22 09:13:36 +000053 for (i = 0; i < n; i++) {
Wayne Davisonf3584872004-01-04 07:06:05 +000054 printf("%lu ", (unsigned long)list[i]);
Wayne Davison58743a82004-01-22 09:13:36 +000055 if (list[i] == gid)
56 gid_in_list = 1;
57 }
58 /* The default gid might not be in the list on some systems. */
59 if (!gid_in_list)
60 printf("%lu", (unsigned long)gid);
Martin Pool4acbfa22002-03-25 02:55:52 +000061 printf("\n");
Wayne Davison0f78b812006-04-25 20:23:34 +000062
Martin Pool4acbfa22002-03-25 02:55:52 +000063 return 0;
64}