-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
274 lines (231 loc) · 5.25 KB
/
main.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <bits/stdc++.h>
#include <unistd.h>
#include <sys/wait.h>
#define BUFFER_SIZE 1024
#define TOK_BUFFER_SIZE 64
#define INPUT_LINE_DELIM " \t\r\n\a"
using namespace std;
# define PATH "/home/blackreaper/Documents/Shell/"
char poss_commands[18][50] = {
"ls",
"echo",
"cp",
"touch",
"rm",
"history",
"whoami",
"zip",
"unzip",
"getp",
"setp",
"help",
"mkdir",
"rmdir",
"mv",
"screenshot",
"test",
"calculator"
};
void execute_exit()
{
exit(0);
}
int execute_pwd(char **argv){
char cwd[1024];
getcwd(cwd, sizeof(cwd));
printf("Current working dir: %s\n", cwd);
return 1;
}
int execute_cd(char **argv){
if (argv[1] == NULL) {
fprintf(stderr, "lsh: expected argument to \"cd\"\n");
return 0;
}
else
{
if (chdir(argv[1]) != 0)
{
perror("lsh");
return 1;
}
else
{
char cwd[1024];
getcwd(cwd, sizeof(cwd));
printf("Current working dir: %s\n", cwd);
}
}
return 1;
}
char **split_input_line(char *input_line)
{
int tokbufsize = TOK_BUFFER_SIZE, position = 0;
char **tokens = (char **)malloc(tokbufsize * sizeof(char *));
char *token;
if (!tokens)
{
fprintf(stderr, "hha@LBP-shell: allocation error\n");
exit(EXIT_FAILURE);
}
token = strtok(input_line, INPUT_LINE_DELIM);
while (token != NULL)
{
tokens[position] = token;
position++;
if (position >= tokbufsize)
{
tokbufsize += TOK_BUFFER_SIZE;
tokens = (char **)realloc(tokens, tokbufsize * sizeof(char *));
if (!tokens)
{
fprintf(stderr, "hha@LBP-shell: allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, INPUT_LINE_DELIM);
}
tokens[position] = NULL;
return tokens;
}
char *read_input()
{
int buff_size = BUFFER_SIZE;
int position = 0;
char *buffer = (char *)malloc(buff_size * sizeof(char));
if (!buffer)
{
fprintf(stderr, "hha@LBP-shell: allocation error\n");
exit(EXIT_FAILURE);
}
char c;
while (1)
{
c = getchar();
if(c == EOF) {
exit(EXIT_SUCCESS);
} else if (c == '\n') {
buffer[position] = '\0';
return buffer;
} else {
buffer[position] = c;
}
position++;
if (position >= buff_size) {
buff_size += BUFFER_SIZE;
buffer = (char*) realloc(buffer, buff_size);
if (!buffer) {
fprintf(stderr, "hha@LBP-shell: allocation error\n");
exit(EXIT_FAILURE);
}
}
}
return buffer;
}
void loop_input(){
char *input_line;
char **splitted_input;
int status = 1;
ofstream history_file("history.txt", ios::out | ios::app);
string command;
int no_poss_comm = sizeof(poss_commands) / 50;
do {
printf("\033[;32mhha@LBP-shell>>\033[0m ");
input_line = read_input();
// wirte the command in history file
command = input_line;
command += "\n";
history_file << command;
history_file.flush();
// split input command against whitespace or other identations
splitted_input = split_input_line(input_line);
if (splitted_input[0] == NULL) {
status = 1;
}
else{
bool matched = false;
for (int i = 0; i < no_poss_comm && !matched; i++) {
if (strcmp(splitted_input[0], poss_commands[i]) == 0) {
int pid = fork();
if(pid < 0) fprintf(stderr, "fork error!");
else if(pid == 0){
char* tobesent[50];
int ind = 0;
while(splitted_input[ind]){
tobesent[ind] = splitted_input[ind];
ind++;
}
tobesent[ind] = splitted_input[ind];
char filename[] = PATH;
strcat(filename, poss_commands[i]);
execvp(filename, splitted_input);
}
else{
if ((pid = waitpid(pid, &status, 0)) < 0)
fprintf(stderr, "pid error!");
status = 1;
}
matched = true;
}
}
if(!matched){
if (strcmp(splitted_input[0], "cd") == 0) {
status = execute_cd(splitted_input);
matched = true;
}
else if (strcmp(splitted_input[0], "pwd") == 0) {
status = execute_pwd(splitted_input);
matched = true;
}
else if (strcmp(splitted_input[0], "exit") == 0) {
free(input_line);
free(splitted_input);
history_file.close();
execute_exit();
}
else{
int pid = fork();
if(pid < 0) fprintf(stderr, "fork error!");
else if(pid == 0){
char current_path[1024];
getcwd(current_path, sizeof(current_path));
strcat(current_path, "/");
strcat(current_path, splitted_input[0]);
int executed = execvp(current_path, splitted_input);
if(executed == -1) {
printf("The command entered is not supported by this shell!!!\n");
kill(getpid(), SIGKILL);
}
}
else{
if ((pid = waitpid(pid, &status, 0)) < 0)
fprintf(stderr, "pid error!");
status = 1;
}
matched = true;
}
}
if(!matched){
printf("The command entered is not supported by this shell!!!\n");
status = 1;
}
}
free(input_line);
free(splitted_input);
} while (status);
history_file.close();
}
int main()
{
printf("*********************************\n\n");
printf("WELCOME TO OUR UNIX-SHELL-VARIANT\n");
printf("Developed by: Hemant, Hemil, Ashish\n");
printf("Under the supervison of: Prof. Manoj Mishra\n");
printf("Enjoy using it!!!\n\n");
printf("*********************************\n\n");
char **something;
loop_input();
return 0;
}