1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

tail: Fix printed header for stdin should be the same on all platforms

This commit is contained in:
Joining7943 2023-04-20 21:12:24 +02:00
parent 08e8b40e45
commit ae60045f3f
2 changed files with 31 additions and 18 deletions

View file

@ -10,7 +10,10 @@ use std::ffi::OsStr;
use std::fs::{File, Metadata}; use std::fs::{File, Metadata};
use std::io::{Seek, SeekFrom}; use std::io::{Seek, SeekFrom};
#[cfg(unix)] #[cfg(unix)]
use std::os::unix::fs::{FileTypeExt, MetadataExt}; use std::os::unix::{
fs::{FileTypeExt, MetadataExt},
prelude::OsStrExt,
};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use uucore::error::UResult; use uucore::error::UResult;
@ -20,6 +23,30 @@ pub enum InputKind {
Stdin, Stdin,
} }
#[cfg(unix)]
impl From<&OsStr> for InputKind {
fn from(value: &OsStr) -> Self {
const DASH: [u8; 1] = [b'-'];
if value.as_bytes() == DASH {
Self::Stdin
} else {
Self::File(PathBuf::from(value))
}
}
}
#[cfg(not(unix))]
impl From<&OsStr> for InputKind {
fn from(value: &OsStr) -> Self {
if value == OsStr::new(text::DASH) {
Self::Stdin
} else {
Self::File(PathBuf::from(value))
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Input { pub struct Input {
kind: InputKind, kind: InputKind,
@ -29,21 +56,11 @@ pub struct Input {
impl Input { impl Input {
pub fn from<T: AsRef<OsStr>>(string: T) -> Self { pub fn from<T: AsRef<OsStr>>(string: T) -> Self {
let string = string.as_ref(); let string = string.as_ref();
let kind = if string == OsStr::new(text::DASH) {
InputKind::Stdin
} else {
InputKind::File(PathBuf::from(string))
};
let kind = string.into();
let display_name = match kind { let display_name = match kind {
InputKind::File(_) => string.to_string_lossy().to_string(), InputKind::File(_) => string.to_string_lossy().to_string(),
InputKind::Stdin => { InputKind::Stdin => text::STDIN_HEADER.to_string(),
if cfg!(unix) {
text::STDIN_HEADER.to_string()
} else {
string.to_string_lossy().to_string()
}
}
}; };
Self { kind, display_name } Self { kind, display_name }

View file

@ -145,12 +145,8 @@ fn test_stdin_redirect_offset() {
} }
#[test] #[test]
#[cfg(all(not(target_vendor = "apple"), not(target_os = "windows")))] // FIXME: for currently not working platforms #[cfg(all(not(target_vendor = "apple")))] // FIXME: for currently not working platforms
fn test_stdin_redirect_offset2() { fn test_stdin_redirect_offset2() {
// FIXME: windows: Failed because of difference in printed header. See below.
// actual : ==> - <==
// expected: ==> standard input <==
// like test_stdin_redirect_offset but with multiple files // like test_stdin_redirect_offset but with multiple files
let ts = TestScenario::new(util_name!()); let ts = TestScenario::new(util_name!());