can_user_push.c: simplify code
[girocco.git] / src / can_user_push.c
blob6ea89c05583e682a3fed02b73aaa6ae5d56f378c
1 /*
3 can_user_push.c -- check project membership in group file
4 Copyright (c) 2013 Kyle J. McKay. All rights reserved.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 USAGE: can_user_push groupname [username]
24 Exit code 0 for yes, non-zero for no
25 If username is an explicit member of groupname returns yes
26 If username is omitted, the LOGNAME environment variable will be used
27 Wrong number of or bad arguments exits with non-zero
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <grp.h>
35 int main(int argc, char *argv[])
37 const char *group;
38 const char *user;
39 struct group *ent;
40 int found = 0;
42 if (argc < 2 || argc > 3)
43 return EXIT_FAILURE;
45 group = argv[1];
46 user = (argc >= 3) ? argv[2] : getenv("LOGNAME");
47 if (!group || !*group || !user || !*user)
48 return EXIT_FAILURE;
50 ent = getgrnam(group);
51 if (ent) {
52 char **memb;
53 memb = ent->gr_mem;
54 if (memb) {
55 for (; *memb; ++memb) {
56 if (strcmp(*memb, user) == 0) {
57 found = 1;
58 break;
64 return found ? EXIT_SUCCESS : EXIT_FAILURE;