[PATCH] Add initial support for id(1)

From: sin <sin_AT_2f30.org>
Date: Wed, 7 Aug 2013 13:40:21 +0100

---
 Makefile |  1 +
 id.1     |  8 ++++++++
 id.c     | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 79 insertions(+)
 create mode 100644 id.1
 create mode 100644 id.c
diff --git a/Makefile b/Makefile
index 2e31f33..33c78a0 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -46,6 +46,7 @@ SRC = \
 	fold.c     \
 	grep.c     \
 	head.c     \
+	id.c       \
 	kill.c     \
 	ln.c       \
 	ls.c       \
diff --git a/id.1 b/id.1
new file mode 100644
index 0000000..a5678e3
--- /dev/null
+++ b/id.1
_AT_@ -0,0 +1,8 @@
+.TH ID 1 sbase\-VERSION
+.SH NAME
+id \- print real and effective user and group IDs
+.SH DESCRIPTION
+.B id
+Print user and group information for the current user.
+.SH SEE ALSO
+.IR who(1)
diff --git a/id.c b/id.c
new file mode 100644
index 0000000..19248d0
--- /dev/null
+++ b/id.c
_AT_@ -0,0 +1,70 @@
+/* See LICENSE file for copyright and license details. */
+#include <sys/types.h>
+#include <unistd.h>
+#include <pwd.h>
+#include <grp.h>
+#include <stdio.h>
+#include <limits.h>
+#include "util.h"
+
+static void
+usage(void)
+{
+	eprintf("usage: %s\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+	struct passwd *pw;
+	struct group *gr;
+	uid_t uid, euid;
+	gid_t gid, egid, groups[NGROUPS_MAX];
+	int ngroups;
+	int i;
+
+	ARGBEGIN {
+	default:
+		usage();
+	} ARGEND;
+
+	/* Print uid/euid info */
+	uid = getuid();
+	printf("uid=%u", uid);
+	if (!(pw = getpwuid(uid)))
+		eprintf("getpwuid:");
+	printf("(%s)", pw->pw_name);
+	if ((euid = geteuid()) != uid) {
+		printf(" euid=%u", euid);
+		if (!(pw = getpwuid(euid)))
+			eprintf("getpwuid:");
+		printf("(%s)", pw->pw_name);
+	}
+
+	/* Print gid/egid info */
+	gid = getgid();
+	printf(" gid=%u", gid);
+	if (!(gr = getgrgid(gid)))
+		eprintf("getgrgid:");
+	printf("(%s)", gr->gr_name);
+	if ((egid = getegid()) != gid) {
+		printf(" egid=%u", egid);
+		if (!(gr = getgrgid(egid)))
+			eprintf("getgrgid:");
+		printf("(%s)", gr->gr_name);
+	}
+
+	/* Print groups */
+	ngroups = getgroups(NGROUPS_MAX, groups);
+	if (ngroups < 0)
+		eprintf("getgroups:");
+	for (i = 0; i < ngroups; i++) {
+		gid = groups[i];
+		printf("%s%u", !i ? " groups=" : ",", gid);
+		if (!(gr = getgrgid(gid)))
+			eprintf("getgrgid:");
+		printf("(%s)", gr->gr_name);
+	}
+	putchar('\n');
+	return 0;
+}
-- 
1.8.2.3
--G4iJoqBmSsgzjUCe--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Thu Aug 08 2013 - 05:00:04 CEST