From c1a55093ec681c4887ed935b7089051bb36f64b7 Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Tue, 8 Dec 2020 15:08:25 +0100 Subject: [PATCH] id: code cleanup --- src/id.c | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/id.c b/src/id.c index d8c0b40..0e14c4c 100644 --- a/src/id.c +++ b/src/id.c @@ -35,6 +35,7 @@ static int _id_G(char const * user, int flagn); static int _id_g(char const * user, int flagn, int flagr); static int _id_u(char const * user, int flagn, int flagr); static int _id_all(char const * user); +static gid_t * _id_getgroups(int * n); static int _id(char const * user, int flag, int flagn, int flagr) { @@ -236,24 +237,8 @@ static int _id_all(char const * user) return _id_error(gr->gr_name, 1); } } - if((n = getgroups(0, NULL)) < 0) - { - putchar('\n'); - return _id_error("getgroups", 1); - } - if(n == 0) - groups = NULL; - else if((groups = malloc(sizeof(*groups) * n)) == NULL) - { - putchar('\n'); - return _id_error("getgroups", 1); - } - else if((n = getgroups(n, groups)) < 0) - { - putchar('\n'); - free(groups); - return _id_error("getgroups", 1); - } + if((groups = _id_getgroups(&n)) == NULL && n < 0) + return 1; printf("%s", " groups="); for(i = 0; i < n; i++) if((gr = getgrgid(groups[i])) == NULL) @@ -283,6 +268,31 @@ static struct group * _print_gid(gid_t gid) return gr; } +static gid_t * _id_getgroups(int * n) +{ + gid_t * groups; + + if((*n = getgroups(0, NULL)) < 0) + { + _id_error("getgroups", 1); + return NULL; + } + if(*n == 0) + groups = NULL; + else if((groups = malloc(sizeof(*groups) * (*n))) == NULL) + { + _id_error("malloc", 1); + return NULL; + } + if((*n = getgroups(*n, groups)) < 0) + { + free(groups); + _id_error("getgroups", 1); + return NULL; + } + return groups; +} + /* usage */ static int _usage(void)