Skip to content

Commit 4a8844d

Browse files
committed
coreutils: Avoid > /dev/full panic
1 parent e5bf248 commit 4a8844d

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

src/bin/coreutils.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,49 @@ use clap::Command;
77
use coreutils::validation;
88
use std::cmp;
99
use std::ffi::OsString;
10-
use std::io::{self, Write};
10+
use std::fmt::Write as _;
11+
use std::io::{self, Write, stdout};
1112
use std::process;
1213

1314
const VERSION: &str = env!("CARGO_PKG_VERSION");
1415

1516
include!(concat!(env!("OUT_DIR"), "/uutils_map.rs"));
1617

1718
fn usage<T>(utils: &UtilityMap<T>, name: &str) {
18-
println!("{name} {VERSION} (multi-call binary)\n");
19-
println!("Usage: {name} [function [arguments...]]");
20-
println!(" {name} --list");
21-
println!();
19+
let mut buffer = String::new();
20+
let _ = write!(
21+
buffer,
22+
r"{name} {VERSION} (multi-call binary)
23+
24+
Usage: {name} [function [arguments...]]
25+
{name} --list
26+
27+
"
28+
);
2229
#[cfg(feature = "feat_common_core")]
2330
{
24-
println!("Functions:");
25-
println!(" '<uutils>' [arguments...]");
26-
println!();
31+
let _ = writeln!(buffer, "Functions:");
32+
let _ = writeln!(buffer, " '<uutils>' [arguments...]\n");
2733
}
28-
println!("Options:");
29-
println!(" --list lists all defined functions, one per row\n");
30-
println!("Currently defined functions:\n");
34+
let _ = writeln!(
35+
buffer,
36+
r"Options:
37+
--list lists all defined functions, one per row
38+
39+
Currently defined functions:
40+
"
41+
);
3142
#[allow(clippy::map_clone)]
3243
let mut utils: Vec<&str> = utils.keys().map(|&s| s).collect();
3344
utils.sort_unstable();
3445
let display_list = utils.join(", ");
3546
let width = cmp::min(textwrap::termwidth(), 100) - 4 * 2; // (opinion/heuristic) max 100 chars wide with 4 character side indentions
36-
println!(
47+
let _ = writeln!(
48+
buffer,
3749
"{}",
3850
textwrap::indent(&textwrap::fill(&display_list, width), " ")
3951
);
52+
let _ = stdout().lock().write_all(buffer.as_bytes()); // todo: > /dev/full should be error
4053
}
4154

4255
#[allow(clippy::cognitive_complexity)]
@@ -76,12 +89,12 @@ fn main() {
7689
let mut utils: Vec<_> = utils.keys().collect();
7790
utils.sort();
7891
for util in utils {
79-
println!("{util}");
92+
let _ = writeln!(stdout(), "{util}"); //todo: --list >/dev/full should be error
8093
}
8194
process::exit(0);
8295
}
8396
"--version" | "-V" => {
84-
println!("{binary_as_util} {VERSION} (multi-call binary)");
97+
let _ = writeln!(stdout(), "{binary_as_util} {VERSION} (multi-call binary)"); //todo: --version >/dev/full should be error
8598
process::exit(0);
8699
}
87100
// Not a special command: fallthrough to calling a util

0 commit comments

Comments
 (0)