use crate::io;
use crate::sys;
use super::get_arg;
const VT_DISALLOCATE: crate::io::IoctlReq = 0x5608u32 as crate::io::IoctlReq;
pub fn deallocvt(argc: i32, argv: *const *const u8) -> i32 {
let fd = io::open(b"/dev/tty0", libc::O_RDWR, 0);
if fd < 0 {
let fd = io::open(b"/dev/tty", libc::O_RDWR, 0);
if fd < 0 {
sys::perror(b"/dev/tty");
return 1;
}
}
if argc < 2 {
if unsafe { libc::ioctl(fd, VT_DISALLOCATE, 0) } < 0 {
sys::perror(b"VT_DISALLOCATE");
io::close(fd);
return 1;
}
} else {
for i in 1..argc {
if let Some(arg) = unsafe { get_arg(argv, i) } {
let vt = sys::parse_i64(arg).unwrap_or(0) as i32;
if vt > 0 {
if unsafe { libc::ioctl(fd, VT_DISALLOCATE, vt) } < 0 {
io::write_str(2, b"deallocvt: could not deallocate VT ");
io::write_num(2, vt as u64);
io::write_str(2, b"\n");
}
}
}
}
}
io::close(fd);
0
}
#[cfg(test)]
mod tests {
extern crate std;
use std::process::Command;
use std::path::PathBuf;
fn get_armybox_path() -> PathBuf {
if let Ok(path) = std::env::var("ARMYBOX_PATH") {
return PathBuf::from(path);
}
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.unwrap_or_else(|_| std::env::current_dir().unwrap());
let release = manifest_dir.join("target/release/armybox");
if release.exists() { return release; }
manifest_dir.join("target/debug/armybox")
}
#[test]
fn test_deallocvt_no_tty() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let output = Command::new(&armybox)
.args(["deallocvt"])
.output()
.unwrap();
assert!(output.status.code() == Some(0) || output.status.code() == Some(1));
}
#[test]
fn test_deallocvt_specific_vt() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let output = Command::new(&armybox)
.args(["deallocvt", "99"])
.output()
.unwrap();
assert!(output.status.code() == Some(0) || output.status.code() == Some(1));
}
}