Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,14 @@ pub fn uu_app() -> Command {
)
}

static mut IS_POSIXLY_CORRECT: bool = false;

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
unsafe {
IS_POSIXLY_CORRECT = std::env::var_os("POSIXLY_CORRECT").is_some();
}

let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;

let options = Options::from_matches(&matches)?;
Expand Down Expand Up @@ -2449,7 +2455,7 @@ fn copy_file(
OverwriteMode::Clobber(ClobberMode::RemoveDestination)
)
&& !is_symlink_loop(dest)
&& std::env::var_os("POSIXLY_CORRECT").is_none()
&& !unsafe { IS_POSIXLY_CORRECT }
{
return Err(CpError::Error(
translate!("cp-error-not-writing-dangling-symlink", "dest" => dest.quote()),
Expand Down
13 changes: 1 addition & 12 deletions src/uu/df/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl BlockSize {

impl Default for BlockSize {
fn default() -> Self {
Self::Bytes(parse_block_size::default_block_size())
Self::Bytes(super::default_block_size())
}
}

Expand Down Expand Up @@ -199,9 +199,6 @@ impl fmt::Display for BlockSize {

#[cfg(test)]
mod tests {

use std::env;

use crate::blocks::{BlockSize, SuffixType, to_magnitude_and_suffix};

#[test]
Expand Down Expand Up @@ -367,12 +364,4 @@ mod tests {
assert_eq!(format!("{}", BlockSize::Bytes(1000 * 1024)), "1.1MB");
assert_eq!(format!("{}", BlockSize::Bytes(1_000_000_000_000)), "1.0TB");
}

#[test]
fn test_default_block_size() {
assert_eq!(BlockSize::Bytes(1024), BlockSize::default());
unsafe { env::set_var("POSIXLY_CORRECT", "1") };
assert_eq!(BlockSize::Bytes(512), BlockSize::default());
unsafe { env::remove_var("POSIXLY_CORRECT") };
}
}
17 changes: 17 additions & 0 deletions src/uu/df/src/df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,25 @@ impl UError for DfError {
}
}

/// Default block size when no env var or flag is set.
///
/// Returns 512 if `POSIXLY_CORRECT` is set, 1024 otherwise.
pub(crate) fn default_block_size() -> u64 {
if unsafe { IS_POSIXLY_CORRECT } {
512
} else {
1024
}
}

static mut IS_POSIXLY_CORRECT: bool = false;

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
unsafe {
IS_POSIXLY_CORRECT = std::env::var_os("POSIXLY_CORRECT").is_some();
}

let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;

#[cfg(windows)]
Expand Down
19 changes: 18 additions & 1 deletion src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,18 @@ fn read_block_size(s: Option<&str>) -> UResult<u64> {
{
Ok(bytes)
} else {
Ok(parse_block_size::default_block_size())
Ok(default_block_size())
}
}

/// Default block size when no env var or flag is set.
///
/// Returns 512 if `POSIXLY_CORRECT` is set, 1024 otherwise.
fn default_block_size() -> u64 {
if unsafe { IS_POSIXLY_CORRECT } {
512
} else {
1024
}
}

Expand Down Expand Up @@ -1002,9 +1013,15 @@ fn parse_size_format(matches: &ArgMatches) -> UResult<SizeFormat> {
.unwrap_or(block_size_value_or_default_fallback))
}

static mut IS_POSIXLY_CORRECT: bool = false;

#[uucore::main]
#[allow(clippy::cognitive_complexity)]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
unsafe {
IS_POSIXLY_CORRECT = env::var_os("POSIXLY_CORRECT").is_some();
}

let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;

let summarize = matches.get_flag(options::SUMMARIZE);
Expand Down
84 changes: 45 additions & 39 deletions src/uu/echo/src/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ fn filter_flags(args: impl Iterator<Item = OsString>) -> (impl Iterator<Item = O
(args, options)
}

static mut IS_POSIXLY_CORRECT: bool = false;

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
unsafe {
IS_POSIXLY_CORRECT = env::var_os("POSIXLY_CORRECT").is_some();
}

// args[0] is the name of the binary.
let mut args = args.skip(1).peekable();

Expand All @@ -135,47 +141,47 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// > escapes are always enabled. To echo the string ‘-n’, one of the
// > characters can be escaped in either octal or hexadecimal
// > representation. For example, echo -e '\x2dn'.
let is_posixly_correct = env::var_os("POSIXLY_CORRECT").is_some();

let (args, options): (Box<dyn Iterator<Item = OsString>>, Options) = if is_posixly_correct {
if args.peek().is_some_and(|arg| arg == "-n") {
// if POSIXLY_CORRECT is set and the first argument is the "-n" flag
// we filter flags normally but 'escaped' is activated nonetheless.
let (args, _) = filter_flags(args);
(
Box::new(args),
Options {
trailing_newline: false,
..Options::posixly_correct_default()
},
)
} else {
// if POSIXLY_CORRECT is set and the first argument is not the "-n" flag
// we just collect all arguments as no arguments are interpreted as flags.
(Box::new(args), Options::posixly_correct_default())
}
} else if let Some(first_arg) = args.next() {
if first_arg == "--help" && args.peek().is_none() {
// If POSIXLY_CORRECT is not set and the first argument
// is `--help`, GNU coreutils prints the help message.
//
// Verify this using:
//
// POSIXLY_CORRECT=1 echo --help
// echo --help
uu_app().print_help()?;
return Ok(());
} else if first_arg == "--version" && args.peek().is_none() {
writeln!(stdout(), "echo {}", crate_version!())?;
return Ok(());
}
let (args, options): (Box<dyn Iterator<Item = OsString>>, Options) =
if unsafe { IS_POSIXLY_CORRECT } {
if args.peek().is_some_and(|arg| arg == "-n") {
// if POSIXLY_CORRECT is set and the first argument is the "-n" flag
// we filter flags normally but 'escaped' is activated nonetheless.
let (args, _) = filter_flags(args);
(
Box::new(args),
Options {
trailing_newline: false,
..Options::posixly_correct_default()
},
)
} else {
// if POSIXLY_CORRECT is set and the first argument is not the "-n" flag
// we just collect all arguments as no arguments are interpreted as flags.
(Box::new(args), Options::posixly_correct_default())
}
} else if let Some(first_arg) = args.next() {
if first_arg == "--help" && args.peek().is_none() {
// If POSIXLY_CORRECT is not set and the first argument
// is `--help`, GNU coreutils prints the help message.
//
// Verify this using:
//
// POSIXLY_CORRECT=1 echo --help
// echo --help
uu_app().print_help()?;
return Ok(());
} else if first_arg == "--version" && args.peek().is_none() {
writeln!(stdout(), "echo {}", crate_version!())?;
return Ok(());
}

// if POSIXLY_CORRECT is not set we filter the flags normally
let (args, options) = filter_flags(std::iter::once(first_arg).chain(args));
(Box::new(args), options)
} else {
(Box::new(args), Options::default())
};
// if POSIXLY_CORRECT is not set we filter the flags normally
let (args, options) = filter_flags(std::iter::once(first_arg).chain(args));
(Box::new(args), options)
} else {
(Box::new(args), Options::default())
};

execute(&mut stdout().lock(), args, options)?;

Expand Down
16 changes: 8 additions & 8 deletions src/uu/id/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,15 @@ struct State {
user_specified: bool,
}

static mut IS_POSIXLY_CORRECT: bool = false;

#[uucore::main(no_signals)]
#[allow(clippy::cognitive_complexity)]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
unsafe {
IS_POSIXLY_CORRECT = std::env::var_os("POSIXLY_CORRECT").is_some();
}

let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
let mut lock = io::stdout().lock();

Expand Down Expand Up @@ -706,10 +712,7 @@ fn id_print(state: &State, groups: &[u32]) -> io::Result<()> {
)?;

#[cfg(feature = "selinux")]
if state.selinux_supported
&& !state.user_specified
&& std::env::var_os("POSIXLY_CORRECT").is_none()
{
if state.selinux_supported && !state.user_specified && !unsafe { IS_POSIXLY_CORRECT } {
// print SElinux context (does not depend on "-Z")
if let Ok(context) = selinux::SecurityContext::current(false) {
let bytes = context.as_bytes();
Expand All @@ -718,10 +721,7 @@ fn id_print(state: &State, groups: &[u32]) -> io::Result<()> {
}

#[cfg(feature = "smack")]
if state.smack_supported
&& !state.user_specified
&& std::env::var_os("POSIXLY_CORRECT").is_none()
{
if state.smack_supported && !state.user_specified && !unsafe { IS_POSIXLY_CORRECT } {
// print SMACK label (does not depend on "-Z")
if let Ok(label) = uucore::smack::get_smack_label_for_self() {
write!(lock, " context={label}")?;
Expand Down
7 changes: 3 additions & 4 deletions src/uu/ls/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ pub mod options {
}

const DEFAULT_TERM_WIDTH: u16 = 80;
const POSIXLY_CORRECT_BLOCK_SIZE: u64 = 512;
const DEFAULT_BLOCK_SIZE: u64 = 1024;
const DEFAULT_FILE_SIZE_BLOCK_SIZE: u64 = 1;

Expand Down Expand Up @@ -156,10 +155,10 @@ fn resolve_block_sizes_from_env(opt_kb: bool) -> (u64, u64) {
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
}
parse_block_size::BlockSizeEnv::NotSet => {
if std::env::var_os("POSIXLY_CORRECT").is_some() && !opt_kb {
(DEFAULT_FILE_SIZE_BLOCK_SIZE, POSIXLY_CORRECT_BLOCK_SIZE)
if opt_kb {
(DEFAULT_FILE_SIZE_BLOCK_SIZE, 1024)
} else {
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
(DEFAULT_FILE_SIZE_BLOCK_SIZE, crate::default_block_size())
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,25 @@ impl UError for LsError {
}
}

/// Default block size when no env var or flag is set.
///
/// Returns 512 if `POSIXLY_CORRECT` is set, 1024 otherwise.
pub(crate) fn default_block_size() -> u64 {
if unsafe { IS_POSIXLY_CORRECT } {
512
} else {
1024
}
}

static mut IS_POSIXLY_CORRECT: bool = false;

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
unsafe {
IS_POSIXLY_CORRECT = std::env::var_os("POSIXLY_CORRECT").is_some();
}

let matches = uucore::clap_localization::handle_clap_result_with_exit_code(uu_app(), args, 2)?;

let config = Config::from(&matches)?;
Expand Down
8 changes: 7 additions & 1 deletion src/uu/mktemp/src/mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,14 @@ impl ValueParserFactory for OptionalPathBufParser {
}
}

static mut IS_POSIXLY_CORRECT: bool = false;

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
unsafe {
IS_POSIXLY_CORRECT = env::var_os("POSIXLY_CORRECT").is_some();
}

let args: Vec<_> = args.collect();
let matches = match uu_app().try_get_matches_from(&args) {
Ok(m) => m,
Expand Down Expand Up @@ -395,7 +401,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// application logic.
let options = Options::from(&matches);

if env::var_os("POSIXLY_CORRECT").is_some() {
if unsafe { IS_POSIXLY_CORRECT } {
// If POSIXLY_CORRECT was set, template MUST be the last argument.
if matches.contains_id(ARG_TEMPLATE) {
// Template argument was provided, check if was the last one.
Expand Down
8 changes: 7 additions & 1 deletion src/uu/nohup/src/nohup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,21 @@ impl UError for NohupError {
}

static FAILURE_CODE: LazyLock<i32> = LazyLock::new(|| {
if env::var_os("POSIXLY_CORRECT").is_some() {
if unsafe { IS_POSIXLY_CORRECT } {
POSIX_NOHUP_FAILURE
} else {
EXIT_CANCELED
}
});

static mut IS_POSIXLY_CORRECT: bool = false;

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
unsafe {
IS_POSIXLY_CORRECT = env::var_os("POSIXLY_CORRECT").is_some();
}

let matches = uucore::clap_localization::handle_clap_result_with_exit_code(
uu_app(),
args,
Expand Down
8 changes: 7 additions & 1 deletion src/uu/pr/src/pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,14 @@ pub fn uu_app() -> Command {
)
}

static mut IS_POSIXLY_CORRECT: bool = false;

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
unsafe {
IS_POSIXLY_CORRECT = std::env::var_os("POSIXLY_CORRECT").is_some();
}

let args = args.collect_ignore();

let opt_args = recreate_arguments(&args);
Expand Down Expand Up @@ -489,7 +495,7 @@ fn get_date_format(matches: &ArgMatches) -> String {
Some(format) => format,
None => {
// Replicate behavior from GNU manual.
if std::env::var("POSIXLY_CORRECT").is_ok()
if unsafe { IS_POSIXLY_CORRECT }
// TODO: This needs to be moved to uucore and handled by icu?
&& (std::env::var_os("LC_TIME").as_deref() == Some(OsStr::new("POSIX"))
|| std::env::var_os("LC_ALL").as_deref() == Some(OsStr::new("POSIX")))
Expand Down
Loading
Loading