mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
commit
637d2c72d0
2 changed files with 39 additions and 13 deletions
4
Makefile
4
Makefile
|
@ -41,6 +41,7 @@ PROGS := \
|
||||||
yes \
|
yes \
|
||||||
head \
|
head \
|
||||||
tail \
|
tail \
|
||||||
|
whoami
|
||||||
|
|
||||||
UNIX_PROGS := \
|
UNIX_PROGS := \
|
||||||
groups \
|
groups \
|
||||||
|
@ -53,8 +54,7 @@ UNIX_PROGS := \
|
||||||
tty \
|
tty \
|
||||||
uname \
|
uname \
|
||||||
uptime \
|
uptime \
|
||||||
users \
|
users
|
||||||
whoami
|
|
||||||
|
|
||||||
ifneq ($(OS),Windows_NT)
|
ifneq ($(OS),Windows_NT)
|
||||||
PROGS := $(PROGS) $(UNIX_PROGS)
|
PROGS := $(PROGS) $(UNIX_PROGS)
|
||||||
|
|
|
@ -20,23 +20,49 @@ extern crate libc;
|
||||||
|
|
||||||
use std::io::print;
|
use std::io::print;
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::str;
|
|
||||||
use c_types::{c_passwd, getpwuid};
|
|
||||||
|
|
||||||
#[path = "../common/util.rs"] mod util;
|
#[path = "../common/util.rs"] mod util;
|
||||||
#[path = "../common/c_types.rs"] mod c_types;
|
|
||||||
|
|
||||||
extern {
|
#[cfg(unix)]
|
||||||
pub fn geteuid() -> libc::c_int;
|
mod platform {
|
||||||
|
use super::libc;
|
||||||
|
use std::str;
|
||||||
|
use self::c_types::{c_passwd, getpwuid};
|
||||||
|
|
||||||
|
#[path = "../../common/c_types.rs"] mod c_types;
|
||||||
|
|
||||||
|
extern {
|
||||||
|
pub fn geteuid() -> libc::c_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe fn getusername() -> String {
|
||||||
|
let passwd: *c_passwd = getpwuid(geteuid());
|
||||||
|
|
||||||
|
let pw_name: *libc::c_char = (*passwd).pw_name;
|
||||||
|
let name = str::raw::from_c_str(pw_name);
|
||||||
|
|
||||||
|
name
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn getusername() -> String {
|
#[cfg(windows)]
|
||||||
let passwd: *c_passwd = getpwuid(geteuid());
|
mod platform {
|
||||||
|
pub use super::libc;
|
||||||
|
use std::mem;
|
||||||
|
use std::str;
|
||||||
|
|
||||||
let pw_name: *libc::c_char = (*passwd).pw_name;
|
extern "system" {
|
||||||
let name = str::raw::from_c_str(pw_name);
|
pub fn GetUserNameA(out: *libc::c_char, len: *libc::uint32_t) -> libc::uint8_t;
|
||||||
|
}
|
||||||
|
|
||||||
name
|
#[allow(unused_unsafe)]
|
||||||
|
pub unsafe fn getusername() -> String {
|
||||||
|
let buffer: [libc::c_char, ..2048] = mem::uninitialized(); // XXX: it may be possible that this isn't long enough. I don't know
|
||||||
|
if !GetUserNameA(buffer.as_ptr(), &(buffer.len() as libc::uint32_t)) == 0 {
|
||||||
|
crash!(1, "username is too long");
|
||||||
|
}
|
||||||
|
str::raw::from_c_str(buffer.as_ptr())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static NAME: &'static str = "whoami";
|
static NAME: &'static str = "whoami";
|
||||||
|
@ -75,7 +101,7 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
|
|
||||||
pub fn exec() {
|
pub fn exec() {
|
||||||
unsafe {
|
unsafe {
|
||||||
let username = getusername();
|
let username = platform::getusername();
|
||||||
println!("{:s}", username);
|
println!("{:s}", username);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue