-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.cpp
More file actions
54 lines (49 loc) · 1.2 KB
/
shell.cpp
File metadata and controls
54 lines (49 loc) · 1.2 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
#include <cstdlib>
#include <iostream>
#include <limits.h>
#include <string>
#include <unistd.h>
#include <vector>
int main() {
while (true) {
std::cout << ">";
std::string s = "";
std::getline(std::cin, s);
std::vector<std::string> tokens;
std::string token = "";
for (char c : s) {
if (c == ' ') {
tokens.push_back(token);
token = "";
} else
token += c;
}
if (!token.empty())
tokens.push_back(token);
if (tokens.empty())
continue;
std::cout << tokens[0] << " -> ";
for (int i = 1; i < tokens.size(); i++)
std::cout << tokens[i] << " ";
std::cout << std::endl;
if (tokens[0] == "exit")
break;
else if (tokens[0] == "cd") {
if (tokens.size() == 2) {
std::string path = tokens[1];
if (!path.empty()) {
chdir(path.c_str());
std::cout << "cd invoked with " << tokens[1] << std::endl;
}
} else if (tokens.size() == 1) {
const char *home = getenv("HOME");
if (home != nullptr) {
chdir(home);
std::cout << "cd back to home\n";
}
}
} else
std::cout << "External command\n";
}
return 0;
}