From d463af089e50911fc67cef8c633285a563abd08a Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Fri, 1 Nov 2019 10:57:30 -0400 Subject: use an enum for display mode --- id.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/id.c b/id.c index 523ee7d..caa84d9 100644 --- a/id.c +++ b/id.c @@ -25,19 +25,15 @@ #define _XOPEN_SOURCE 700 #include #include -#include #include #include #include #include #include -#define FULL 0 -#define NAMES 1 -#define NUMS 2 - enum type { USER, GROUP }; enum mode { DEFAULT, UID, GID, ALL_GID }; +enum display { FULL, NAMES, NUMBERS }; static char *get_name(enum type type, id_t id) { @@ -56,7 +52,7 @@ static char *get_name(enum type type, id_t id) return NULL; } -static void print_id(const char *prefix, const char *name, id_t id, int mode) +static void print_id(const char *prefix, const char *name, id_t id, enum display mode) { printf("%s", prefix); if (mode == NAMES) { @@ -65,7 +61,7 @@ static void print_id(const char *prefix, const char *name, id_t id, int mode) } else { printf("%ju", (uintmax_t)id); } - } else if (mode == NUMS) { + } else if (mode == NUMBERS) { printf("%ju", (uintmax_t)id); } else { if (name) { @@ -76,7 +72,7 @@ static void print_id(const char *prefix, const char *name, id_t id, int mode) } } -static void print_groups(uid_t uid, int mode) +static void print_groups(uid_t uid, enum display mode) { struct passwd *pwd = getpwuid(uid); if (pwd == NULL) { @@ -107,7 +103,7 @@ static void print_groups(uid_t uid, int mode) int main(int argc, char *argv[]) { - bool names = false; + enum display dmode = NUMBERS; enum mode mode = DEFAULT; int c; uid_t ruid = getuid(); @@ -132,7 +128,7 @@ int main(int argc, char *argv[]) break; case 'n': - names = true; + dmode = NAMES; break; case 'r': @@ -165,11 +161,11 @@ int main(int argc, char *argv[]) if (mode == ALL_GID) { /* TODO: output real and/or effective gids if necessary */ - print_groups(uid, names ? NAMES : NUMS); + print_groups(uid, dmode); } else if (mode == GID) { - print_id("", get_name(GROUP, gid), gid, names); + print_id("", get_name(GROUP, gid), gid, dmode); } else if (mode == UID) { - print_id("", get_name(USER, uid), uid, names); + print_id("", get_name(USER, uid), uid, dmode); } else { print_id("uid=", get_name(USER, ruid), ruid, 0); print_id(" gid=", get_name(GROUP, rgid), rgid, 0); -- cgit v1.2.1