| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 1 | /* |
| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 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 |
| Wayne Davison | af57b55 | 2020-06-27 21:19:52 -0700 | [diff] [blame] | 6 | * Copyright (C) 2003-2020 Wayne Davison |
| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 7 | * |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| Wayne Davison | 4fd842f | 2007-07-07 05:33:14 +0000 | [diff] [blame] | 9 | * it under the terms of the GNU General Public License version 3 as |
| Wayne Davison | ba2133d | 2007-02-04 14:54:58 +0000 | [diff] [blame] | 10 | * published by the Free Software Foundation. |
| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 11 | * |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 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. |
| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 16 | * |
| Wayne Davison | e7c6706 | 2006-04-25 23:51:12 +0000 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License along |
| Wayne Davison | 4fd842f | 2007-07-07 05:33:14 +0000 | [diff] [blame] | 18 | * with this program; if not, visit the http://fsf.org website. |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 21 | #include "rsync.h" |
| 22 | |
| Wayne Davison | 11eb67e | 2020-06-25 19:59:19 -0700 | [diff] [blame] | 23 | int main(UNUSED(int argc), UNUSED(char *argv[])) |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 24 | { |
| 25 | int n, i; |
| Wayne Davison | 40ae4f9 | 2004-02-20 17:01:33 +0000 | [diff] [blame] | 26 | gid_t *list; |
| Wayne Davison | 670d8ab | 2004-02-04 17:05:44 +0000 | [diff] [blame] | 27 | gid_t gid = MY_GID(); |
| Wayne Davison | 58743a8 | 2004-01-22 09:13:36 +0000 | [diff] [blame] | 28 | int gid_in_list = 0; |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 29 | |
| Wayne Davison | 4f5b075 | 2005-02-14 00:53:43 +0000 | [diff] [blame] | 30 | #ifdef HAVE_GETGROUPS |
| Wayne Davison | 40ae4f9 | 2004-02-20 17:01:33 +0000 | [diff] [blame] | 31 | if ((n = getgroups(0, NULL)) < 0) { |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 32 | perror("getgroups"); |
| 33 | return 1; |
| 34 | } |
| Wayne Davison | 1df395f | 2004-02-04 17:49:36 +0000 | [diff] [blame] | 35 | #else |
| 36 | n = 0; |
| 37 | #endif |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 38 | |
| Wayne Davison | 40ae4f9 | 2004-02-20 17:01:33 +0000 | [diff] [blame] | 39 | 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 Davison | 4f5b075 | 2005-02-14 00:53:43 +0000 | [diff] [blame] | 45 | #ifdef HAVE_GETGROUPS |
| Wayne Davison | 40ae4f9 | 2004-02-20 17:01:33 +0000 | [diff] [blame] | 46 | if (n > 0) |
| 47 | n = getgroups(n, list); |
| 48 | #endif |
| 49 | |
| Wayne Davison | 58743a8 | 2004-01-22 09:13:36 +0000 | [diff] [blame] | 50 | for (i = 0; i < n; i++) { |
| Wayne Davison | f358487 | 2004-01-04 07:06:05 +0000 | [diff] [blame] | 51 | printf("%lu ", (unsigned long)list[i]); |
| Wayne Davison | 58743a8 | 2004-01-22 09:13:36 +0000 | [diff] [blame] | 52 | 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 Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 58 | printf("\n"); |
| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 59 | |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 60 | return 0; |
| 61 | } |