logo

utils-std

Collection of commonly available Unix tools
commit: 7cc2ea7f4ed4c9fae6789e94761d0e7a5c842e29
parent ee7d8eb47d2ed1e4d7a671c0892d7ef2c827bd37
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu,  1 Aug 2024 03:33:48 +0200

cmd/id: Fix separator logic to account due to skips

ngroups isn't a good source due to basegid being skipped when already printed out.

Diffstat:

Mcmd/id.c39+++++++++++++++++++--------------------
1 file changed, 19 insertions(+), 20 deletions(-)

diff --git a/cmd/id.c b/cmd/id.c @@ -16,6 +16,7 @@ bool name_flag = false; static int simple_list_groups(struct passwd *pw, int ngroups, gid_t *groups) { + bool first = true; // Don't print basegid twice when present in pw and grouplist bool basegid_done = false; @@ -34,28 +35,29 @@ simple_list_groups(struct passwd *pw, int ngroups, gid_t *groups) struct group *lgr = getgrgid(groups[i]); if(lgr == NULL) return 1; - int sep = ' '; - if(i == ngroups - 1) sep = '\n'; + if(!first) putchar(' '); - int ret = printf("%s%c", lgr->gr_name, sep); - if(ret < 0) return 1; + printf("%s", lgr->gr_name); } else { - int sep = ' '; - if(i == ngroups - 1) sep = '\n'; + if(!first) putchar(' '); - int ret = printf("%u%c", groups[i], sep); - if(ret < 0) return 1; + printf("%u", groups[i]); } + + first = false; } + putchar('\n'); + return 0; } static int list_groups(struct passwd *pw, int ngroups, gid_t *groups) { + bool first = true; // Don't print basegid twice when present in pw and grouplist bool basegid_done = false; @@ -77,27 +79,24 @@ list_groups(struct passwd *pw, int ngroups, gid_t *groups) { if(lgr == NULL) return 1; - int sep = ' '; - if(i == ngroups - 1) sep = '\0'; + if(!first) putchar(' '); - int ret = printf("%s%c", lgr->gr_name, sep); - if(ret < 0) return 1; + printf("%s", lgr->gr_name); } else { - int ret = printf("%u", groups[i]); - if(ret < 0) return 1; + if(!first) putchar(','); - if(lgr != NULL) - { - int ret = printf("(%s)", lgr->gr_name); - if(ret < 0) return 1; - } + printf("%u", groups[i]); - if(i != ngroups - 1) printf(","); + if(lgr != NULL) printf("(%s)", lgr->gr_name); } + + first = false; } + putchar('\n'); + return 0; }