Fix csplit to handle non-UTF-8 filenames

This commit is contained in:
Sylvestre Ledru
2025-08-14 10:52:24 +02:00
parent 39b7721464
commit 20793600f7
2 changed files with 16 additions and 2 deletions
+4 -2
View File
@@ -6,6 +6,7 @@
#![allow(rustdoc::private_intra_doc_links)]
use std::cmp::Ordering;
use std::ffi::OsString;
use std::io::{self, BufReader, ErrorKind};
use std::{
fs::{File, remove_file},
@@ -608,7 +609,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().get_matches_from_localized(args);
// get the file to split
let file_name = matches.get_one::<String>(options::FILE).unwrap();
let file_name = matches.get_one::<OsString>(options::FILE).unwrap();
// get the patterns to split on
let patterns: Vec<String> = matches
@@ -689,7 +690,8 @@ pub fn uu_app() -> Command {
Arg::new(options::FILE)
.hide(true)
.required(true)
.value_hint(clap::ValueHint::FilePath),
.value_hint(clap::ValueHint::FilePath)
.value_parser(clap::value_parser!(OsString)),
)
.arg(
Arg::new(options::PATTERN)
+12
View File
@@ -1501,3 +1501,15 @@ fn test_stdin_no_trailing_newline() {
.succeeds()
.stdout_only("2\n5\n");
}
#[test]
#[cfg(target_os = "linux")]
fn test_csplit_non_utf8_paths() {
use std::os::unix::ffi::OsStringExt;
let (at, mut ucmd) = at_and_ucmd!();
let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]);
std::fs::write(at.plus(&filename), b"line1\nline2\nline3\nline4\nline5\n").unwrap();
ucmd.arg(&filename).arg("3").succeeds();
}