Skip to content
Merged
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
29 changes: 15 additions & 14 deletions src/uu/stdbuf/src/stdbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
use std::fs::File;
use std::io::{self, Write};
use std::io::Write;
use std::os::unix::process::ExitStatusExt;
use std::path::PathBuf;
use std::process;
use tempfile::tempdir;
use tempfile::TempDir;
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::parse_size::parse_size_u64;
use uucore::{crash, format_usage, help_about, help_section, help_usage};
use uucore::{format_usage, help_about, help_section, help_usage};

const ABOUT: &str = help_about!("stdbuf.md");
const USAGE: &str = help_usage!("stdbuf.md");
Expand Down Expand Up @@ -66,13 +66,13 @@ struct ProgramOptionsError(String);
target_os = "netbsd",
target_os = "dragonflybsd"
))]
fn preload_strings() -> (&'static str, &'static str) {
("LD_PRELOAD", "so")
fn preload_strings() -> UResult<(&'static str, &'static str)> {
Ok(("LD_PRELOAD", "so"))
}

#[cfg(target_vendor = "apple")]
fn preload_strings() -> (&'static str, &'static str) {
("DYLD_LIBRARY_PATH", "dylib")
fn preload_strings() -> UResult<(&'static str, &'static str)> {
Ok(("DYLD_LIBRARY_PATH", "dylib"))
}

#[cfg(not(any(
Expand All @@ -83,10 +83,11 @@ fn preload_strings() -> (&'static str, &'static str) {
target_os = "dragonflybsd",
target_vendor = "apple"
)))]
fn preload_strings() -> (&'static str, &'static str) {
use uucore::crash;

crash!(1, "Command not supported for this operating system!")
fn preload_strings() -> UResult<(&'static str, &'static str)> {
Err(USimpleError::new(
1,
"Command not supported for this operating system!",
))
}

fn check_option(matches: &ArgMatches, name: &str) -> Result<BufferType, ProgramOptionsError> {
Expand All @@ -102,7 +103,7 @@ fn check_option(matches: &ArgMatches, name: &str) -> Result<BufferType, ProgramO
}
}
x => parse_size_u64(x).map_or_else(
|e| crash!(125, "invalid mode {}", e),
|e| Err(ProgramOptionsError(format!("invalid mode {e}"))),
|m| {
Ok(BufferType::Size(m.try_into().map_err(|_| {
ProgramOptionsError(format!(
Expand All @@ -128,8 +129,8 @@ fn set_command_env(command: &mut process::Command, buffer_name: &str, buffer_typ
}
}

fn get_preload_env(tmp_dir: &TempDir) -> io::Result<(String, PathBuf)> {
let (preload, extension) = preload_strings();
fn get_preload_env(tmp_dir: &TempDir) -> UResult<(String, PathBuf)> {
let (preload, extension) = preload_strings()?;
let inject_path = tmp_dir.path().join("libstdbuf").with_extension(extension);

let mut file = File::create(&inject_path)?;
Expand All @@ -151,7 +152,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let command_params: Vec<&str> = command_values.map(|s| s.as_ref()).collect();

let tmp_dir = tempdir().unwrap();
let (preload_env, libstdbuf) = get_preload_env(&tmp_dir).map_err_context(String::new)?;
let (preload_env, libstdbuf) = get_preload_env(&tmp_dir)?;
command.env(preload_env, libstdbuf);
set_command_env(&mut command, "_STDBUF_I", &options.stdin);
set_command_env(&mut command, "_STDBUF_O", &options.stdout);
Expand Down
2 changes: 1 addition & 1 deletion tests/by-util/test_stdbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn test_stdbuf_invalid_mode_fails() {
.args(&[*option, "1024R", "head"])
.fails()
.code_is(125)
.stderr_only("stdbuf: invalid mode '1024R': Value too large for defined data type\n");
.usage_error("invalid mode '1024R': Value too large for defined data type");
#[cfg(not(target_pointer_width = "128"))]
new_ucmd!()
.args(&[*option, "1Y", "head"])
Expand Down