Skip to content

Commit 6f80cd7

Browse files
committed
redox: handle multiple paths in PATH
1 parent 2652ce6 commit 6f80cd7

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

src/libstd/sys/redox/os.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ pub fn split_paths(unparsed: &OsStr) -> SplitPaths {
7373
fn bytes_to_path(b: &[u8]) -> PathBuf {
7474
PathBuf::from(<OsStr as OsStrExt>::from_bytes(b))
7575
}
76-
fn is_colon(b: &u8) -> bool { *b == b':' }
76+
fn is_semicolon(b: &u8) -> bool { *b == b';' }
7777
let unparsed = unparsed.as_bytes();
7878
SplitPaths {
79-
iter: unparsed.split(is_colon as fn(&u8) -> bool)
79+
iter: unparsed.split(is_semicolon as fn(&u8) -> bool)
8080
.map(bytes_to_path as fn(&[u8]) -> PathBuf)
8181
}
8282
}
@@ -94,7 +94,7 @@ pub fn join_paths<I, T>(paths: I) -> Result<OsString, JoinPathsError>
9494
where I: Iterator<Item=T>, T: AsRef<OsStr>
9595
{
9696
let mut joined = Vec::new();
97-
let sep = b':';
97+
let sep = b';';
9898

9999
for (i, path) in paths.enumerate() {
100100
let path = path.as_ref().as_bytes();

src/libstd/sys/redox/process.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
// except according to those terms.
1010

1111
use collections::hash_map::HashMap;
12-
use env;
12+
use env::{self, split_paths};
1313
use ffi::OsStr;
14+
use os::unix::ffi::OsStrExt;
1415
use fmt;
1516
use io::{self, Error, ErrorKind};
16-
use path::Path;
17+
use path::{Path, PathBuf};
1718
use sys::fd::FileDesc;
1819
use sys::fs::{File, OpenOptions};
1920
use sys::pipe::{self, AnonPipe};
@@ -313,23 +314,29 @@ impl Command {
313314
}
314315

315316
let program = if self.program.contains(':') || self.program.contains('/') {
316-
self.program.to_owned()
317-
} else {
318-
let mut path_env = ::env::var("PATH").unwrap_or(".".to_string());
319-
320-
if ! path_env.ends_with('/') {
321-
path_env.push('/');
317+
Some(PathBuf::from(&self.program))
318+
} else if let Ok(path_env) = ::env::var("PATH") {
319+
let mut program = None;
320+
for mut path in split_paths(&path_env) {
321+
path.push(&self.program);
322+
if path.exists() {
323+
program = Some(path);
324+
break;
325+
}
322326
}
323-
324-
path_env.push_str(&self.program);
325-
326-
path_env
327+
program
328+
} else {
329+
None
327330
};
328331

329-
if let Err(err) = syscall::execve(&program, &args) {
330-
io::Error::from_raw_os_error(err.errno as i32)
332+
if let Some(program) = program {
333+
if let Err(err) = syscall::execve(program.as_os_str().as_bytes(), &args) {
334+
io::Error::from_raw_os_error(err.errno as i32)
335+
} else {
336+
panic!("return from exec without err");
337+
}
331338
} else {
332-
panic!("return from exec without err");
339+
io::Error::new(io::ErrorKind::NotFound, "")
333340
}
334341
}
335342

src/libstd/sys/redox/syscall/call.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ pub fn dup2(fd: usize, newfd: usize, buf: &[u8]) -> Result<usize> {
7777
}
7878

7979
/// Replace the current process with a new executable
80-
pub fn execve(path: &str, args: &[[usize; 2]]) -> Result<usize> {
81-
unsafe { syscall4(SYS_EXECVE, path.as_ptr() as usize, path.len(),
82-
args.as_ptr() as usize, args.len()) }
80+
pub fn execve<T: AsRef<[u8]>>(path: T, args: &[[usize; 2]]) -> Result<usize> {
81+
unsafe { syscall4(SYS_EXECVE, path.as_ref().as_ptr() as usize,
82+
path.as_ref().len(), args.as_ptr() as usize, args.len()) }
8383
}
8484

8585
/// Exit the current process

0 commit comments

Comments
 (0)