blob: 8a37ed0b7a20c2d6e988b3d0eb838dbb36d42f03 [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
Wayne Davisonaf57b552020-06-27 21:19:52 -07006 * Copyright (C) 2003-2020 Wayne Davison
Wayne Davison0f78b812006-04-25 20:23:34 +00007 *
Martin Pool4acbfa22002-03-25 02:55:52 +00008 * This program is free software; you can redistribute it and/or modify
Wayne Davison4fd842f2007-07-07 05:33:14 +00009 * it under the terms of the GNU General Public License version 3 as
Wayne Davisonba2133d2007-02-04 14:54:58 +000010 * published by the Free Software Foundation.
Wayne Davison0f78b812006-04-25 20:23:34 +000011 *
Martin Pool4acbfa22002-03-25 02:55:52 +000012 * 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.
Wayne Davison0f78b812006-04-25 20:23:34 +000016 *
Wayne Davisone7c67062006-04-25 23:51:12 +000017 * You should have received a copy of the GNU General Public License along
Wayne Davison4fd842f2007-07-07 05:33:14 +000018 * with this program; if not, visit the http://fsf.org website.
Martin Pool4acbfa22002-03-25 02:55:52 +000019 */
20
Martin Pool4acbfa22002-03-25 02:55:52 +000021#include "rsync.h"
22
Wayne Davison11eb67e2020-06-25 19:59:19 -070023 int main(UNUSED(int argc), UNUSED(char *argv[]))
Martin Pool4acbfa22002-03-25 02:55:52 +000024{
25 int n, i;
Wayne Davison40ae4f92004-02-20 17:01:33 +000026 gid_t *list;
Wayne Davison670d8ab2004-02-04 17:05:44 +000027 gid_t gid = MY_GID();
Wayne Davison58743a82004-01-22 09:13:36 +000028 int gid_in_list = 0;
Martin Pool4acbfa22002-03-25 02:55:52 +000029
Wayne Davison4f5b0752005-02-14 00:53:43 +000030#ifdef HAVE_GETGROUPS
Wayne Davison40ae4f92004-02-20 17:01:33 +000031 if ((n = getgroups(0, NULL)) < 0) {
Martin Pool4acbfa22002-03-25 02:55:52 +000032 perror("getgroups");
33 return 1;
34 }
Wayne Davison1df395f2004-02-04 17:49:36 +000035#else
36 n = 0;
37#endif
Martin Pool4acbfa22002-03-25 02:55:52 +000038
Wayne Davison40ae4f92004-02-20 17:01:33 +000039 list = (gid_t*)malloc(sizeof (gid_t) * (n + 1));
40 if (!list) {
41 fprintf(stderr, "out of memory!\n");
42 exit(1);
43 }
44
Wayne Davison4f5b0752005-02-14 00:53:43 +000045#ifdef HAVE_GETGROUPS
Wayne Davison40ae4f92004-02-20 17:01:33 +000046 if (n > 0)
47 n = getgroups(n, list);
48#endif
49
Wayne Davison58743a82004-01-22 09:13:36 +000050 for (i = 0; i < n; i++) {
Wayne Davisonf3584872004-01-04 07:06:05 +000051 printf("%lu ", (unsigned long)list[i]);
Wayne Davison58743a82004-01-22 09:13:36 +000052 if (list[i] == gid)
53 gid_in_list = 1;
54 }
55 /* The default gid might not be in the list on some systems. */
56 if (!gid_in_list)
57 printf("%lu", (unsigned long)gid);
Martin Pool4acbfa22002-03-25 02:55:52 +000058 printf("\n");
Wayne Davison0f78b812006-04-25 20:23:34 +000059
Martin Pool4acbfa22002-03-25 02:55:52 +000060 return 0;
61}