| 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 |
| 6 | * Copyright (C) 2003, 2004 Wayne Davison |
| 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 |
| 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 Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 12 | * |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 13 | * 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 Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 17 | * |
| Wayne Davison | e7c6706 | 2006-04-25 23:51:12 +0000 | [diff] [blame^] | 18 | * 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 Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 23 | #include "rsync.h" |
| 24 | |
| Wayne Davison | f69204a | 2003-09-10 08:27:34 +0000 | [diff] [blame] | 25 | int |
| 26 | main(UNUSED(int argc), UNUSED(char *argv[])) |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 27 | { |
| 28 | int n, i; |
| Wayne Davison | 40ae4f9 | 2004-02-20 17:01:33 +0000 | [diff] [blame] | 29 | gid_t *list; |
| Wayne Davison | 670d8ab | 2004-02-04 17:05:44 +0000 | [diff] [blame] | 30 | gid_t gid = MY_GID(); |
| Wayne Davison | 58743a8 | 2004-01-22 09:13:36 +0000 | [diff] [blame] | 31 | int gid_in_list = 0; |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 32 | |
| Wayne Davison | 4f5b075 | 2005-02-14 00:53:43 +0000 | [diff] [blame] | 33 | #ifdef HAVE_GETGROUPS |
| Wayne Davison | 40ae4f9 | 2004-02-20 17:01:33 +0000 | [diff] [blame] | 34 | if ((n = getgroups(0, NULL)) < 0) { |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 35 | perror("getgroups"); |
| 36 | return 1; |
| 37 | } |
| Wayne Davison | 1df395f | 2004-02-04 17:49:36 +0000 | [diff] [blame] | 38 | #else |
| 39 | n = 0; |
| 40 | #endif |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 41 | |
| Wayne Davison | 40ae4f9 | 2004-02-20 17:01:33 +0000 | [diff] [blame] | 42 | 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 Davison | 4f5b075 | 2005-02-14 00:53:43 +0000 | [diff] [blame] | 48 | #ifdef HAVE_GETGROUPS |
| Wayne Davison | 40ae4f9 | 2004-02-20 17:01:33 +0000 | [diff] [blame] | 49 | if (n > 0) |
| 50 | n = getgroups(n, list); |
| 51 | #endif |
| 52 | |
| Wayne Davison | 58743a8 | 2004-01-22 09:13:36 +0000 | [diff] [blame] | 53 | for (i = 0; i < n; i++) { |
| Wayne Davison | f358487 | 2004-01-04 07:06:05 +0000 | [diff] [blame] | 54 | printf("%lu ", (unsigned long)list[i]); |
| Wayne Davison | 58743a8 | 2004-01-22 09:13:36 +0000 | [diff] [blame] | 55 | 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 Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 61 | printf("\n"); |
| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 62 | |
| Martin Pool | 4acbfa2 | 2002-03-25 02:55:52 +0000 | [diff] [blame] | 63 | return 0; |
| 64 | } |