Skip to content

Commit 067520d

Browse files
committed
introduce static mut IS_POSIXLY_CORRECT
1 parent b3469ae commit 067520d

15 files changed

Lines changed: 158 additions & 117 deletions

File tree

src/uu/cp/src/cp.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,14 @@ pub fn uu_app() -> Command {
809809
)
810810
}
811811

812+
static mut IS_POSIXLY_CORRECT: bool = false;
813+
812814
#[uucore::main]
813815
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
816+
unsafe {
817+
IS_POSIXLY_CORRECT = std::env::var_os("POSIXLY_CORRECT").is_some();
818+
}
819+
814820
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
815821

816822
let options = Options::from_matches(&matches)?;
@@ -2449,7 +2455,7 @@ fn copy_file(
24492455
OverwriteMode::Clobber(ClobberMode::RemoveDestination)
24502456
)
24512457
&& !is_symlink_loop(dest)
2452-
&& std::env::var_os("POSIXLY_CORRECT").is_none()
2458+
&& !unsafe { IS_POSIXLY_CORRECT }
24532459
{
24542460
return Err(CpError::Error(
24552461
translate!("cp-error-not-writing-dangling-symlink", "dest" => dest.quote()),

src/uu/df/src/blocks.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl BlockSize {
156156

157157
impl Default for BlockSize {
158158
fn default() -> Self {
159-
Self::Bytes(parse_block_size::default_block_size())
159+
Self::Bytes(super::default_block_size())
160160
}
161161
}
162162

@@ -199,9 +199,6 @@ impl fmt::Display for BlockSize {
199199

200200
#[cfg(test)]
201201
mod tests {
202-
203-
use std::env;
204-
205202
use crate::blocks::{BlockSize, SuffixType, to_magnitude_and_suffix};
206203

207204
#[test]
@@ -367,12 +364,4 @@ mod tests {
367364
assert_eq!(format!("{}", BlockSize::Bytes(1000 * 1024)), "1.1MB");
368365
assert_eq!(format!("{}", BlockSize::Bytes(1_000_000_000_000)), "1.0TB");
369366
}
370-
371-
#[test]
372-
fn test_default_block_size() {
373-
assert_eq!(BlockSize::Bytes(1024), BlockSize::default());
374-
unsafe { env::set_var("POSIXLY_CORRECT", "1") };
375-
assert_eq!(BlockSize::Bytes(512), BlockSize::default());
376-
unsafe { env::remove_var("POSIXLY_CORRECT") };
377-
}
378367
}

src/uu/df/src/df.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,25 @@ impl UError for DfError {
442442
}
443443
}
444444

445+
/// Default block size when no env var or flag is set.
446+
///
447+
/// Returns 512 if `POSIXLY_CORRECT` is set, 1024 otherwise.
448+
pub(crate) fn default_block_size() -> u64 {
449+
if unsafe { IS_POSIXLY_CORRECT } {
450+
512
451+
} else {
452+
1024
453+
}
454+
}
455+
456+
static mut IS_POSIXLY_CORRECT: bool = false;
457+
445458
#[uucore::main]
446459
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
460+
unsafe {
461+
IS_POSIXLY_CORRECT = std::env::var_os("POSIXLY_CORRECT").is_some();
462+
}
463+
447464
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
448465

449466
#[cfg(windows)]

src/uu/du/src/du.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,18 @@ fn read_block_size(s: Option<&str>) -> UResult<u64> {
284284
{
285285
Ok(bytes)
286286
} else {
287-
Ok(parse_block_size::default_block_size())
287+
Ok(default_block_size())
288+
}
289+
}
290+
291+
/// Default block size when no env var or flag is set.
292+
///
293+
/// Returns 512 if `POSIXLY_CORRECT` is set, 1024 otherwise.
294+
fn default_block_size() -> u64 {
295+
if unsafe { IS_POSIXLY_CORRECT } {
296+
512
297+
} else {
298+
1024
288299
}
289300
}
290301

@@ -1004,9 +1015,15 @@ fn parse_size_format(matches: &ArgMatches) -> UResult<SizeFormat> {
10041015
.unwrap_or(block_size_value_or_default_fallback))
10051016
}
10061017

1018+
static mut IS_POSIXLY_CORRECT: bool = false;
1019+
10071020
#[uucore::main]
10081021
#[allow(clippy::cognitive_complexity)]
10091022
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
1023+
unsafe {
1024+
IS_POSIXLY_CORRECT = env::var_os("POSIXLY_CORRECT").is_some();
1025+
}
1026+
10101027
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
10111028

10121029
let summarize = matches.get_flag(options::SUMMARIZE);

src/uu/echo/src/echo.rs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,14 @@ fn filter_flags(args: impl Iterator<Item = OsString>) -> (impl Iterator<Item = O
119119
(args, options)
120120
}
121121

122+
static mut IS_POSIXLY_CORRECT: bool = false;
123+
122124
#[uucore::main]
123125
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
126+
unsafe {
127+
IS_POSIXLY_CORRECT = env::var_os("POSIXLY_CORRECT").is_some();
128+
}
129+
124130
// args[0] is the name of the binary.
125131
let mut args = args.skip(1).peekable();
126132

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

140-
let (args, options): (Box<dyn Iterator<Item = OsString>>, Options) = if is_posixly_correct {
141-
if args.peek().is_some_and(|arg| arg == "-n") {
142-
// if POSIXLY_CORRECT is set and the first argument is the "-n" flag
143-
// we filter flags normally but 'escaped' is activated nonetheless.
144-
let (args, _) = filter_flags(args);
145-
(
146-
Box::new(args),
147-
Options {
148-
trailing_newline: false,
149-
..Options::posixly_correct_default()
150-
},
151-
)
152-
} else {
153-
// if POSIXLY_CORRECT is set and the first argument is not the "-n" flag
154-
// we just collect all arguments as no arguments are interpreted as flags.
155-
(Box::new(args), Options::posixly_correct_default())
156-
}
157-
} else if let Some(first_arg) = args.next() {
158-
if first_arg == "--help" && args.peek().is_none() {
159-
// If POSIXLY_CORRECT is not set and the first argument
160-
// is `--help`, GNU coreutils prints the help message.
161-
//
162-
// Verify this using:
163-
//
164-
// POSIXLY_CORRECT=1 echo --help
165-
// echo --help
166-
uu_app().print_help()?;
167-
return Ok(());
168-
} else if first_arg == "--version" && args.peek().is_none() {
169-
writeln!(stdout(), "echo {}", crate_version!())?;
170-
return Ok(());
171-
}
145+
let (args, options): (Box<dyn Iterator<Item = OsString>>, Options) =
146+
if unsafe { IS_POSIXLY_CORRECT } {
147+
if args.peek().is_some_and(|arg| arg == "-n") {
148+
// if POSIXLY_CORRECT is set and the first argument is the "-n" flag
149+
// we filter flags normally but 'escaped' is activated nonetheless.
150+
let (args, _) = filter_flags(args);
151+
(
152+
Box::new(args),
153+
Options {
154+
trailing_newline: false,
155+
..Options::posixly_correct_default()
156+
},
157+
)
158+
} else {
159+
// if POSIXLY_CORRECT is set and the first argument is not the "-n" flag
160+
// we just collect all arguments as no arguments are interpreted as flags.
161+
(Box::new(args), Options::posixly_correct_default())
162+
}
163+
} else if let Some(first_arg) = args.next() {
164+
if first_arg == "--help" && args.peek().is_none() {
165+
// If POSIXLY_CORRECT is not set and the first argument
166+
// is `--help`, GNU coreutils prints the help message.
167+
//
168+
// Verify this using:
169+
//
170+
// POSIXLY_CORRECT=1 echo --help
171+
// echo --help
172+
uu_app().print_help()?;
173+
return Ok(());
174+
} else if first_arg == "--version" && args.peek().is_none() {
175+
writeln!(stdout(), "echo {}", crate_version!())?;
176+
return Ok(());
177+
}
172178

173-
// if POSIXLY_CORRECT is not set we filter the flags normally
174-
let (args, options) = filter_flags(std::iter::once(first_arg).chain(args));
175-
(Box::new(args), options)
176-
} else {
177-
(Box::new(args), Options::default())
178-
};
179+
// if POSIXLY_CORRECT is not set we filter the flags normally
180+
let (args, options) = filter_flags(std::iter::once(first_arg).chain(args));
181+
(Box::new(args), options)
182+
} else {
183+
(Box::new(args), Options::default())
184+
};
179185

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

src/uu/id/src/id.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,15 @@ struct State {
121121
user_specified: bool,
122122
}
123123

124+
static mut IS_POSIXLY_CORRECT: bool = false;
125+
124126
#[uucore::main(no_signals)]
125127
#[allow(clippy::cognitive_complexity)]
126128
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
129+
unsafe {
130+
IS_POSIXLY_CORRECT = std::env::var_os("POSIXLY_CORRECT").is_some();
131+
}
132+
127133
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
128134
let mut lock = io::stdout().lock();
129135

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

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

720723
#[cfg(feature = "smack")]
721-
if state.smack_supported
722-
&& !state.user_specified
723-
&& std::env::var_os("POSIXLY_CORRECT").is_none()
724-
{
724+
if state.smack_supported && !state.user_specified && !unsafe { IS_POSIXLY_CORRECT } {
725725
// print SMACK label (does not depend on "-Z")
726726
if let Ok(label) = uucore::smack::get_smack_label_for_self() {
727727
write!(lock, " context={label}")?;

src/uu/ls/src/config.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ pub mod options {
120120
}
121121

122122
const DEFAULT_TERM_WIDTH: u16 = 80;
123-
const POSIXLY_CORRECT_BLOCK_SIZE: u64 = 512;
124123
const DEFAULT_BLOCK_SIZE: u64 = 1024;
125124
const DEFAULT_FILE_SIZE_BLOCK_SIZE: u64 = 1;
126125

@@ -156,10 +155,10 @@ fn resolve_block_sizes_from_env(opt_kb: bool) -> (u64, u64) {
156155
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
157156
}
158157
parse_block_size::BlockSizeEnv::NotSet => {
159-
if std::env::var_os("POSIXLY_CORRECT").is_some() && !opt_kb {
160-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, POSIXLY_CORRECT_BLOCK_SIZE)
158+
if opt_kb {
159+
(DEFAULT_FILE_SIZE_BLOCK_SIZE, 1024)
161160
} else {
162-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
161+
(DEFAULT_FILE_SIZE_BLOCK_SIZE, crate::default_block_size())
163162
}
164163
}
165164
}

src/uu/ls/src/ls.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,25 @@ impl UError for LsError {
115115
}
116116
}
117117

118+
/// Default block size when no env var or flag is set.
119+
///
120+
/// Returns 512 if `POSIXLY_CORRECT` is set, 1024 otherwise.
121+
pub(crate) fn default_block_size() -> u64 {
122+
if unsafe { IS_POSIXLY_CORRECT } {
123+
512
124+
} else {
125+
1024
126+
}
127+
}
128+
129+
static mut IS_POSIXLY_CORRECT: bool = false;
130+
118131
#[uucore::main]
119132
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
133+
unsafe {
134+
IS_POSIXLY_CORRECT = std::env::var_os("POSIXLY_CORRECT").is_some();
135+
}
136+
120137
let matches = uucore::clap_localization::handle_clap_result_with_exit_code(uu_app(), args, 2)?;
121138

122139
let config = Config::from(&matches)?;

src/uu/mktemp/src/mktemp.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,14 @@ impl ValueParserFactory for OptionalPathBufParser {
366366
}
367367
}
368368

369+
static mut IS_POSIXLY_CORRECT: bool = false;
370+
369371
#[uucore::main]
370372
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
373+
unsafe {
374+
IS_POSIXLY_CORRECT = env::var_os("POSIXLY_CORRECT").is_some();
375+
}
376+
371377
let args: Vec<_> = args.collect();
372378
let matches = match uu_app().try_get_matches_from(&args) {
373379
Ok(m) => m,
@@ -395,7 +401,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
395401
// application logic.
396402
let options = Options::from(&matches);
397403

398-
if env::var_os("POSIXLY_CORRECT").is_some() {
404+
if unsafe { IS_POSIXLY_CORRECT } {
399405
// If POSIXLY_CORRECT was set, template MUST be the last argument.
400406
if matches.contains_id(ARG_TEMPLATE) {
401407
// Template argument was provided, check if was the last one.

src/uu/nohup/src/nohup.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,21 @@ impl UError for NohupError {
5959
}
6060

6161
static FAILURE_CODE: LazyLock<i32> = LazyLock::new(|| {
62-
if env::var_os("POSIXLY_CORRECT").is_some() {
62+
if unsafe { IS_POSIXLY_CORRECT } {
6363
POSIX_NOHUP_FAILURE
6464
} else {
6565
EXIT_CANCELED
6666
}
6767
});
6868

69+
static mut IS_POSIXLY_CORRECT: bool = false;
70+
6971
#[uucore::main]
7072
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
73+
unsafe {
74+
IS_POSIXLY_CORRECT = env::var_os("POSIXLY_CORRECT").is_some();
75+
}
76+
7177
let matches = uucore::clap_localization::handle_clap_result_with_exit_code(
7278
uu_app(),
7379
args,

0 commit comments

Comments
 (0)