diff options
Diffstat (limited to '')
-rw-r--r-- | command/id.c (renamed from src/commands/id.c) | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/src/commands/id.c b/command/id.c index 3bef4f6..3a63989 100644 --- a/src/commands/id.c +++ b/command/id.c @@ -1,9 +1,20 @@ -#include "../command.h" +#include "args.h" +#include "command.h" +#include "lslib.h" +#include <errno.h> #include <grp.h> #include <pwd.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> -COMMAND_EMPTY(user_id) { +static void help (void) { + printf("Usage: id [USER]\n\n"); + printf("Print information about the USER\n"); +} + +COMMAND(user_id) { uid_t uid; gid_t gid, *groups; @@ -11,13 +22,24 @@ COMMAND_EMPTY(user_id) { struct passwd* pw; struct group* ugr; - uid = getuid(); - gid = getgid(); + parse_help(argc, argv, help); - pw = getpwuid(uid); + if (argc < 1) { + uid = getuid(); + pw = getpwuid(uid); + } else { + pw = getpwnam(argv[0]); + } + if(pw == NULL){ - error("failed to fetch groups: %s", strerror(errno)); + if (errno == 0) { + error("user not found"); + } else { + error("failed to fetch groups: %s", strerror(errno)); + } } + + uid = pw->pw_uid; ngroups = 0; getgrouplist(pw->pw_name, pw->pw_gid, NULL, &ngroups); @@ -25,6 +47,7 @@ COMMAND_EMPTY(user_id) { groups = malloc(sizeof(gid_t) * ngroups); getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); + gid = pw->pw_gid; ugr = getgrgid(gid); printf("uid=%d(%s) gid=%d(%s) ", uid, ugr->gr_name, gid, ugr->gr_name); |