-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
executable file
·355 lines (317 loc) · 8.17 KB
/
Copy pathshell.c
File metadata and controls
executable file
·355 lines (317 loc) · 8.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "headers.h"
#include "echo.h"
#include "printdir.h"
#include "history.h"
#include "cd.h"
#include "ls.h"
#include "discover.h"
/*
NOTE
curr_dir is the inital directory from which the code is executed not the present directory
*/
char last_working_dir[100];
int no_of_background = 1;
int spec6_output_index = 0;
int spec6_output_traverser = 0;
int bg_processes[1000];
char bg_command[1000][100];
char spec6_output[1000][1000];
int time_flag = 0;
void background_handler()
{
int status;
int x = 0;
int p_id = waitpid(-1, &status, WNOHANG);
if (p_id > 0)
{
// printf("hello\n");
while (bg_processes[x] != p_id)
{
++x;
}
// printf("hii\n");
if (WIFSTOPPED(status))
{
return;
}
sprintf(spec6_output[spec6_output_index++], "%s with pid %d exited %s\n", bg_command[x], bg_processes[x], WIFEXITED(status) ? "normally" : "abnormally");
// printf("%s with pid %d exited %s\n", bg_command[x], bg_processes[x], WIFEXITED(status) ? "normally" : "abnormally");
return;
}
}
void init_shell()
{
clear();
}
void pwd_implementation()
{
char current_wd[100];
getcwd(current_wd, 100);
printf("%s\n", current_wd);
}
void pinfo(char command_words[MAX_NO_COMMANDS_IN_INPUT][100], int len, char curr_dir[100], int number_of_commands)
{
int pid;
char path[1000];
char exec_filepath[1000];
if (number_of_commands == 1)
{
pid = getpid();
sprintf(path, "/proc/%d/stat", pid);
sprintf(exec_filepath, "/proc/%d/exe", pid);
}
else
{
pid = atoi(command_words[1]);
strcpy(path, "/proc/");
strcat(path, command_words[1]);
strcpy(exec_filepath, path);
strcat(path, "/stat");
strcat(exec_filepath, "/exe");
}
char pinfo[1000];
FILE *procfile;
procfile = fopen(path, "r");
if (procfile == NULL)
{
perror("Couldn't open procfile\n");
return;
}
fread(pinfo, 1000, 1, procfile);
fclose(procfile);
char tokens_pinfo[1000][100];
char *rest = pinfo;
char *token;
int x = 0;
while ((token = strtok_r(rest, " ", &rest)))
{
strcpy(tokens_pinfo[x], token);
++x;
}
if (strcmp(tokens_pinfo[4], tokens_pinfo[7]) == 0 )
{
strcat(tokens_pinfo[2], "+");
}
printf("pid : %d\n", pid);
printf("process Status : %s\n", tokens_pinfo[2]);
printf("memory : %s\n", tokens_pinfo[22]);
// For the executable path
char exec_path[4096] = {'\0'};
int readlink_ret = readlink(exec_filepath, exec_path, 4096);
if (readlink_ret == -1)
{
printf("Zombie process: Execpath doesn't exist\n");
}
else
{
printf("executable path : %s\n", exec_path);
}
}
void executer(char *command_words[100])
{
if (execvp(command_words[0], command_words) < 0)
{
printf("Error: Command not found : %s\n", command_words[0]);
}
}
void executeInputcommand(char command_words[100][100], int len, char curr_dir[100], int number_of_commands)
{
if (strcmp(command_words[0], "echo") == 0)
{
echo_implementation(command_words, number_of_commands);
}
else if (strcmp(command_words[0], "pwd") == 0)
{
pwd_implementation();
}
else if (strcmp(command_words[0], "cd") == 0)
{
if (number_of_commands <= 2)
{
cd_implementation(command_words, len, curr_dir, number_of_commands);
}
else
{
printf("Too many arguments\n");
}
}
else if (strcmp(command_words[0], "history") == 0)
{
history_implementation(curr_dir);
}
else if (strcmp(command_words[0], "ls") == 0)
{
ls_implementation(command_words, len, curr_dir, number_of_commands);
}
else if (strcmp(command_words[0], "pinfo") == 0)
{
pinfo(command_words, len, curr_dir, number_of_commands);
}
else if (strcmp(command_words[0], "discover") == 0)
{
discover(command_words, len, curr_dir, number_of_commands);
}
else
{
printf("Invalid Command\n");
}
}
void space_parser(char command[100], int len, char curr_dir[100])
{
int number_of_commands = 0;
char command_words[MAX_NO_COMMANDS_IN_INPUT][100];
char *token;
char *rest = command;
while ((token = strtok_r(rest, " ", &rest)))
{
strcpy(command_words[number_of_commands], token);
++number_of_commands;
}
executeInputcommand(command_words, len, curr_dir, number_of_commands);
}
void background_proc(char command[100], int len, char curr_dir[100])
{
char *command_words[100];
char *token;
char *rest = command;
int no_of_commands = 0;
while ((token = strtok_r(rest, " ", &rest)))
{
command_words[no_of_commands] = token;
++no_of_commands;
}
command_words[no_of_commands] = NULL;
pid_t ret_value = fork();
int status;
if (ret_value == 0)
{
setpgid(0,0);
if (execvp(command_words[0], command_words) < 0)
{
printf("Error: Command not found : %s\n", command_words[0]);
}
}
else
{
waitpid(ret_value, &status, WNOHANG);
bg_processes[no_of_background] = ret_value;
strcpy(bg_command[no_of_background], command_words[0]);
printf("[%d] %d\n", no_of_background, ret_value);
++no_of_background;
}
}
void foreground_proc(char command[100], int len, char curr_dir[100])
{
char *command_words[100];
time_t start;
start = time(NULL);
char *token;
char *rest = command;
int no_of_commands = 0;
while ((token = strtok_r(rest, " ", &rest)))
{
command_words[no_of_commands] = token;
++no_of_commands;
}
command_words[no_of_commands] = NULL;
pid_t ret_value = fork();
int status;
if (ret_value == 0)
{
if (execvp(command_words[0], command_words) < 0)
{
printf("Error: Command not found : %s\n", command_words[0]);
}
}
else
{
waitpid(ret_value, &status, 0);
time_t end;
end = time(NULL);
time_flag = end - start;
}
}
void Readinputcommand_amp(char command[100], int len, char curr_dir[100])
{
char *token;
char *rest = command;
int flag = 0;
int str_len = strlen(command);
int flag2 = 0;
for (int i = 0; command[i] != '\0'; ++i)
{
if (command[i] == '&')
{
flag2 = 1;
break;
}
}
if (flag2 != 1)
{
space_parser(command, len, curr_dir);
return;
}
if (command[str_len - 1] == '&')
{
flag = 1;
}
while ((token = strtok_r(rest, "&", &rest)))
{
if (rest[0] == '\0' && flag == 0)
{
foreground_proc(token, len, rest);
}
else
{
background_proc(token, len, curr_dir);
}
}
}
void Readinputcommand(char input_command[MAX_COMMAND_LEN], int len, char curr_dir[100])
{
char command[100];
char *token;
char *rest = input_command;
while ((token = strtok_r(rest, ";", &rest)))
{
strcpy(command, token);
Readinputcommand_amp(command, len, curr_dir);
}
}
int main()
{
signal(SIGCHLD, background_handler);
init_shell();
char curr_dir[CURR_DIR_LEN];
getcwd(curr_dir, 100);
int len = strlen(curr_dir);
strcpy(last_working_dir, curr_dir);
char present_dir[100];
char prev_command[100];
int flag = 0;
char last_command[100];
while (1)
{
getcwd(present_dir, 100);
if (strlen(present_dir) >= strlen(curr_dir))
{
print_directory(len);
}
else
{
print_directory2(len);
}
char input_command[MAX_COMMAND_LEN];
gets(input_command);
if (strcmp(input_command, last_command) != 0)
{
add_to_History(input_command, curr_dir, len);
}
Readinputcommand(input_command, len, curr_dir);
for (int i = spec6_output_traverser; spec6_output[i][0] != '\0'; ++i)
{
printf("%s", spec6_output[spec6_output_traverser++]);
}
strcpy(last_command, input_command);
}
}