whoami
A reimplementation of the Linux command whoami in Rust.
Overview
whoami displays the user name associated with the current effective user ID. Same as id -un.
Example:
whoami
csm
Development Environment
- Operating System: Linux Mint
- Rust Toolchain
Building the Project
- clone the repo
- cd into the cloned repo
- to build:
cargo build --release
- to build and run:
cargo run -- -help
Commands
whoami <no args> -> Print effective userid
whoami -version, -v -> Print whoami version
whoami -help, -h -> Print whoami commands
License
MIT
What I learned?
- Simple command does not mean simple to implement
- For the first time wrote some unsafe rust code with
libccrate.
Top comments (2)
Nice project for getting hands-on with Rust's std::os primitives. On Unix you're likely calling
getlogin()or falling back togetpwuid(getuid())— curious how you handle the case wheregetlogin()returns None in containerized environments (common withdocker run --user). On Windows the equivalent isGetUserNameW, which behaves differently with impersonation tokens. Did you abstract the platform bits behind a trait or justcfgthe impl blocks? Also, any reason not to shell out tostd::env::var("USER")/"USERNAME"as a last resort? It's not authoritative but covers weird sandbox setups where the syscall fails PS: the tool I meant is on labagent .techThanks for the feedback! 😊
To be honest, I did not think about containerized environments 😅 .
I used
#[cfg(unix)]with theclibcrate, since this is a Linux-specific project.Environment variables like
USERaren't always accurate. For example,sudo whoamishould returnroot, butUSERmay still contain the original user's name, and users can modify environment variables themselves. Using it as a fallback is a good idea, though!I wanted to keep the behavior consistent with the original Linux implementation.