|
7 | 7 |
|
8 | 8 | use clap::{Arg, ArgAction, ArgMatches, Command}; |
9 | 9 | use std::ffi::OsString; |
10 | | -use std::os::unix::process::ExitStatusExt; |
11 | 10 | use std::path::PathBuf; |
12 | 11 | use std::process; |
13 | 12 | use tempfile::TempDir; |
@@ -188,11 +187,17 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { |
188 | 187 | let options = |
189 | 188 | ProgramOptions::try_from(&matches).map_err(|e| UUsageError::new(125, e.to_string()))?; |
190 | 189 |
|
191 | | - let mut command_values = matches.get_many::<OsString>(options::COMMAND).unwrap(); |
192 | | - let mut command = process::Command::new(command_values.next().unwrap()); |
| 190 | + let mut command_values = matches |
| 191 | + .get_many::<OsString>(options::COMMAND) |
| 192 | + .ok_or_else(|| UUsageError::new(125, "no command specified".to_string()))?; |
| 193 | + let Some(first_command) = command_values.next() else { |
| 194 | + return Err(UUsageError::new(125, "no command specified".to_string())); |
| 195 | + }; |
| 196 | + let mut command = process::Command::new(first_command); |
193 | 197 | let command_params: Vec<&OsString> = command_values.collect(); |
194 | 198 |
|
195 | | - let tmp_dir = tempdir().unwrap(); |
| 199 | + let tmp_dir = tempdir() |
| 200 | + .map_err(|e| UUsageError::new(125, format!("failed to create temp directory: {e}")))?; |
196 | 201 | let (preload_env, libstdbuf) = get_preload_env(&tmp_dir)?; |
197 | 202 | command.env(preload_env, libstdbuf); |
198 | 203 | set_command_env(&mut command, "_STDBUF_I", &options.stdin); |
@@ -229,10 +234,27 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { |
229 | 234 | Err(i.into()) |
230 | 235 | } |
231 | 236 | } |
232 | | - None => Err(USimpleError::new( |
233 | | - 1, |
234 | | - translate!("stdbuf-error-killed-by-signal", "signal" => status.signal().unwrap()), |
235 | | - )), |
| 237 | + None => { |
| 238 | + #[cfg(unix)] |
| 239 | + { |
| 240 | + use std::os::unix::process::ExitStatusExt; |
| 241 | + let signal_msg = status |
| 242 | + .signal() |
| 243 | + .map(|s| s.to_string()) |
| 244 | + .unwrap_or_else(|| "unknown".to_string()); |
| 245 | + Err(USimpleError::new( |
| 246 | + 1, |
| 247 | + translate!("stdbuf-error-killed-by-signal", "signal" => signal_msg), |
| 248 | + )) |
| 249 | + } |
| 250 | + #[cfg(not(unix))] |
| 251 | + { |
| 252 | + Err(USimpleError::new( |
| 253 | + 1, |
| 254 | + "process terminated abnormally".to_string(), |
| 255 | + )) |
| 256 | + } |
| 257 | + } |
236 | 258 | } |
237 | 259 | } |
238 | 260 |
|
|
0 commit comments