#![allow(unsafe_code)]
use std::os::fd::{BorrowedFd, RawFd};
#[must_use]
pub fn getuid() -> u32 {
unsafe { libc::getuid() }
}
pub fn kill(pid: i32, sig: i32) -> std::io::Result<()> {
let ret = unsafe { libc::kill(pid as libc::pid_t, sig) };
if ret == 0 {
Ok(())
} else {
Err(std::io::Error::last_os_error())
}
}
#[must_use]
pub fn page_size() -> u64 {
#[allow(clippy::cast_sign_loss)]
let size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) as u64 };
size
}
#[must_use]
pub const fn borrow_fd(fd: RawFd) -> BorrowedFd<'static> {
unsafe { BorrowedFd::borrow_raw(fd) }
}
#[must_use]
pub fn terminal_size() -> Option<(u16, u16)> {
use libc::{TIOCGWINSZ, winsize};
use std::os::unix::io::AsRawFd;
let fd = std::io::stdout().as_raw_fd();
let mut ws: winsize = unsafe { std::mem::zeroed() };
let result = unsafe { libc::ioctl(fd, TIOCGWINSZ, &mut ws) };
if result == 0 && ws.ws_row > 0 && ws.ws_col > 0 {
Some((ws.ws_row, ws.ws_col))
} else {
None
}
}