Shell: do path resolution only if command doesn't contain '/'

This commit is contained in:
Bananymous 2023-12-07 13:34:46 +02:00
parent 2e858fddb5
commit 24243268a6
1 changed files with 15 additions and 21 deletions

View File

@ -469,11 +469,12 @@ pid_t execute_command_no_wait(BAN::Vector<BAN::String>& args, int fd_in, int fd_
// do PATH resolution // do PATH resolution
BAN::String executable_file; BAN::String executable_file;
if (!args.front().empty() && args.front().front() != '.' && args.front().front() != '/') if (!args.front().sv().contains('/'))
{
char* path_env_cstr = getenv("PATH");
if (path_env_cstr)
{ {
const char* path_env_cstr = getenv("PATH");
if (path_env_cstr == nullptr)
path_env_cstr = "";
auto path_env_list = MUST(BAN::StringView(path_env_cstr).split(':')); auto path_env_list = MUST(BAN::StringView(path_env_cstr).split(':'));
for (auto path_env : path_env_list) for (auto path_env : path_env_list)
{ {
@ -488,13 +489,6 @@ pid_t execute_command_no_wait(BAN::Vector<BAN::String>& args, int fd_in, int fd_
break; break;
} }
} }
if (executable_file.empty())
{
fprintf(stderr, "command not found: %s\n", args.front().data());
return -1;
}
}
} }
else else
{ {
@ -504,7 +498,7 @@ pid_t execute_command_no_wait(BAN::Vector<BAN::String>& args, int fd_in, int fd_
// Verify that the file exists is executable // Verify that the file exists is executable
{ {
struct stat st; struct stat st;
if (stat(executable_file.data(), &st) == -1) if (executable_file.empty() || stat(executable_file.data(), &st) == -1)
{ {
fprintf(stderr, "command not found: %s\n", args.front().data()); fprintf(stderr, "command not found: %s\n", args.front().data());
return -1; return -1;