-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_command.cpp
More file actions
40 lines (36 loc) · 1.17 KB
/
type_command.cpp
File metadata and controls
40 lines (36 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "type_command.h"
#include <iostream>
#include "command_factory.h"
#include "command.h"
#include <unistd.h>
void TypeCommand::execute(const std::string& args) {
const std::string path = "PATH";
const char* envPath = getenv(path.c_str());
CommandFactory commandFactory;
auto command = commandFactory.get(args);
if(command) {
std::cout << command->name() << " is a shell " << command->type() << std::endl;
}
else {
if (envPath != nullptr) {
std::string pathEnvStr(envPath);
size_t start = 0, end = 0;
while ((end = pathEnvStr.find(':', start)) != std::string::npos) {
std::string dir = pathEnvStr.substr(start, end - start);
std::string fullPath = dir + "/" + args;
if (access(fullPath.c_str(), X_OK) == 0) {
std::cout << args << " is " << fullPath << std::endl;
return;
}
start = end + 1;
}
}
std::cout << args << ": not found" << std::endl;
}
}
std::string TypeCommand::type() {
return "builtin";
}
std::string TypeCommand::name() {
return "type";
}