mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-09-15 19:36:16 +00:00
commit
3e8b4045c8
7 changed files with 45 additions and 65 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -3222,6 +3222,7 @@ name = "uu_whoami"
|
||||||
version = "0.0.7"
|
version = "0.0.7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"libc",
|
||||||
"uucore",
|
"uucore",
|
||||||
"uucore_procs",
|
"uucore_procs",
|
||||||
"winapi 0.3.9",
|
"winapi 0.3.9",
|
||||||
|
|
|
@ -16,12 +16,15 @@ path = "src/whoami.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "2.33", features = ["wrap_help"] }
|
clap = { version = "2.33", features = ["wrap_help"] }
|
||||||
uucore = { version=">=0.0.9", package="uucore", path="../../uucore", features=["entries", "wide"] }
|
uucore = { version=">=0.0.9", package="uucore", path="../../uucore", features=["entries"] }
|
||||||
uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" }
|
uucore_procs = { version=">=0.0.6", package="uucore_procs", path="../../uucore_procs" }
|
||||||
|
|
||||||
[target.'cfg(target_os = "windows")'.dependencies]
|
[target.'cfg(target_os = "windows")'.dependencies]
|
||||||
winapi = { version = "0.3", features = ["lmcons"] }
|
winapi = { version = "0.3", features = ["lmcons"] }
|
||||||
|
|
||||||
|
[target.'cfg(unix)'.dependencies]
|
||||||
|
libc = "0.2.42"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "whoami"
|
name = "whoami"
|
||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
|
@ -8,14 +8,14 @@
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// spell-checker:ignore (ToDO) getusername
|
use std::ffi::OsString;
|
||||||
|
use std::io;
|
||||||
|
|
||||||
use std::io::Result;
|
|
||||||
use uucore::entries::uid2usr;
|
use uucore::entries::uid2usr;
|
||||||
use uucore::libc::geteuid;
|
|
||||||
|
|
||||||
pub unsafe fn get_username() -> Result<String> {
|
pub fn get_username() -> io::Result<OsString> {
|
||||||
// Get effective user id
|
// SAFETY: getuid() does nothing with memory and is always successful.
|
||||||
let uid = geteuid();
|
let uid = unsafe { libc::geteuid() };
|
||||||
uid2usr(uid)
|
// uid2usr should arguably return an OsString but currently doesn't
|
||||||
|
uid2usr(uid).map(Into::into)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,22 +7,21 @@
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
extern crate winapi;
|
use std::ffi::OsString;
|
||||||
|
use std::io;
|
||||||
|
use std::os::windows::ffi::OsStringExt;
|
||||||
|
|
||||||
use self::winapi::shared::lmcons;
|
use winapi::shared::lmcons;
|
||||||
use self::winapi::shared::minwindef;
|
use winapi::shared::minwindef::DWORD;
|
||||||
use self::winapi::um::{winbase, winnt};
|
use winapi::um::winbase;
|
||||||
use std::io::{Error, Result};
|
|
||||||
use std::mem;
|
|
||||||
use uucore::wide::FromWide;
|
|
||||||
|
|
||||||
pub unsafe fn get_username() -> Result<String> {
|
pub fn get_username() -> io::Result<OsString> {
|
||||||
#[allow(deprecated)]
|
const BUF_LEN: DWORD = lmcons::UNLEN + 1;
|
||||||
let mut buffer: [winnt::WCHAR; lmcons::UNLEN as usize + 1] = mem::uninitialized();
|
let mut buffer = [0_u16; BUF_LEN as usize];
|
||||||
let mut len = buffer.len() as minwindef::DWORD;
|
let mut len = BUF_LEN;
|
||||||
if winbase::GetUserNameW(buffer.as_mut_ptr(), &mut len) == 0 {
|
// SAFETY: buffer.len() == len
|
||||||
return Err(Error::last_os_error());
|
if unsafe { winbase::GetUserNameW(buffer.as_mut_ptr(), &mut len) } == 0 {
|
||||||
|
return Err(io::Error::last_os_error());
|
||||||
}
|
}
|
||||||
let username = String::from_wide(&buffer[..len as usize - 1]);
|
Ok(OsString::from_wide(&buffer[..len as usize - 1]))
|
||||||
Ok(username)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use clap::App;
|
|
||||||
|
|
||||||
// * 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>
|
||||||
|
@ -14,46 +12,25 @@ extern crate clap;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
use uucore::error::{UResult, USimpleError};
|
use clap::App;
|
||||||
|
|
||||||
|
use uucore::display::println_verbatim;
|
||||||
|
use uucore::error::{FromIo, UResult};
|
||||||
|
|
||||||
mod platform;
|
mod platform;
|
||||||
|
|
||||||
|
static ABOUT: &str = "Print the current username.";
|
||||||
|
|
||||||
#[uucore_procs::gen_uumain]
|
#[uucore_procs::gen_uumain]
|
||||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let app = uu_app();
|
uu_app().get_matches_from(args);
|
||||||
|
let username = platform::get_username().map_err_context(|| "failed to get username".into())?;
|
||||||
if let Err(err) = app.get_matches_from_safe(args) {
|
println_verbatim(&username).map_err_context(|| "failed to print username".into())?;
|
||||||
if err.kind == clap::ErrorKind::HelpDisplayed
|
|
||||||
|| err.kind == clap::ErrorKind::VersionDisplayed
|
|
||||||
{
|
|
||||||
println!("{}", err);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
|
||||||
return Err(USimpleError::new(1, format!("{}", err)));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
exec()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uu_app() -> App<'static, 'static> {
|
pub fn uu_app() -> App<'static, 'static> {
|
||||||
app_from_crate!()
|
App::new(uucore::util_name())
|
||||||
}
|
.version(crate_version!())
|
||||||
|
.about(ABOUT)
|
||||||
pub fn exec() -> UResult<()> {
|
|
||||||
unsafe {
|
|
||||||
match platform::get_username() {
|
|
||||||
Ok(username) => {
|
|
||||||
println!("{}", username);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
Err(err) => match err.raw_os_error() {
|
|
||||||
Some(0) | None => Err(USimpleError::new(1, "failed to get username")),
|
|
||||||
Some(_) => Err(USimpleError::new(
|
|
||||||
1,
|
|
||||||
format!("failed to get username: {}", err),
|
|
||||||
)),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
// * 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.
|
||||||
|
|
||||||
#[cfg(unix)]
|
|
||||||
use crate::common::util::*;
|
use crate::common::util::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -34,7 +33,6 @@ fn test_normal_compare_id() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(unix)]
|
|
||||||
fn test_normal_compare_env() {
|
fn test_normal_compare_env() {
|
||||||
let whoami = whoami();
|
let whoami = whoami();
|
||||||
if whoami == "nobody" {
|
if whoami == "nobody" {
|
||||||
|
|
|
@ -1069,7 +1069,9 @@ pub fn whoami() -> String {
|
||||||
|
|
||||||
// Use environment variable to get current user instead of
|
// Use environment variable to get current user instead of
|
||||||
// invoking `whoami` and fall back to user "nobody" on error.
|
// invoking `whoami` and fall back to user "nobody" on error.
|
||||||
std::env::var("USER").unwrap_or_else(|e| {
|
std::env::var("USER")
|
||||||
|
.or_else(|_| std::env::var("USERNAME"))
|
||||||
|
.unwrap_or_else(|e| {
|
||||||
println!("{}: {}, using \"nobody\" instead", UUTILS_WARNING, e);
|
println!("{}: {}, using \"nobody\" instead", UUTILS_WARNING, e);
|
||||||
"nobody".to_string()
|
"nobody".to_string()
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue