1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 20:47:46 +00:00

whoami: use uucore::entries

This commit is contained in:
Knight 2016-08-14 13:53:42 +08:00
parent 99f0114450
commit 270290efe6
3 changed files with 10 additions and 21 deletions

View file

@ -9,10 +9,13 @@ path = "whoami.rs"
[dependencies]
getopts = "*"
libc = "*"
winapi = "*"
advapi32-sys = "*"
uucore = { path="../uucore" }
[dependencies.uucore]
path = "../uucore"
default-features = false
features = ["entries"]
[[bin]]
name = "whoami"

View file

@ -2,31 +2,18 @@
* This file is part of the uutils coreutils package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
* (c) Jian Zeng <anonymousknight96 AT gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use std::io::{Result, Error};
use ::libc;
use uucore::c_types::{c_passwd, getpwuid};
extern {
pub fn geteuid() -> libc::uid_t;
}
use std::io::Result;
use uucore::libc::geteuid;
use uucore::entries::uid2usr;
pub unsafe fn getusername() -> Result<String> {
// Get effective user id
let uid = geteuid();
// Try to find username for uid
let passwd: *const c_passwd = getpwuid(uid);
if passwd.is_null() {
return Err(Error::last_os_error())
}
// Extract username from passwd struct
let pw_name: *const libc::c_char = (*passwd).pw_name;
let username = String::from_utf8_lossy(::std::ffi::CStr::from_ptr(pw_name).to_bytes()).to_string();
Ok(username)
uid2usr(uid)
}

View file

@ -12,7 +12,6 @@
/* last synced with: whoami (GNU coreutils) 8.21 */
extern crate getopts;
extern crate libc;
#[macro_use]
extern crate uucore;