1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Merge pull request #4279 from rivy/up.uname

change/uname ~ update and revise
This commit is contained in:
Sylvestre Ledru 2023-01-18 11:33:29 +01:00 committed by GitHub
commit afbe850115
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 56 deletions

4
Cargo.lock generated
View file

@ -1633,9 +1633,9 @@ checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
[[package]] [[package]]
name = "platform-info" name = "platform-info"
version = "1.0.1" version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4278b2b54a23c9a91d5bae9b09e21d566f8b23890491951160b05d64f29d1d8b" checksum = "4e7c23cfae725ae06d9e43010153fa77bdfa8c827bf08fe4beeb2a3514e6be12"
dependencies = [ dependencies = [
"libc", "libc",
"winapi", "winapi",

View file

@ -15,7 +15,7 @@ edition = "2021"
path = "src/uname.rs" path = "src/uname.rs"
[dependencies] [dependencies]
platform-info = "1.0.1" platform-info = "1.0.2"
clap = { version = "4.0", features = ["wrap_help", "cargo"] } clap = { version = "4.0", features = ["wrap_help", "cargo"] }
uucore = { version=">=0.0.16", package="uucore", path="../../uucore" } uucore = { version=">=0.0.16", package="uucore", path="../../uucore" }

View file

@ -8,7 +8,7 @@
// last synced with: uname (GNU coreutils) 8.21 // last synced with: uname (GNU coreutils) 8.21
// spell-checker:ignore (ToDO) nodename kernelname kernelrelease kernelversion sysname hwplatform mnrsv // spell-checker:ignore (API) nodename osname sysname (options) mnrsv mnrsvo
use clap::{crate_version, Arg, ArgAction, Command}; use clap::{crate_version, Arg, ArgAction, Command};
use platform_info::*; use platform_info::*;
@ -23,37 +23,16 @@ const USAGE: &str = "{} [OPTION]...";
pub mod options { pub mod options {
pub static ALL: &str = "all"; pub static ALL: &str = "all";
pub static KERNELNAME: &str = "kernel-name"; pub static KERNEL_NAME: &str = "kernel-name";
pub static NODENAME: &str = "nodename"; pub static NODENAME: &str = "nodename";
pub static KERNELVERSION: &str = "kernel-version"; pub static KERNEL_VERSION: &str = "kernel-version";
pub static KERNELRELEASE: &str = "kernel-release"; pub static KERNEL_RELEASE: &str = "kernel-release";
pub static MACHINE: &str = "machine"; pub static MACHINE: &str = "machine";
pub static PROCESSOR: &str = "processor"; pub static PROCESSOR: &str = "processor";
pub static HWPLATFORM: &str = "hardware-platform"; pub static HARDWARE_PLATFORM: &str = "hardware-platform";
pub static OS: &str = "operating-system"; pub static OS: &str = "operating-system";
} }
#[cfg(all(target_os = "linux", any(target_env = "gnu", target_env = "")))]
const HOST_OS: &str = "GNU/Linux";
#[cfg(all(target_os = "linux", not(any(target_env = "gnu", target_env = ""))))]
const HOST_OS: &str = "Linux";
#[cfg(target_os = "android")]
const HOST_OS: &str = "Android";
#[cfg(target_os = "windows")]
const HOST_OS: &str = "Windows NT";
#[cfg(target_os = "freebsd")]
const HOST_OS: &str = "FreeBSD";
#[cfg(target_os = "netbsd")]
const HOST_OS: &str = "NetBSD";
#[cfg(target_os = "openbsd")]
const HOST_OS: &str = "OpenBSD";
#[cfg(target_vendor = "apple")]
const HOST_OS: &str = "Darwin";
#[cfg(target_os = "fuchsia")]
const HOST_OS: &str = "Fuchsia";
#[cfg(target_os = "redox")]
const HOST_OS: &str = "Redox";
#[uucore::main] #[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> { pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?; let matches = uu_app().try_get_matches_from(args)?;
@ -63,41 +42,42 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let mut output = String::new(); let mut output = String::new();
let all = matches.get_flag(options::ALL); let all = matches.get_flag(options::ALL);
let kernelname = matches.get_flag(options::KERNELNAME); let kernel_name = matches.get_flag(options::KERNEL_NAME);
let nodename = matches.get_flag(options::NODENAME); let nodename = matches.get_flag(options::NODENAME);
let kernelrelease = matches.get_flag(options::KERNELRELEASE); let kernel_release = matches.get_flag(options::KERNEL_RELEASE);
let kernelversion = matches.get_flag(options::KERNELVERSION); let kernel_version = matches.get_flag(options::KERNEL_VERSION);
let machine = matches.get_flag(options::MACHINE); let machine = matches.get_flag(options::MACHINE);
let processor = matches.get_flag(options::PROCESSOR); let processor = matches.get_flag(options::PROCESSOR);
let hwplatform = matches.get_flag(options::HWPLATFORM); let hardware_platform = matches.get_flag(options::HARDWARE_PLATFORM);
let os = matches.get_flag(options::OS); let os = matches.get_flag(options::OS);
let none = !(all let none = !(all
|| kernelname || kernel_name
|| nodename || nodename
|| kernelrelease || kernel_release
|| kernelversion || kernel_version
|| machine || machine
|| os || os
|| processor || processor
|| hwplatform); || hardware_platform);
if kernelname || all || none { if kernel_name || all || none {
output.push_str(&uname.sysname()); output.push_str(&uname.sysname());
output.push(' '); output.push(' ');
} }
if nodename || all { if nodename || all {
output.push_str(&uname.nodename()); // maint: [2023-01-14; rivy] remove `.trim_end_matches('\0')` when platform-info nodename-NUL bug is fixed (see GH:uutils/platform-info/issues/32)
output.push_str(uname.nodename().trim_end_matches('\0'));
output.push(' '); output.push(' ');
} }
if kernelrelease || all { if kernel_release || all {
output.push_str(&uname.release()); output.push_str(&uname.release());
output.push(' '); output.push(' ');
} }
if kernelversion || all { if kernel_version || all {
output.push_str(&uname.version()); output.push_str(&uname.version());
output.push(' '); output.push(' ');
} }
@ -108,7 +88,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
} }
if os || all { if os || all {
output.push_str(HOST_OS); output.push_str(&uname.osname());
output.push(' '); output.push(' ');
} }
@ -121,7 +101,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// This option is unsupported on modern Linux systems // This option is unsupported on modern Linux systems
// See: https://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00063.html // See: https://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00063.html
if hwplatform { if hardware_platform {
output.push_str("unknown"); output.push_str("unknown");
output.push(' '); output.push(' ');
} }
@ -141,13 +121,13 @@ pub fn uu_app() -> Command {
Arg::new(options::ALL) Arg::new(options::ALL)
.short('a') .short('a')
.long(options::ALL) .long(options::ALL)
.help("Behave as though all of the options -mnrsv were specified.") .help("Behave as though all of the options -mnrsvo were specified.")
.action(ArgAction::SetTrue), .action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::KERNELNAME) Arg::new(options::KERNEL_NAME)
.short('s') .short('s')
.long(options::KERNELNAME) .long(options::KERNEL_NAME)
.alias("sysname") // Obsolescent option in GNU uname .alias("sysname") // Obsolescent option in GNU uname
.help("print the kernel name.") .help("print the kernel name.")
.action(ArgAction::SetTrue), .action(ArgAction::SetTrue),
@ -163,17 +143,17 @@ pub fn uu_app() -> Command {
.action(ArgAction::SetTrue), .action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::KERNELRELEASE) Arg::new(options::KERNEL_RELEASE)
.short('r') .short('r')
.long(options::KERNELRELEASE) .long(options::KERNEL_RELEASE)
.alias("release") // Obsolescent option in GNU uname .alias("release") // Obsolescent option in GNU uname
.help("print the operating system release.") .help("print the operating system release.")
.action(ArgAction::SetTrue), .action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::KERNELVERSION) Arg::new(options::KERNEL_VERSION)
.short('v') .short('v')
.long(options::KERNELVERSION) .long(options::KERNEL_VERSION)
.help("print the operating system version.") .help("print the operating system version.")
.action(ArgAction::SetTrue), .action(ArgAction::SetTrue),
) )
@ -200,9 +180,9 @@ pub fn uu_app() -> Command {
.hide(true), .hide(true),
) )
.arg( .arg(
Arg::new(options::HWPLATFORM) Arg::new(options::HARDWARE_PLATFORM)
.short('i') .short('i')
.long(options::HWPLATFORM) .long(options::HARDWARE_PLATFORM)
.help("print the hardware platform (non-portable)") .help("print the hardware platform (non-portable)")
.action(ArgAction::SetTrue) .action(ArgAction::SetTrue)
.hide(true), .hide(true),

View file

@ -104,10 +104,11 @@ fn test_uname_operating_system() {
.succeeds() .succeeds()
.stdout_is("Redox\n"); .stdout_is("Redox\n");
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
new_ucmd!() {
.arg("--operating-system") let result = new_ucmd!().arg("--operating-system").succeeds();
.succeeds() println!("{:?}", result.stdout_str());
.stdout_is("Windows NT\n"); assert!(result.stdout_str().starts_with("MS/Windows"));
}
} }
#[test] #[test]
@ -117,3 +118,11 @@ fn test_uname_help() {
.succeeds() .succeeds()
.stdout_contains("system information"); .stdout_contains("system information");
} }
#[test]
fn test_uname_output_for_invisible_chars() {
// let re = regex::Regex::new("[^[[:print:]]]").unwrap(); // matches invisible (and emojis)
let re = regex::Regex::new("[^[[:print:]]\\p{Other_Symbol}]").unwrap(); // matches invisible (not emojis)
let result = new_ucmd!().arg("--all").succeeds();
assert_eq!(re.find(result.stdout_str().trim_end()), None);
}