summaryrefslogtreecommitdiff
path: root/command/id.c
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-05-06 00:39:44 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-05-06 00:39:44 -0400
commitd8f2c10b7108fff6b7e437291093a1cadc15ab9f (patch)
tree3fc50a19d6fbb9c94a8fe147cd2a6c4ba7f59b8d /command/id.c
parentansii c (diff)
downloadlazysphere-d8f2c10b7108fff6b7e437291093a1cadc15ab9f.tar.gz
lazysphere-d8f2c10b7108fff6b7e437291093a1cadc15ab9f.tar.bz2
lazysphere-d8f2c10b7108fff6b7e437291093a1cadc15ab9f.zip
refactor
Diffstat (limited to 'command/id.c')
-rw-r--r--command/id.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/command/id.c b/command/id.c
new file mode 100644
index 0000000..3a63989
--- /dev/null
+++ b/command/id.c
@@ -0,0 +1,76 @@
+#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>
+
+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;
+ int ngroups, i;
+ struct passwd* pw;
+ struct group* ugr;
+
+ parse_help(argc, argv, help);
+
+ if (argc < 1) {
+ uid = getuid();
+ pw = getpwuid(uid);
+ } else {
+ pw = getpwnam(argv[0]);
+ }
+
+ if(pw == NULL){
+ 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);
+
+ 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);
+
+ if (ngroups > 0) {
+ printf("groups=");
+ }
+
+ for (i = 0; i < ngroups; i++){
+ struct group* gr = getgrgid(groups[i]);
+ if(gr == NULL) {
+ free(groups);
+ error("failed to fetch groups: %s", strerror(errno));
+ }
+
+ printf("%d(%s)", gr->gr_gid, gr->gr_name);
+
+ if (i + 1 < ngroups) putchar(',');
+ }
+
+ printf("\b\n");
+
+ free(groups);
+
+ return EXIT_SUCCESS;
+}