-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilt-ins.cpp
More file actions
113 lines (97 loc) · 2.5 KB
/
Copy pathbuilt-ins.cpp
File metadata and controls
113 lines (97 loc) · 2.5 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "built-ins.h"
int totalJobs;
int nextId;
struct Process myJobs[5];
string fifoDir = "/tmp/Quash/";
void cd(std::vector<std::string> args){
if(args.size() == 1){
chdir(getenv("HOME"));
}else{
chdir(args[1].c_str());
}
}
void exportVar(std::vector<std::string> args){
if(args.size() == 1){
std::cout << "Error: No arguments given" << std::endl;
}else{
vector<string> subArgs = split(args[1], '=');
// std::string var = args[1];
// std::string val = args[2];
setenv(subArgs[0].c_str(), subArgs[1].c_str(), 1);
}
}
void echo(std::vector<std::string> args){
for(int i = 1; i < args.size(); i++){
if (args[i][0] == '$') {
string envVar = "";
envVar = getenv(args[i].erase(0, 1).c_str());
// if (getenv(args[i].erase(0, 1).c_str()) == NULL) {
// std::cout<<"Environment Variable not found"<<" ";
// }
std::cout<<envVar<<" ";
} else {
std::cout << args[i] << " ";
}
}
std::cout << std::endl;
}
void pwd(){
char cwd[1024];
getcwd(cwd, sizeof(cwd));
std::cout << cwd << std::endl;
}
void jobs(){
for(int i=0; i<totalJobs; i++) {
std::cout << "[" << myJobs[i].id << "] " << myJobs[i].pid << myJobs[i].cmd << std::endl;
}
}
void kill(std::vector<std::string> args){
if (args.size() < 2) {
std::cout << "Error: Not enough arguments" << std::endl;
return;
}
int sig;
if (args.size() == 2) {
sig = SIGTERM;
}else{
sig = stoi(args.at(2));
}
printf("Killing %d with signal %d\n", stoi(args.at(1)), sig);
kill(stoi(args.at(1)), sig);
}
void clearFifo(){
try {
for (const auto & entry : std::filesystem::directory_iterator(fifoDir)) {
std::filesystem::remove(entry.path());
}
} catch (std::filesystem::filesystem_error& e) {
std::cerr << e.what() << '\n';
}
}
string tag(){
usleep(2000);
return "[QUASH]$ ";
}
vector<string> split (const string &s, char delim) {
vector<string> result;
stringstream ss (s);
string item;
while (getline (ss, item, delim)) {
item = trim(item, ' ');
result.push_back (item);
}
return result;
}
string trim(string s, char delim) {
int i = s.length();
if (i == 0) {
return s;
}
if (s[0] == delim) {
s.erase(0, 1);
}
if (s[i-1] == delim) {
s.erase(i-1, 1);
}
return s;
}