From c873a9d8f08007aeeedfc729cd7ae57979d37d5b Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Mon, 11 Nov 2024 22:08:21 +0100 Subject: [PATCH 1/8] `fn pad`: remove `fprintf` --- bzip2.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/bzip2.rs b/bzip2.rs index 1e59cd9b2..7f2a30ebb 100644 --- a/bzip2.rs +++ b/bzip2.rs @@ -873,10 +873,9 @@ unsafe fn pad(s: *mut c_char) { if strlen(s) as i32 >= longestFileName { return; } - let mut i = 1 as libc::c_int; - while i <= longestFileName - strlen(s) as i32 { - fprintf(stderr, b" \0" as *const u8 as *const libc::c_char); - i += 1; + + for _ in 1..=longestFileName - strlen(s) as i32 { + eprint!(" "); } } unsafe fn copyFileName(to: *mut c_char, from: *const c_char) { From c266d68af521460ee1fbc0da3c1d0805f94fd239 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Mon, 11 Nov 2024 22:10:10 +0100 Subject: [PATCH 2/8] `fn copyFileName`: remove `fprintf` --- bzip2.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bzip2.rs b/bzip2.rs index 7f2a30ebb..c2ea44dc5 100644 --- a/bzip2.rs +++ b/bzip2.rs @@ -880,11 +880,14 @@ unsafe fn pad(s: *mut c_char) { } unsafe fn copyFileName(to: *mut c_char, from: *const c_char) { if strlen(from) > (1034 as libc::c_int - 10 as libc::c_int) as libc::size_t { - fprintf( - stderr, - b"bzip2: file name\n`%s'\nis suspiciously (more than %d chars) long.\nTry using a reasonable file name instead. Sorry! :-)\n\0" - as *const u8 as *const libc::c_char, - from, + eprint!( + concat!( + "bzip2: file name\n", + "`{}'\n", + "is suspiciously (more than {} chars) long.\n", + "Try using a reasonable file name instead. Sorry! :-)\n", + ), + CStr::from_ptr(from).to_string_lossy(), 1034 as libc::c_int - 10 as libc::c_int, ); setExit(1 as libc::c_int); From 636565fa53afaaa18bba58a5154758b5cd64d5c1 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Mon, 11 Nov 2024 22:15:27 +0100 Subject: [PATCH 3/8] `fn compress`: remove `fprintf` --- bzip2.rs | 94 +++++++++++++++++++++++--------------------------------- 1 file changed, 38 insertions(+), 56 deletions(-) diff --git a/bzip2.rs b/bzip2.rs index c2ea44dc5..e66d51839 100644 --- a/bzip2.rs +++ b/bzip2.rs @@ -1118,11 +1118,10 @@ unsafe fn compress(name: *mut c_char) { } if srcMode != SourceMode::I2O && contains_dubious_chars(inName.as_mut_ptr()) { if noisy { - fprintf( - stderr, - b"%s: There are no files matching `%s'.\n\0" as *const u8 as *const libc::c_char, - progName, - inName.as_mut_ptr(), + eprintln!( + "{}: There are no files matching `{}'.", + get_program_name().display(), + CStr::from_ptr(inName.as_ptr()).to_string_lossy(), ); } setExit(1 as libc::c_int); @@ -1157,11 +1156,10 @@ unsafe fn compress(name: *mut c_char) { if srcMode == SourceMode::F2F || srcMode == SourceMode::F2O { stat(inName.as_mut_ptr(), &mut statBuf); if statBuf.st_mode & 0o170000 == 0o40000 { - fprintf( - stderr, - b"%s: Input file %s is a directory.\n\0" as *const u8 as *const libc::c_char, - progName, - inName.as_mut_ptr(), + eprintln!( + "{}: Input file {} is a directory.", + get_program_name().display(), + CStr::from_ptr(inName.as_ptr()).to_string_lossy(), ); setExit(1 as libc::c_int); return; @@ -1172,11 +1170,10 @@ unsafe fn compress(name: *mut c_char) { && notAStandardFile(inName.as_mut_ptr()) as libc::c_int != 0 { if noisy { - fprintf( - stderr, - b"%s: Input file %s is not a normal file.\n\0" as *const u8 as *const libc::c_char, - progName, - inName.as_mut_ptr(), + eprintln!( + "{}: Input file {} is not a normal file.", + get_program_name().display(), + CStr::from_ptr(inName.as_ptr()).to_string_lossy(), ); } setExit(1 as libc::c_int); @@ -1186,11 +1183,10 @@ unsafe fn compress(name: *mut c_char) { if force_overwrite { remove(outName.as_mut_ptr()); } else { - fprintf( - stderr, - b"%s: Output file %s already exists.\n\0" as *const u8 as *const libc::c_char, - progName, - outName.as_mut_ptr(), + eprintln!( + "{}: Output file {} already exists.", + get_program_name().display(), + CStr::from_ptr(outName.as_ptr()).to_string_lossy(), ); setExit(1 as libc::c_int); return; @@ -1200,17 +1196,12 @@ unsafe fn compress(name: *mut c_char) { n = countHardLinks(inName.as_mut_ptr()); n > 0 as libc::c_int } { - fprintf( - stderr, - b"%s: Input file %s has %d other link%s.\n\0" as *const u8 as *const libc::c_char, - progName, - inName.as_mut_ptr(), + eprintln!( + "{}: Input file {} has {} other link{}.", + get_program_name().display(), + CStr::from_ptr(inName.as_ptr()).to_string_lossy(), n, - if n > 1 as libc::c_int { - b"s\0" as *const u8 as *const libc::c_char - } else { - b"\0" as *const u8 as *const libc::c_char - }, + if n > 1 as libc::c_int { "s" } else { "" }, ); setExit(1 as libc::c_int); return; @@ -1223,17 +1214,14 @@ unsafe fn compress(name: *mut c_char) { inStr = stdin; outStr = stdout; if std::io::stdout().is_terminal() { - fprintf( - stderr, - b"%s: I won't write compressed data to a terminal.\n\0" as *const u8 - as *const libc::c_char, - progName, + eprintln!( + "{}: I won't write compressed data to a terminal.", + get_program_name().display(), ); - fprintf( - stderr, - b"%s: For help, type: `%s --help'.\n\0" as *const u8 as *const libc::c_char, - progName, - progName, + eprintln!( + "{}: For help, type: `{} --help'.", + get_program_name().display(), + get_program_name().display(), ); setExit(1 as libc::c_int); return; @@ -1246,17 +1234,14 @@ unsafe fn compress(name: *mut c_char) { ); outStr = stdout; if std::io::stdout().is_terminal() { - fprintf( - stderr, - b"%s: I won't write compressed data to a terminal.\n\0" as *const u8 - as *const libc::c_char, - progName, + eprintln!( + "{}: I won't write compressed data to a terminal.", + get_program_name().display(), ); - fprintf( - stderr, - b"%s: For help, type: `%s --help'.\n\0" as *const u8 as *const libc::c_char, - progName, - progName, + eprintln!( + "{}: For help, type: `{} --help'.", + get_program_name().display(), + get_program_name().display(), ); if !inStr.is_null() { fclose(inStr); @@ -1267,7 +1252,7 @@ unsafe fn compress(name: *mut c_char) { if inStr.is_null() { eprintln!( "{}: Can't open input file {}: {}.", - std::env::args().next().unwrap(), + get_program_name().display(), CStr::from_ptr(inName.as_ptr()).to_string_lossy(), display_last_os_error(), ); @@ -1313,11 +1298,7 @@ unsafe fn compress(name: *mut c_char) { } } if verbosity >= 1 as libc::c_int { - fprintf( - stderr, - b" %s: \0" as *const u8 as *const libc::c_char, - inName.as_mut_ptr(), - ); + eprint!(" {}: ", CStr::from_ptr(inName.as_ptr()).to_string_lossy(),); pad(inName.as_mut_ptr()); fflush(stderr); } @@ -1337,6 +1318,7 @@ unsafe fn compress(name: *mut c_char) { } delete_output_on_interrupt = false; } + unsafe fn uncompress(name: Option) { let inStr: *mut FILE; let outStr: *mut FILE; From 179facd3811d72f43bab8dd494f1d331554e1948 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Mon, 11 Nov 2024 22:15:48 +0100 Subject: [PATCH 4/8] `bzip2.rs`: remove `fprintf` import --- bzip2.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bzip2.rs b/bzip2.rs index e66d51839..6e3a556f4 100644 --- a/bzip2.rs +++ b/bzip2.rs @@ -14,9 +14,9 @@ use libbzip2_rs_sys::{ }; use libc::{ - _exit, close, exit, fclose, fdopen, ferror, fflush, fgetc, fileno, fopen, fprintf, fread, - fwrite, open, perror, remove, rewind, signal, stat, strcat, strcmp, strlen, strncpy, ungetc, - utimbuf, write, FILE, + _exit, close, exit, fclose, fdopen, ferror, fflush, fgetc, fileno, fopen, fread, fwrite, open, + perror, remove, rewind, signal, stat, strcat, strcmp, strlen, strncpy, ungetc, utimbuf, write, + FILE, }; extern "C" { static mut stdin: *mut FILE; From 021994dde689f4243d46372f93fd12930894ec9d Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Mon, 11 Nov 2024 22:19:23 +0100 Subject: [PATCH 5/8] `bzip2.rs`: use the `FILE_NAME_LEN` constant --- bzip2.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/bzip2.rs b/bzip2.rs index 6e3a556f4..06ec7d8b1 100644 --- a/bzip2.rs +++ b/bzip2.rs @@ -68,14 +68,16 @@ enum OperationMode { Test = 3, } +const FILE_NAME_LEN: usize = 1034; + static mut opMode: OperationMode = OperationMode::Zip; static mut srcMode: SourceMode = SourceMode::I2O; static mut longestFileName: i32 = 0; -static mut inName: [c_char; 1034] = [0; 1034]; -static mut outName: [c_char; 1034] = [0; 1034]; +static mut inName: [c_char; FILE_NAME_LEN] = [0; FILE_NAME_LEN]; +static mut outName: [c_char; FILE_NAME_LEN] = [0; FILE_NAME_LEN]; static mut progName: *mut c_char = ptr::null_mut(); -static mut progNameReally: [c_char; 1034] = [0; 1034]; +static mut progNameReally: [c_char; FILE_NAME_LEN] = [0; FILE_NAME_LEN]; // this should eventually be removed and just passed down into functions from the root fn get_program_name() -> PathBuf { @@ -879,7 +881,7 @@ unsafe fn pad(s: *mut c_char) { } } unsafe fn copyFileName(to: *mut c_char, from: *const c_char) { - if strlen(from) > (1034 as libc::c_int - 10 as libc::c_int) as libc::size_t { + if strlen(from) > (FILE_NAME_LEN - 10) { eprint!( concat!( "bzip2: file name\n", @@ -888,17 +890,13 @@ unsafe fn copyFileName(to: *mut c_char, from: *const c_char) { "Try using a reasonable file name instead. Sorry! :-)\n", ), CStr::from_ptr(from).to_string_lossy(), - 1034 as libc::c_int - 10 as libc::c_int, + FILE_NAME_LEN - 10 ); setExit(1 as libc::c_int); exit(exitValue); } - strncpy( - to, - from, - (1034 as libc::c_int - 10 as libc::c_int) as libc::size_t, - ); - *to.offset((1034 as libc::c_int - 10 as libc::c_int) as isize) = '\0' as i32 as c_char; + strncpy(to, from, FILE_NAME_LEN - 10); + *to.wrapping_add(FILE_NAME_LEN - 10) = '\0' as i32 as c_char; } unsafe fn fileExists(name: *mut c_char) -> Bool { let tmp: *mut FILE = fopen(name, b"rb\0" as *const u8 as *const libc::c_char); From 2ebaf07a60c005775a21a42566ecc3baa032d49f Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Mon, 11 Nov 2024 22:21:31 +0100 Subject: [PATCH 6/8] `bzip2.rs`: flush stderr using rust `std` --- bzip2.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bzip2.rs b/bzip2.rs index 06ec7d8b1..ca039ea4c 100644 --- a/bzip2.rs +++ b/bzip2.rs @@ -4,6 +4,7 @@ use std::ffi::{c_char, CStr, CString, OsStr}; use std::io::IsTerminal; +use std::io::Write; use std::mem::zeroed; use std::path::{Path, PathBuf}; use std::ptr; @@ -21,7 +22,6 @@ use libc::{ extern "C" { static mut stdin: *mut FILE; static mut stdout: *mut FILE; - static mut stderr: *mut FILE; } type Bool = libc::c_uchar; @@ -1298,7 +1298,7 @@ unsafe fn compress(name: *mut c_char) { if verbosity >= 1 as libc::c_int { eprint!(" {}: ", CStr::from_ptr(inName.as_ptr()).to_string_lossy(),); pad(inName.as_mut_ptr()); - fflush(stderr); + let _ = std::io::stderr().flush(); } outputHandleJustInCase = outStr; delete_output_on_interrupt = true; @@ -1553,7 +1553,7 @@ unsafe fn uncompress(name: Option) { if verbosity >= 1 { eprint!(" {}: ", in_name.display(),); pad(inName.as_mut_ptr()); - fflush(stderr); + let _ = std::io::stderr().flush(); } /*--- Now the input and output handles are sane. Do the Biz. ---*/ @@ -1706,7 +1706,7 @@ unsafe fn testf(name: Option) { if verbosity >= 1 { eprint!(" {}: ", in_name.display()); pad(inName.as_mut_ptr()); - fflush(stderr); + let _ = std::io::stderr().flush(); } outputHandleJustInCase = std::ptr::null_mut::(); let allOK = testStream(inStr); From a5763139779a9546598b99c513c1c37121c4f866 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Tue, 12 Nov 2024 11:29:02 +0100 Subject: [PATCH 7/8] `bzip2.rs`: fix extra newline in output --- bzip2.rs | 2 +- tests/quick.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/bzip2.rs b/bzip2.rs index ca039ea4c..accba91cf 100644 --- a/bzip2.rs +++ b/bzip2.rs @@ -470,7 +470,7 @@ unsafe fn uncompressStream(zStream: *mut FILE, stream: *mut FILE) -> bool { } else { if noisy { eprintln!( - "\n{}: {}: trailing garbage after EOF ignored\n", + "\n{}: {}: trailing garbage after EOF ignored", CStr::from_ptr(progName).to_string_lossy(), CStr::from_ptr(inName.as_ptr()).to_string_lossy(), ); diff --git a/tests/quick.rs b/tests/quick.rs index afbae0ed6..a701664dc 100644 --- a/tests/quick.rs +++ b/tests/quick.rs @@ -896,7 +896,6 @@ mod decompress_command { " [1: huff+mtf rt+rld {{0xccf1b5a5, 0xccf1b5a5}}]\n", " combined CRCs: stored = 0xccf1b5a5, computed = 0xccf1b5a5\n", "bzip2: (stdin): trailing garbage after EOF ignored\n", - "\n", "done\n", "" ),), From 62855e3fcc2a746c3ae4d501537ac9cd63f6834a Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Tue, 12 Nov 2024 11:42:58 +0100 Subject: [PATCH 8/8] `bzip2.rs`: remove flushing of stderr --- bzip2.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/bzip2.rs b/bzip2.rs index accba91cf..b33f6b5fe 100644 --- a/bzip2.rs +++ b/bzip2.rs @@ -4,7 +4,6 @@ use std::ffi::{c_char, CStr, CString, OsStr}; use std::io::IsTerminal; -use std::io::Write; use std::mem::zeroed; use std::path::{Path, PathBuf}; use std::ptr; @@ -1298,7 +1297,6 @@ unsafe fn compress(name: *mut c_char) { if verbosity >= 1 as libc::c_int { eprint!(" {}: ", CStr::from_ptr(inName.as_ptr()).to_string_lossy(),); pad(inName.as_mut_ptr()); - let _ = std::io::stderr().flush(); } outputHandleJustInCase = outStr; delete_output_on_interrupt = true; @@ -1553,7 +1551,6 @@ unsafe fn uncompress(name: Option) { if verbosity >= 1 { eprint!(" {}: ", in_name.display(),); pad(inName.as_mut_ptr()); - let _ = std::io::stderr().flush(); } /*--- Now the input and output handles are sane. Do the Biz. ---*/ @@ -1706,7 +1703,6 @@ unsafe fn testf(name: Option) { if verbosity >= 1 { eprint!(" {}: ", in_name.display()); pad(inName.as_mut_ptr()); - let _ = std::io::stderr().flush(); } outputHandleJustInCase = std::ptr::null_mut::(); let allOK = testStream(inStr);