logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 9c217fe1593ca848e178a61338eae474b18ed9c1
parent c27d050f40099ebe2f65cf4e49687e96f0f79b77
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 12 Mar 2022 02:33:52 +0100

bin/id: Simplify printing ids

Diffstat:

Mbin/id.c101++++++++++++++++++++++++-------------------------------------------------------
1 file changed, 31 insertions(+), 70 deletions(-)

diff --git a/bin/id.c b/bin/id.c @@ -57,7 +57,7 @@ list_groups(struct passwd *pw) } else { - int ret = printf("%d", groups[i]); + int ret = printf("%u", groups[i]); if(ret < 0) { goto failure; @@ -89,52 +89,52 @@ failure: } int -print_gid(char *field, char *name, gid_t gid) +print_gid(char *field, struct group *gr, gid_t gid) { - if(name_flag) + if(gr && gr->gr_name) { - if(name) + assert(gr->gr_gid == gid); + + if(name_flag) { - return printf("%s=%s", field, name); + return printf("%s=%s", field, gr->gr_name); } - else - { - return -1; - } - } - if(name) - { - return printf("%s=%d(%s)", field, gid, name); + return printf("%s=%u(%s)", field, gid, gr->gr_name); } else { - return printf("%s=%d", field, gid); + if(name_flag) + { + return -1; + } + + return printf("%s=%u", field, gid); } } int -print_uid(char *field, char *name, uid_t uid) +print_uid(char *field, struct passwd *pw, uid_t uid) { - if(name_flag) + if(pw && pw->pw_name) { - if(name) - { - return printf("%s=%s", field, name); - } - else + assert(pw->pw_uid == uid); + + if(name_flag) { - return -1; + return printf("%s=%s", field, pw->pw_name); } - } - if(name) - { - return printf("%s=%d(%s)", field, uid, name); + return printf("%s=%u(%s)", field, uid, pw->pw_name); } else { - return printf("%s=%d", field, uid); + if(name_flag) + { + return -1; + } + + return printf("%s=%u", field, uid); } } @@ -177,17 +177,7 @@ main(int argc, char *argv[]) struct passwd *epw = getpwuid(euid); // can return NULL (ie. without /etc/passwd) - if(pw != NULL) - { - assert(pw->pw_uid == uid); - - ret = print_uid("uid", pw->pw_name, uid); - } - else - { - ret = print_uid("uid", NULL, uid); - } - + ret = print_uid("uid", pw, uid); if(ret < 0) { return 1; @@ -195,17 +185,7 @@ main(int argc, char *argv[]) if(euid != uid) { - if(epw != NULL) - { - assert(epw->pw_uid == euid); - - ret = print_uid(" euid", epw->pw_name, euid); - } - else - { - ret = print_uid(" euid", NULL, euid); - } - + ret = print_uid(" euid", epw, euid); if(ret < 0) { return 1; @@ -214,17 +194,7 @@ main(int argc, char *argv[]) struct group *gr = getgrgid(gid); - if(gr != NULL) - { - assert(gr->gr_gid == gid); - - ret = print_gid(" gid", gr->gr_name, gid); - } - else - { - ret = print_gid(" gid", NULL, gid); - } - + ret = print_gid(" gid", gr, gid); if(ret < 0) { return 1; @@ -233,16 +203,7 @@ main(int argc, char *argv[]) struct group *egr = getgrgid(egid); if(egid != gid) { - if(egr != NULL) - { - assert(egr->gr_gid == egid); - - ret = print_gid(" egid", egr->gr_name, egid); - } - else - { - ret = print_gid(" egid", NULL, egid); - } + ret = print_gid(" egid", egr, egid); if(ret < 0) {