-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
75 lines (71 loc) · 1.81 KB
/
main.cpp
File metadata and controls
75 lines (71 loc) · 1.81 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <memory>
#include <unordered_map>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
void sys_exit(const char *msg) {
perror(msg);
exit(EXIT_FAILURE);
}
std::vector<std::string> split(std::string const& str, char delim = ' ') {
std::istringstream stream{str};
std::vector<std::string> temp_vec;
std::string temp;
while(std::getline(stream, temp, delim)) {
temp_vec.emplace_back(temp.begin(), temp.end());
}
return temp_vec;
}
constexpr const char* default_shell_name = "Ahmet";
std::vector<std::string> paths(split(std::getenv("PATH"), ':'));
int main(int argc, char **argv) {
std::string temp;
for(;;) {
char cwd[255];
std::cout << getcwd(cwd,255) << " > ";
getline(std::cin, temp);
auto splitted = split(temp);
if(splitted.size() <= 0 || splitted[0] == "") {
continue;
}
if(splitted[0] == "cd" && splitted.size() == 2) {
chdir(splitted[1].c_str());
continue;
}
std::vector<const char *> cvec;
int i{};
std::for_each(splitted.begin(),splitted.end(), [&cvec, &i](const auto& item) {
cvec.push_back(const_cast<char*>(item.c_str()));
});
std::string path;
std::for_each(std::begin(paths), std::end(paths), [&](const auto& item) {
std::string t{item};
t += '/';
t += splitted[0];
if(access(t.c_str(), F_OK) != -1) {
path = t;
}
});
if(path.length() == 0 && splitted[0].size() > 2 && splitted[0][0] == '.' && splitted[0][1] == '/') {
path = splitted[0].substr(2);
}
else if(path.length() == 0) {
std::cout << splitted[0] << " NOT FOUND!" << "\n";
continue;
}
pid_t child_process = fork();
if(child_process == 0) {
execv(path.c_str(),const_cast<char*const*>(cvec.data()));
}
else {
wait(NULL);
}
}
return 0;
}