Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bzip2.rs: remove final fprintf usage #34

Merged
merged 8 commits into from
Nov 12, 2024
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
146 changes: 62 additions & 84 deletions bzip2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
};

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;
static mut stdout: *mut FILE;
static mut stderr: *mut FILE;
}
type Bool = libc::c_uchar;

Expand Down Expand Up @@ -68,14 +67,16 @@
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 {
Expand Down Expand Up @@ -468,7 +469,7 @@
} 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(),
);
Expand Down Expand Up @@ -873,30 +874,28 @@
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) {
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,
1034 as libc::c_int - 10 as libc::c_int,
if strlen(from) > (FILE_NAME_LEN - 10) {
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(),
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);
Expand Down Expand Up @@ -1116,11 +1115,10 @@
}
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(),

Check warning on line 1121 in bzip2.rs

View check run for this annotation

Codecov / codecov/patch

bzip2.rs#L1118-L1121

Added lines #L1118 - L1121 were not covered by tests
);
}
setExit(1 as libc::c_int);
Expand Down Expand Up @@ -1155,11 +1153,10 @@
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;
Expand All @@ -1170,11 +1167,10 @@
&& 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);
Expand All @@ -1184,11 +1180,10 @@
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;
Expand All @@ -1198,17 +1193,12 @@
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;
Expand All @@ -1221,17 +1211,14 @@
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;
Expand All @@ -1244,17 +1231,14 @@
);
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);
Expand All @@ -1265,7 +1249,7 @@
if inStr.is_null() {
eprintln!(
"{}: Can't open input file {}: {}.",
std::env::args().next().unwrap(),
get_program_name().display(),

Check warning on line 1252 in bzip2.rs

View check run for this annotation

Codecov / codecov/patch

bzip2.rs#L1252

Added line #L1252 was not covered by tests
CStr::from_ptr(inName.as_ptr()).to_string_lossy(),
display_last_os_error(),
);
Expand Down Expand Up @@ -1311,13 +1295,8 @@
}
}
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);
}
outputHandleJustInCase = outStr;
delete_output_on_interrupt = true;
Expand All @@ -1335,6 +1314,7 @@
}
delete_output_on_interrupt = false;
}

unsafe fn uncompress(name: Option<String>) {
let inStr: *mut FILE;
let outStr: *mut FILE;
Expand Down Expand Up @@ -1571,7 +1551,6 @@
if verbosity >= 1 {
eprint!(" {}: ", in_name.display(),);
pad(inName.as_mut_ptr());
fflush(stderr);
}

/*--- Now the input and output handles are sane. Do the Biz. ---*/
Expand Down Expand Up @@ -1724,7 +1703,6 @@
if verbosity >= 1 {
eprint!(" {}: ", in_name.display());
pad(inName.as_mut_ptr());
fflush(stderr);
}
outputHandleJustInCase = std::ptr::null_mut::<FILE>();
let allOK = testStream(inStr);
Expand Down
1 change: 0 additions & 1 deletion tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
""
),),
Expand Down
Loading