commit: d1389313d74289a2226c348e2eb26f299c14d61b
parent 9f6f97bb6724cf7780a25d2e34379fc77baec581
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sat, 7 Dec 2024 14:33:03 +0100
cmd/whoami: new command
Diffstat:
3 files changed, 48 insertions(+), 1 deletion(-)
diff --git a/cmd/whoami.1 b/cmd/whoami.1
@@ -0,0 +1,21 @@
+.\" utils-std: Collection of commonly available Unix tools
+.\" Copyright 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+.\" SPDX-License-Identifier: MPL-2.0
+.Dd 2024-12-07
+.Dt WHOAMI 1
+.Os
+.Sh NAME
+.Nm whoami
+.Nd return username
+.Sh SYNOPSIS
+.Nm
+.Sh DESCRIPTION
+.Nm
+prints the username of the current effective user ID, equivalent to
+.Cm id
+.Fl un .
+.Sh SEE ALSO
+.Xr id 1 ,
+.Xr logname 1
+.Sh AUTHORS
+.An Haelwenn (lanodan) Monnier Aq Mt contact+utils@hacktivis.me
diff --git a/cmd/whoami.c b/cmd/whoami.c
@@ -0,0 +1,26 @@
+#include "../lib/err.h"
+#include <pwd.h> // getpwuid
+#include <unistd.h> // geteuid
+#include <stdio.h>
+#include <errno.h>
+#include <string.h> // strerror
+
+int
+main(int argc, char *argv[])
+{
+ uid_t euid = geteuid();
+ struct passwd *pw = getpwuid(euid);
+
+ if(pw == NULL)
+ {
+ const char *errstr = strerror(errno);
+
+ if(errno == 0) errstr = "Not Found";
+
+ errx(1, "Failed getting name for user ID %d: %s", euid, errstr);
+ }
+
+ puts(pw->pw_name);
+
+ return 0;
+}
diff --git a/coreutils.txt b/coreutils.txt
@@ -102,5 +102,5 @@ users: No
vdir: No, use ls
wc: Done
who: No
-whoami: No. Use id
+whoami: Done
yes: Done