Skip to content
Snippets Groups Projects
Commit d932ade7 authored by Sag0Sag0's avatar Sag0Sag0 Committed by Michael Aaron Murphy
Browse files

Add ability for which builtin to process multiple arguments (#629)

* Start adding manpages

* Add manpages for all builtins

* Fix so that changes pass tests

* Add ability for which to process more than one argument

* Fix typo

* Fixes

* Fixes
parent d096b4d1
No related branches found
No related tags found
No related merge requests found
......@@ -577,9 +577,9 @@ pub(crate) const MAN_WHICH: &'static str = r#"NAME
which - locate a program file in the current user's path
SYNOPSIS
which PROGRAM
which PROGRAM
DESCRIPTION
The which utility takes a command name and searches the path for the executable file that would
be run had the command been executed.
The which utility takes a list of command names and searches for the
alias/builtin/function/executable that would be executed if you ran that command.
"#;
\ No newline at end of file
......@@ -585,34 +585,28 @@ fn builtin_which(args: &[&str], shell: &mut Shell) -> i32 {
return SUCCESS
}
if args[1..].len() != 1 {
let stderr = io::stderr();
let mut stderr = stderr.lock();
let _ = stderr.write_all(b"which takes one argument\n");
return BAD_ARG;
}
let command = args[1];
if let Some(alias) = shell.variables.aliases.get(command) {
println!("{}: alias to {}", command, alias);
SUCCESS
} else if shell.builtins.contains_key(command) {
println!("{}: built-in shell command", command);
SUCCESS
} else if shell.functions.contains_key(command) {
println!("{}: function", command);
SUCCESS
} else {
for path in env::var("PATH").unwrap_or("/bin".to_string()).split(sys::PATH_SEPARATOR) {
let executable = Path::new(path).join(command);
if executable.is_file() {
println!("{}", executable.display());
return SUCCESS;
let mut result = SUCCESS;
'outer: for &command in &args[1..] {
if let Some(alias) = shell.variables.aliases.get(command) {
println!("{}: alias to {}", command, alias);
continue;
} else if shell.functions.contains_key(command) {
println!("{}: function", command);
continue;
} else if shell.builtins.contains_key(command) {
println!("{}: built-in shell command", command);
continue;
} else {
for path in env::var("PATH").unwrap_or("/bin".to_string()).split(sys::PATH_SEPARATOR) {
let executable = Path::new(path).join(command);
if executable.is_file() {
println!("{}", executable.display());
continue 'outer;
}
}
result = FAILURE;
println!("{} not found", command);
}
println!("{} not found", command);
FAILURE
}
result
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment