use crate::io;
pub fn fgconsole(_argc: i32, _argv: *const *const u8) -> i32 {
let fd = io::open(b"/sys/class/tty/tty0/active", libc::O_RDONLY, 0);
if fd >= 0 {
let mut buf = [0u8; 32];
let n = io::read(fd, &mut buf);
io::close(fd);
if n > 0 {
let content = &buf[..n as usize];
if content.starts_with(b"tty") {
let num_start = 3;
let num_end = content.iter().position(|&c| c == b'\n' || c == 0).unwrap_or(n as usize);
if num_end > num_start {
io::write_all(1, &content[num_start..num_end]);
io::write_str(1, b"\n");
return 0;
}
}
}
}
io::write_str(1, b"1\n");
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_fgconsole_runs() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let output = Command::new(&armybox)
.args(["fgconsole"])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
let stdout = std::string::String::from_utf8_lossy(&output.stdout);
assert!(!stdout.is_empty());
}
}