mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-31 13:07:46 +00:00
whoami: use uucore::entries
This commit is contained in:
parent
99f0114450
commit
270290efe6
3 changed files with 10 additions and 21 deletions
|
@ -9,10 +9,13 @@ path = "whoami.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
getopts = "*"
|
getopts = "*"
|
||||||
libc = "*"
|
|
||||||
winapi = "*"
|
winapi = "*"
|
||||||
advapi32-sys = "*"
|
advapi32-sys = "*"
|
||||||
uucore = { path="../uucore" }
|
|
||||||
|
[dependencies.uucore]
|
||||||
|
path = "../uucore"
|
||||||
|
default-features = false
|
||||||
|
features = ["entries"]
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "whoami"
|
name = "whoami"
|
||||||
|
|
|
@ -2,31 +2,18 @@
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
*
|
*
|
||||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
* (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
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::io::{Result, Error};
|
use std::io::Result;
|
||||||
use ::libc;
|
use uucore::libc::geteuid;
|
||||||
use uucore::c_types::{c_passwd, getpwuid};
|
use uucore::entries::uid2usr;
|
||||||
|
|
||||||
extern {
|
|
||||||
pub fn geteuid() -> libc::uid_t;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub unsafe fn getusername() -> Result<String> {
|
pub unsafe fn getusername() -> Result<String> {
|
||||||
// Get effective user id
|
// Get effective user id
|
||||||
let uid = geteuid();
|
let uid = geteuid();
|
||||||
|
uid2usr(uid)
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,6 @@
|
||||||
/* last synced with: whoami (GNU coreutils) 8.21 */
|
/* last synced with: whoami (GNU coreutils) 8.21 */
|
||||||
|
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue