From c68f5603b9c845d920ab8831919d7aafc73205a0 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Tue, 5 Nov 2024 15:58:24 +0100 Subject: [PATCH] Fix for macOS --- bzip2.rs | 147 +++++++++++++++++++++--------------------------- bzip2recover.rs | 12 +--- 2 files changed, 67 insertions(+), 92 deletions(-) diff --git a/bzip2.rs b/bzip2.rs index ed9830af4..402e48850 100644 --- a/bzip2.rs +++ b/bzip2.rs @@ -4,6 +4,7 @@ #![allow(unused_assignments)] #![allow(unused_mut)] +use std::ffi::CStr; use std::mem::zeroed; use libbzip2_rs_sys::bzlib::{ @@ -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; @@ -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 @@ -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; @@ -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); @@ -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, @@ -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; @@ -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); @@ -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); @@ -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, @@ -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); @@ -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); @@ -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); @@ -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, @@ -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; diff --git a/bzip2recover.rs b/bzip2recover.rs index 24102e289..d2d183397 100644 --- a/bzip2recover.rs +++ b/bzip2recover.rs @@ -8,8 +8,8 @@ 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" { @@ -17,10 +17,6 @@ extern "C" { 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)] @@ -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;