Skip to content

Commit

Permalink
Fix for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Nov 5, 2024
1 parent 9d4f1ce commit c68f560
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 92 deletions.
147 changes: 64 additions & 83 deletions bzip2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(unused_assignments)]
#![allow(unused_mut)]

use std::ffi::CStr;
use std::mem::zeroed;

use libbzip2_rs_sys::bzlib::{
Expand All @@ -12,10 +13,10 @@ use libbzip2_rs_sys::bzlib::{
};

use libc::{
__errno_location, _exit, close, exit, fchmod, fchown, fclose, fdopen, ferror, fflush, fgetc,
fileno, fopen, fprintf, fread, free, fwrite, getenv, isatty, lstat, malloc, open, perror,
remove, rewind, signal, size_t, stat, strcat, strcmp, strcpy, strerror, strlen, strncmp,
strncpy, strstr, ungetc, utimbuf, utime, write, FILE,
_exit, close, exit, fchmod, fchown, fclose, fdopen, ferror, fflush, fgetc, fileno, fopen,
fprintf, fread, free, fwrite, getenv, isatty, lstat, malloc, open, perror, remove, rewind,
signal, size_t, stat, strcat, strcmp, strcpy, strlen, strncmp, strncpy, strstr, ungetc,
utimbuf, utime, write, FILE,
};
extern "C" {
static mut stdin: *mut FILE;
Expand Down Expand Up @@ -1541,9 +1542,7 @@ unsafe fn notAStandardFile(mut name: *mut i8) -> Bool {
if i != 0 as libc::c_int {
return 1 as Bool;
}
if statBuf.st_mode & 0o170000 as libc::c_int as libc::c_uint
== 0o100000 as libc::c_int as libc::c_uint
{
if statBuf.st_mode & 0o170000 == 0o100000 {
return 0 as Bool;
}
1 as Bool
Expand Down Expand Up @@ -1672,12 +1671,11 @@ unsafe fn compress(mut name: *mut i8) {
return;
}
if srcMode != 1 as libc::c_int && fileExists(inName.as_mut_ptr()) == 0 {
fprintf(
stderr,
b"%s: Can't open input file %s: %s.\n\0" as *const u8 as *const libc::c_char,
progName,
inName.as_mut_ptr(),
strerror(*__errno_location()),
eprintln!(
"{}: Can't open input file {}: {}.",
std::env::args().next().unwrap(),
CStr::from_ptr(inName.as_ptr()).to_string_lossy(),
std::io::Error::last_os_error(),
);
setExit(1 as libc::c_int);
return;
Expand All @@ -1686,13 +1684,11 @@ unsafe fn compress(mut name: *mut i8) {
while i < 4 as libc::c_int {
if hasSuffix(inName.as_mut_ptr(), zSuffix[i as usize]) != 0 {
if noisy != 0 {
fprintf(
stderr,
b"%s: Input file %s already has %s suffix.\n\0" as *const u8
as *const libc::c_char,
progName,
inName.as_mut_ptr(),
zSuffix[i as usize],
eprintln!(
"{}: Input file {} already has {} suffix.",
std::env::args().next().unwrap(),
CStr::from_ptr(inName.as_ptr()).to_string_lossy(),
CStr::from_ptr(zSuffix[i as usize]).to_string_lossy(),
);
}
setExit(1 as libc::c_int);
Expand All @@ -1702,9 +1698,7 @@ unsafe fn compress(mut name: *mut i8) {
}
if srcMode == 3 as libc::c_int || srcMode == 2 as libc::c_int {
stat(inName.as_mut_ptr(), &mut statBuf);
if statBuf.st_mode & 0o170000 as libc::c_int as libc::c_uint
== 0o40000 as libc::c_int as libc::c_uint
{
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,
Expand Down Expand Up @@ -1813,12 +1807,11 @@ unsafe fn compress(mut name: *mut i8) {
return;
}
if inStr.is_null() {
fprintf(
stderr,
b"%s: Can't open input file %s: %s.\n\0" as *const u8 as *const libc::c_char,
progName,
inName.as_mut_ptr(),
strerror(*__errno_location()),
eprintln!(
"{}: Can't open input file {}: {}.",
std::env::args().next().unwrap(),
CStr::from_ptr(inName.as_ptr()).to_string_lossy(),
std::io::Error::last_os_error(),
);
setExit(1 as libc::c_int);
return;
Expand All @@ -1834,12 +1827,11 @@ unsafe fn compress(mut name: *mut i8) {
b"wb\0" as *const u8 as *const libc::c_char,
);
if outStr.is_null() {
fprintf(
stderr,
b"%s: Can't create output file %s: %s.\n\0" as *const u8 as *const libc::c_char,
progName,
outName.as_mut_ptr(),
strerror(*__errno_location()),
eprintln!(
"{}: Can't create output file {}: {}.",
std::env::args().next().unwrap(),
CStr::from_ptr(inName.as_ptr()).to_string_lossy(),
std::io::Error::last_os_error(),
);
if !inStr.is_null() {
fclose(inStr);
Expand All @@ -1848,12 +1840,11 @@ unsafe fn compress(mut name: *mut i8) {
return;
}
if inStr.is_null() {
fprintf(
stderr,
b"%s: Can't open input file %s: %s.\n\0" as *const u8 as *const libc::c_char,
progName,
inName.as_mut_ptr(),
strerror(*__errno_location()),
eprintln!(
"{}: Can't open input file {}: {}.",
std::env::args().next().unwrap(),
CStr::from_ptr(inName.as_ptr()).to_string_lossy(),
std::io::Error::last_os_error(),
);
if !outStr.is_null() {
fclose(outStr);
Expand Down Expand Up @@ -1970,21 +1961,18 @@ unsafe fn uncompress(mut name: *mut i8) {
return;
}
if srcMode != 1 as libc::c_int && fileExists(inName.as_mut_ptr()) == 0 {
fprintf(
stderr,
b"%s: Can't open input file %s: %s.\n\0" as *const u8 as *const libc::c_char,
progName,
inName.as_mut_ptr(),
strerror(*__errno_location()),
eprintln!(
"{}: Can't open input file {}: {}.",
std::env::args().next().unwrap(),
CStr::from_ptr(inName.as_ptr()).to_string_lossy(),
std::io::Error::last_os_error(),
);
setExit(1 as libc::c_int);
return;
}
if srcMode == 3 as libc::c_int || srcMode == 2 as libc::c_int {
stat(inName.as_mut_ptr(), &mut statBuf);
if statBuf.st_mode & 0o170000 as libc::c_int as libc::c_uint
== 0o40000 as libc::c_int as libc::c_uint
{
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,
Expand Down Expand Up @@ -2084,12 +2072,11 @@ unsafe fn uncompress(mut name: *mut i8) {
);
outStr = stdout;
if inStr.is_null() {
fprintf(
stderr,
b"%s: Can't open input file %s:%s.\n\0" as *const u8 as *const libc::c_char,
progName,
inName.as_mut_ptr(),
strerror(*__errno_location()),
eprintln!(
"{}: Can't open input file {}:{}.",
std::env::args().next().unwrap(),
CStr::from_ptr(inName.as_ptr()).to_string_lossy(),
std::io::Error::last_os_error(),
);
if !inStr.is_null() {
fclose(inStr);
Expand All @@ -2108,12 +2095,11 @@ unsafe fn uncompress(mut name: *mut i8) {
b"wb\0" as *const u8 as *const libc::c_char,
);
if outStr.is_null() {
fprintf(
stderr,
b"%s: Can't create output file %s: %s.\n\0" as *const u8 as *const libc::c_char,
progName,
outName.as_mut_ptr(),
strerror(*__errno_location()),
eprintln!(
"{}: Can't create output file {}: {}.",
std::env::args().next().unwrap(),
CStr::from_ptr(inName.as_ptr()).to_string_lossy(),
std::io::Error::last_os_error(),
);
if !inStr.is_null() {
fclose(inStr);
Expand All @@ -2122,12 +2108,11 @@ unsafe fn uncompress(mut name: *mut i8) {
return;
}
if inStr.is_null() {
fprintf(
stderr,
b"%s: Can't open input file %s: %s.\n\0" as *const u8 as *const libc::c_char,
progName,
inName.as_mut_ptr(),
strerror(*__errno_location()),
eprintln!(
"{}: Can't open input file {}: {}.",
std::env::args().next().unwrap(),
CStr::from_ptr(inName.as_ptr()).to_string_lossy(),
std::io::Error::last_os_error(),
);
if !outStr.is_null() {
fclose(outStr);
Expand Down Expand Up @@ -2237,21 +2222,18 @@ unsafe fn testf(mut name: *mut i8) {
return;
}
if srcMode != 1 as libc::c_int && fileExists(inName.as_mut_ptr()) == 0 {
fprintf(
stderr,
b"%s: Can't open input %s: %s.\n\0" as *const u8 as *const libc::c_char,
progName,
inName.as_mut_ptr(),
strerror(*__errno_location()),
eprintln!(
"{}: Can't open input {}: {}.",
std::env::args().next().unwrap(),
CStr::from_ptr(inName.as_ptr()).to_string_lossy(),
std::io::Error::last_os_error(),
);
setExit(1 as libc::c_int);
return;
}
if srcMode != 1 as libc::c_int {
stat(inName.as_mut_ptr(), &mut statBuf);
if statBuf.st_mode & 0o170000 as libc::c_int as libc::c_uint
== 0o40000 as libc::c_int as libc::c_uint
{
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,
Expand Down Expand Up @@ -2288,12 +2270,11 @@ unsafe fn testf(mut name: *mut i8) {
b"rb\0" as *const u8 as *const libc::c_char,
);
if inStr.is_null() {
fprintf(
stderr,
b"%s: Can't open input file %s:%s.\n\0" as *const u8 as *const libc::c_char,
progName,
inName.as_mut_ptr(),
strerror(*__errno_location()),
eprintln!(
"{}: Can't open input file {}:{}.",
std::env::args().next().unwrap(),
CStr::from_ptr(inName.as_ptr()).to_string_lossy(),
std::io::Error::last_os_error(),
);
setExit(1 as libc::c_int);
return;
Expand Down
12 changes: 3 additions & 9 deletions bzip2recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@
use libc::{fprintf, FILE};

use libc::{
__errno_location, close, exit, fclose, fdopen, fflush, fopen, free, malloc, open, perror,
sprintf, strcat, strcpy, strlen, strncpy, strrchr,
close, exit, fclose, fdopen, fflush, fopen, free, malloc, open, perror, sprintf, strcat,
strcpy, strlen, strncpy, strrchr,
};

extern "C" {
static mut stderr: *mut FILE;
fn getc(__stream: *mut FILE) -> libc::c_int;
fn putc(__c: libc::c_int, __stream: *mut FILE) -> libc::c_int;
}
pub type __off_t = libc::c_long;
pub type __off64_t = libc::c_long;
pub type size_t = libc::c_ulong;
pub type _IO_lock_t = ();
pub type MaybeUInt64 = libc::c_ulonglong;
pub type Bool = libc::c_uchar;
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -146,9 +142,7 @@ unsafe fn bsGetBit(mut bs: *mut BitStream) -> i32 {
} else {
let mut retVal: i32 = getc((*bs).handle);
if retVal == -1 as libc::c_int {
if *__errno_location() != 0 as libc::c_int {
readError();
}
readError();
return 2 as libc::c_int;
}
(*bs).buffLive = 7 as libc::c_int;
Expand Down

0 comments on commit c68f560

Please sign in to comment.