Skip to content
This repository has been archived by the owner on Aug 17, 2022. It is now read-only.

Commit

Permalink
Use execvp instead of re-implementing PATH search
Browse files Browse the repository at this point in the history
  • Loading branch information
jennamagius committed Jun 25, 2018
1 parent 7dc52c5 commit d68cd8a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 26 deletions.
7 changes: 3 additions & 4 deletions src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use log::{debug, error, info, log, trace, warn};
use nix::{
pty::openpty,
unistd::{
close, dup2, execv, fork, setsid,
close, dup2, execvp, fork, setsid,
ForkResult::{Child, Parent},
Pid,
},
Expand Down Expand Up @@ -51,8 +51,7 @@ impl Pty {
let argv: Vec<CString>;
if command.is_some() {
let command = command.unwrap();
let exe_path = crate::util::search_path(&command[0]).ok_or(())?;
exe = CString::new(exe_path).map_err(|_| ())?;
exe = CString::new(command[0].as_str()).map_err(|_| ())?;
let argv2: Vec<Result<CString, _>> = command.into_iter().map(|x| CString::new(x)).collect();
if argv2.iter().filter(|x| x.is_err()).next().is_some() {
return Err(());
Expand Down Expand Up @@ -119,7 +118,7 @@ impl Pty {
close(*i).ok();
}
}
execv(&exe, &argv[..]).expect("execv failed");
execvp(&exe, &argv[..]).expect("execvp failed");
unreachable!();
}
Err(_) => panic!("Fork failed"),
Expand Down
22 changes: 0 additions & 22 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,3 @@ crate fn current_user_pw() -> Result<Pwent, ()> {
let uid = unsafe { libc::getuid() };
getpwuid(uid)
}

crate fn search_path(bin: &str) -> Option<String> {
trace!("Searching path for {:?}", bin);
if ::std::path::Path::new(bin).is_absolute() {
return Some(bin.to_string());
}
let path = std::env::var("PATH");
if path.is_err() {
return None;
}
let path: String = path.unwrap();
for part in path.split(':') {
let mut dest = ::std::path::PathBuf::from(part);
dest.push(bin);
if ::std::fs::metadata(&dest).is_ok() {
if let Some(dest) = dest.to_str() {
return Some(dest.to_string());
}
}
}
None
}

0 comments on commit d68cd8a

Please sign in to comment.