~sircmpwn/hautils

04e0578d43fd4def2edf3da0fb13defb1d7af685 — Gurvan Debaussart a month ago 23f32fd
whoami: new util

whoami: add pwent_finish
3 files changed, 25 insertions(+), 0 deletions(-)

M .gitignore
M Makefile
A whoami.ha
M .gitignore => .gitignore +1 -0
@@ 14,4 14,5 @@ tee
true
uname
uniq
whoami
wc

M Makefile => Makefile +2 -0
@@ 22,6 22,7 @@ utils=\
	true \
	uname \
	uniq \
	whoami \
	wc

all: $(utils)


@@ 52,4 53,5 @@ touch: touch.ha
true: true.ha
uname: uname.ha main/main.ha
uniq: uniq.ha main/main.ha
whoami: whoami.ha main/main.ha
wc: wc.ha main/main.ha

A whoami.ha => whoami.ha +22 -0
@@ 0,0 1,22 @@
use fmt;
use getopt;
use main;
use os;
use unix;
use unix::passwd;

export fn utilmain() (main::error | void) = {
	const help: []getopt::help = [
		"print effective user name",
	];
	const cmd = getopt::parse(os::args, help...);
	defer getopt::finish(&cmd);

	const euid = unix::geteuid();
	match (unix::passwd::getuid(euid)) {
	case let pwent: unix::passwd::pwent =>
		fmt::println(pwent.username)?;
		unix::passwd::pwent_finish(&pwent);
	case => fmt::fatal("Error: couldn't find name associated to euid");
	};
};