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,30 +469,24 @@ 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"); const char* path_env_cstr = getenv("PATH");
if (path_env_cstr) if (path_env_cstr == nullptr)
path_env_cstr = "";
auto path_env_list = MUST(BAN::StringView(path_env_cstr).split(':'));
for (auto path_env : path_env_list)
{ {
auto path_env_list = MUST(BAN::StringView(path_env_cstr).split(':')); BAN::String test_file = path_env;
for (auto path_env : path_env_list) MUST(test_file.push_back('/'));
{ MUST(test_file.append(args.front()));
BAN::String test_file = path_env;
MUST(test_file.push_back('/'));
MUST(test_file.append(args.front()));
struct stat st; struct stat st;
if (stat(test_file.data(), &st) == 0) if (stat(test_file.data(), &st) == 0)
{
executable_file = BAN::move(test_file);
break;
}
}
if (executable_file.empty())
{ {
fprintf(stderr, "command not found: %s\n", args.front().data()); executable_file = BAN::move(test_file);
return -1; break;
} }
} }
} }
@ -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;