Skip to content

Commit 7c8123f

Browse files
xtqqczzecakebaker
authored andcommitted
nohup: use LazyLock for FAILURE_CODE
1 parent 3b75c47 commit 7c8123f

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/uu/nohup/src/nohup.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::os::unix::prelude::*;
1414
use std::os::unix::process::CommandExt;
1515
use std::path::{Path, PathBuf};
1616
use std::process;
17+
use std::sync::LazyLock;
1718
use thiserror::Error;
1819
use uucore::display::Quotable;
1920
use uucore::error::{UError, UResult, set_exit_code};
@@ -55,20 +56,20 @@ impl UError for NohupError {
5556
}
5657
}
5758

58-
fn failure_code() -> i32 {
59-
if env::var("POSIXLY_CORRECT").is_ok() {
59+
static FAILURE_CODE: LazyLock<i32> = LazyLock::new(|| {
60+
if env::var_os("POSIXLY_CORRECT").is_some() {
6061
POSIX_NOHUP_FAILURE
6162
} else {
6263
EXIT_CANCELED
6364
}
64-
}
65+
});
6566

6667
#[uucore::main]
6768
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
6869
let matches = uucore::clap_localization::handle_clap_result_with_exit_code(
6970
uu_app(),
7071
args,
71-
failure_code(),
72+
*FAILURE_CODE,
7273
)?;
7374

7475
replace_fds()?;
@@ -136,8 +137,6 @@ fn replace_fds() -> UResult<()> {
136137
}
137138

138139
fn find_stdout() -> UResult<File> {
139-
let internal_failure_code = failure_code();
140-
141140
match OpenOptions::new()
142141
.create(true)
143142
.append(true)
@@ -152,7 +151,7 @@ fn find_stdout() -> UResult<File> {
152151
}
153152
Err(e1) => {
154153
let Ok(home) = env::var("HOME") else {
155-
return Err(NohupError::OpenFailed(internal_failure_code, e1).into());
154+
return Err(NohupError::OpenFailed(*FAILURE_CODE, e1).into());
156155
};
157156
let mut homeout = PathBuf::from(home);
158157
homeout.push(NOHUP_OUT);
@@ -165,13 +164,12 @@ fn find_stdout() -> UResult<File> {
165164
);
166165
Ok(t)
167166
}
168-
Err(e2) => Err(NohupError::OpenFailed2(
169-
internal_failure_code,
170-
e1,
171-
homeout_str.to_string(),
172-
e2,
173-
)
174-
.into()),
167+
Err(e2) => {
168+
Err(
169+
NohupError::OpenFailed2(*FAILURE_CODE, e1, homeout_str.to_string(), e2)
170+
.into(),
171+
)
172+
}
175173
}
176174
}
177175
}

0 commit comments

Comments
 (0)