Skip to content

Commit

Permalink
Merge pull request #5609 from cakebaker/ls_ignore_value_of_posixly_co…
Browse files Browse the repository at this point in the history
…rrect

ls: ignore value of `POSIXLY_CORRECT`
  • Loading branch information
sylvestre committed Dec 6, 2023
2 parents 6b0eff6 + 51fc2d7 commit 80b1ccd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore (ToDO) cpio svgz webm somegroup nlink rmvb xspf tabsize dired subdired dtype
// spell-checker:ignore (ToDO) somegroup nlink tabsize dired subdired dtype

use clap::{
builder::{NonEmptyStringValueParser, ValueParser},
Expand All @@ -20,7 +20,7 @@ use std::os::windows::fs::MetadataExt;
use std::{
cmp::Reverse,
error::Error,
ffi::{OsStr, OsString},
ffi::OsString,
fmt::{Display, Write as FmtWrite},
fs::{self, DirEntry, FileType, Metadata, ReadDir},
io::{stdout, BufWriter, ErrorKind, Stdout, Write},
Expand Down Expand Up @@ -741,24 +741,24 @@ impl Config {

let mut needs_color = extract_color(options);

let cmd_line_bs = options.get_one::<String>(options::size::BLOCK_SIZE);
let opt_si = cmd_line_bs.is_some()
let opt_block_size = options.get_one::<String>(options::size::BLOCK_SIZE);
let opt_si = opt_block_size.is_some()
&& options
.get_one::<String>(options::size::BLOCK_SIZE)
.unwrap()
.eq("si")
|| options.get_flag(options::size::SI);
let opt_hr = (cmd_line_bs.is_some()
let opt_hr = (opt_block_size.is_some()
&& options
.get_one::<String>(options::size::BLOCK_SIZE)
.unwrap()
.eq("human-readable"))
|| options.get_flag(options::size::HUMAN_READABLE);
let opt_kb = options.get_flag(options::size::KIBIBYTES);

let bs_env_var = std::env::var_os("BLOCK_SIZE");
let ls_bs_env_var = std::env::var_os("LS_BLOCK_SIZE");
let pc_env_var = std::env::var_os("POSIXLY_CORRECT");
let env_var_block_size = std::env::var_os("BLOCK_SIZE");
let env_var_ls_block_size = std::env::var_os("LS_BLOCK_SIZE");
let env_var_posixly_correct = std::env::var_os("POSIXLY_CORRECT");

let size_format = if opt_si {
SizeFormat::Decimal
Expand All @@ -768,34 +768,32 @@ impl Config {
SizeFormat::Bytes
};

let raw_bs = if let Some(cmd_line_bs) = cmd_line_bs {
OsString::from(cmd_line_bs)
let raw_block_size = if let Some(opt_block_size) = opt_block_size {
OsString::from(opt_block_size)
} else if !opt_kb {
if let Some(ls_bs_env_var) = ls_bs_env_var {
ls_bs_env_var
} else if let Some(bs_env_var) = bs_env_var {
bs_env_var
if let Some(env_var_ls_block_size) = env_var_ls_block_size {
env_var_ls_block_size
} else if let Some(env_var_block_size) = env_var_block_size {
env_var_block_size
} else {
OsString::from("")
}
} else {
OsString::from("")
};

let block_size: Option<u64> = if !opt_si && !opt_hr && !raw_bs.is_empty() {
match parse_size_u64(&raw_bs.to_string_lossy()) {
let block_size: Option<u64> = if !opt_si && !opt_hr && !raw_block_size.is_empty() {
match parse_size_u64(&raw_block_size.to_string_lossy()) {
Ok(size) => Some(size),
Err(_) => {
show!(LsError::BlockSizeParseError(cmd_line_bs.unwrap().clone()));
show!(LsError::BlockSizeParseError(
opt_block_size.unwrap().clone()
));
None
}
}
} else if let Some(pc) = pc_env_var {
if pc.as_os_str() == OsStr::new("true") || pc == OsStr::new("1") {
Some(POSIXLY_CORRECT_BLOCK_SIZE)
} else {
None
}
} else if env_var_posixly_correct.is_some() {
Some(POSIXLY_CORRECT_BLOCK_SIZE)
} else {
None
};
Expand Down
27 changes: 27 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3828,3 +3828,30 @@ fn test_ls_cf_output_should_be_delimited_by_tab() {
.succeeds()
.stdout_is("a2345/\tb/\n");
}

#[cfg(all(unix, feature = "dd"))]
#[test]
fn test_posixly_correct() {
let scene = TestScenario::new(util_name!());

scene
.ccmd("dd")
.arg("if=/dev/zero")
.arg("of=file")
.arg("bs=1024")
.arg("count=1")
.succeeds();

scene
.ucmd()
.arg("-s")
.succeeds()
.stdout_contains_line("total 4");

scene
.ucmd()
.arg("-s")
.env("POSIXLY_CORRECT", "some_value")
.succeeds()
.stdout_contains_line("total 8");
}

0 comments on commit 80b1ccd

Please sign in to comment.