Skip to content

Commit 9f07ed2

Browse files
committed
added ability to search path for command
1 parent f6a6261 commit 9f07ed2

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/type_command.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,31 @@
22
#include <iostream>
33
#include "command_factory.h"
44
#include "command.h"
5+
#include <unistd.h>
56

67
void TypeCommand::execute(const std::string& args) {
8+
const std::string path = "PATH";
9+
const char* envPath = getenv(path.c_str());
10+
711
CommandFactory commandFactory;
812
auto command = commandFactory.get(args);
913
if(command) {
1014
std::cout << command->name() << " is a shell " << command->type() << std::endl;
11-
} else {
15+
}
16+
else {
17+
if (envPath != nullptr) {
18+
std::string pathEnvStr(envPath);
19+
size_t start = 0, end = 0;
20+
while ((end = pathEnvStr.find(':', start)) != std::string::npos) {
21+
std::string dir = pathEnvStr.substr(start, end - start);
22+
std::string fullPath = dir + "/" + args;
23+
if (access(fullPath.c_str(), X_OK) == 0) {
24+
std::cout << args << " is " << fullPath << std::endl;
25+
return;
26+
}
27+
start = end + 1;
28+
}
29+
}
1230
std::cout << args << ": not found" << std::endl;
1331
}
1432
}

0 commit comments

Comments
 (0)