From 7b54240848668ec6523bc555c20a2499741da3b7 Mon Sep 17 00:00:00 2001 From: Takashi Kobayashi <7821157+t-koba@users.noreply.github.com> Date: Sun, 17 Dec 2023 15:55:23 +0900 Subject: [PATCH] macOS support by removing system dependencies --- src/elements/command/simple.rs | 4 ++-- src/main.rs | 25 +++++++++++-------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/elements/command/simple.rs b/src/elements/command/simple.rs index 766cb6aa..162c516e 100644 --- a/src/elements/command/simple.rs +++ b/src/elements/command/simple.rs @@ -3,7 +3,7 @@ use std::env; -use nix::unistd::{execvpe, fork, ForkResult, Pid}; +use nix::unistd::{execve, fork, ForkResult, Pid}; use nix::unistd; use std::ffi::CString; use std::process::exit; @@ -222,7 +222,7 @@ impl SimpleCommand { .map(|a| CString::new(a.to_string()).unwrap()) .collect(); - let _ = execvpe(&cargs[0], &cargs, &envs); + let _ = execve(&cargs[0], &cargs, &envs); eprintln!("Command not found: {:?}", &cargs[0]); exit(127); diff --git a/src/main.rs b/src/main.rs index f959f31b..e6c1a5dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,10 +12,11 @@ mod utils; mod feeder; mod debuginfo; -use std::{env, process, path}; -use std::os::linux::fs::MetadataExt; +use std::{env, process}; use std::fs::{File,OpenOptions}; -use std::io::Read; +use nix::libc; +use nix::unistd::isatty; +use nix::sys::utsname::uname; use crate::core::ShellCore; use crate::core::proc; @@ -26,10 +27,9 @@ use crate::elements::script::Script; use crate::file_descs::FileDescs; use std::os::unix::io::IntoRawFd; -fn is_interactive(pid: u32) -> bool { - let std_path = format!("/proc/{}/fd/0", pid); - match path::Path::new(&std_path).metadata() { - Ok(metadata) => metadata.st_mode() == 8592, +fn is_interactive() -> bool { + match isatty(libc::STDIN_FILENO) { + Ok(atty) => atty, Err(err) => panic!("{}", err), } } @@ -44,12 +44,9 @@ fn read_bashrc(core: &mut ShellCore){ } fn get_hostname() -> String{ - if let Ok(mut file) = File::open("/etc/hostname") { - - let mut fullname = String::new(); - if let Ok(_) = file.read_to_string(&mut fullname) { - return fullname.trim_end().to_string(); - } + if let Ok(uts) = uname() { + let fullname = uts.nodename().to_string_lossy(); + return fullname.to_string(); } "unknown".to_string() @@ -121,7 +118,7 @@ fn main() { core.set_var("HOSTNAME", &get_hostname()); core.set_var("SHELL", "rustybash"); core.set_var("BASH", &core.args[0].to_string()); - if is_interactive(pid) { + if is_interactive() { core.flags += "i"; }