File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed
Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff line change 22#include < iostream>
33#include " command_factory.h"
44#include " command.h"
5+ #include < unistd.h>
56
67void 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}
You can’t perform that action at this time.
0 commit comments