#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
use std::process::ExitCode;
#[cfg(unix)]
fn reset_sigpipe() {
unsafe {
libc::signal(libc::SIGPIPE, libc::SIG_DFL);
}
}
#[cfg(not(unix))]
fn reset_sigpipe() {}
#[cfg(unix)]
mod interrupt {
fn append(buf: &mut [u8; 96], len: &mut usize, src: &[u8]) {
for &byte in src {
if *len < buf.len() {
buf[*len] = byte;
*len += 1;
}
}
}
fn append_usize(buf: &mut [u8; 96], len: &mut usize, mut value: usize) {
if value == 0 {
append(buf, len, b"0");
return;
}
let mut digits = [0u8; 20];
let mut count = 0;
while value > 0 {
digits[count] = b'0' + (value % 10) as u8;
value /= 10;
count += 1;
}
while count > 0 {
count -= 1;
let digit = digits[count];
append(buf, len, &[digit]);
}
}
extern "C" fn handle_sigint(_signum: libc::c_int) {
let (scanned, total, findings) = keyhog::interrupt_counts();
let mut buf = [0u8; 96];
let mut len = 0;
append(&mut buf, &mut len, b"\nScan interrupted. ");
append_usize(&mut buf, &mut len, scanned);
append(&mut buf, &mut len, b"/");
append_usize(&mut buf, &mut len, total);
append(&mut buf, &mut len, b" files scanned. ");
append_usize(&mut buf, &mut len, findings);
append(&mut buf, &mut len, b" findings.\n");
unsafe {
libc::write(2, buf.as_ptr().cast(), len);
libc::_exit(keyhog::exit_codes::EXIT_INTERRUPTED as libc::c_int);
}
}
pub(super) fn install() {
unsafe {
libc::signal(
libc::SIGINT,
handle_sigint as *const () as libc::sighandler_t,
);
}
}
}
#[cfg(not(unix))]
mod interrupt {
pub(super) fn install() {}
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> ExitCode {
reset_sigpipe();
interrupt::install();
keyhog::cli_main().await
}