-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
387 lines (310 loc) · 7.21 KB
/
Copy pathshell.c
File metadata and controls
387 lines (310 loc) · 7.21 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <stdbool.h>
#include <sys/mman.h>
#include <signal.h>
#include "functions.h"
// utils.c
void whereami(char *);
int parse_args(char *, char **, int);
int get_type_process(char*);
void _add_history(char*, char*);
// builtin.c
void _export(char**, void *);
void _cd(char**, void*);
void _history(char *);
void _kill(char **, void*);
void _jobs(node*);
void _fg(char**, void*);
void _bg(char**, void*);
void _echo(char **);
void _set();
int pid_changed = -1, pid_action = -1;
// Signal handlers
void handle_sigint(int sig){
}
void handle_sigtstp(int sig){
}
void handle_sigchld(int sig){
pid_t pid;
int status;
pid = waitpid(-1, &status, (WNOHANG | WUNTRACED | WCONTINUED));
if(pid > 0){
if(WIFSTOPPED(status)){
pid_changed = pid;
pid_action = 1;
}else if(WIFEXITED(status) || WIFSIGNALED(status)){
pid_changed = pid;
pid_action = 0;
}else if(WIFCONTINUED(status)){
pid_changed = pid;
pid_action = 2;
}
}
}
extern char **environ;
int main(int argc, char **argv, char **envp){
pid_t pid;
char cmd[100], *args[100], wami[200], cmd_aux[200], hpath[500];
char aux[200], aux2[200], *sptr1;
void *shmem;
bool bg = false;
int t_process;
int len;
// Initialize jobs list
node* jobs = NULL;
// Create shared memory
shmem = mmap(NULL, 200, (PROT_READ | PROT_WRITE), (MAP_SHARED | MAP_ANONYMOUS), -1, 0);
// Defining signal handles
signal(SIGINT, handle_sigint);
signal(SIGTSTP, handle_sigtstp);
// MYPATH
setenv("MYPATH", getenv("PATH"), 1);
// MYPS1
setenv("MYPS1", "tecii$ ", 1);
// History path
if(access(".history", F_OK) != 0){
FILE* fp = fopen(".history", "w");
fclose(fp);
}
strcpy(hpath, getenv("PWD"));
strcat(hpath, "/.history");
// Defining pgid
pid = getpid();
setpgid(pid, pid);
tcsetpgrp(0, pid);
while(1){
// Update jobs linked list
if(pid_changed != -1){
if(pid_action == 1){
update_status(&jobs, pid_changed, 0);
}else if(pid_action == 0){
del(&jobs, pid_changed);
}else if(pid_action == 2){
update_status(&jobs, pid_changed, 1);
}
pid_changed = pid_action = -1;
}
// Start new shell input
//whereami(wami);
fprintf(stdout, "\033[38;5;46m%s\033[m", getenv("MYPS1"));
// Finish the shell, if Ctrl+D is pressed
if(fgets(cmd, 100, stdin) == NULL){
kill_jobs_and_free_memory(jobs);
exit(0);
}
len = strlen(cmd);
cmd[--len] = '\0';
// Ignore empty cmds
if(len == 0) continue;
// Add cmd to .history file
_add_history(cmd, hpath);
// Finish sheel, when 'exit' command
if (strncmp(cmd, "exit", 4) == 0){
kill_jobs_and_free_memory(jobs);
exit(0);
}
// Check background execution
if(cmd[len-1] == '&'){
cmd[--len] = '\0';
bg = true;
}else{
bg = false;
}
// Create empty linked list to parse commands
cnode *cmds, *ptr;
cmds = NULL;
// Parse commands and insert it on a Linked List
char * _cmd;
strcpy(cmd_aux, cmd);
_cmd = strtok_r(cmd_aux, "|", &sptr1);
while(_cmd != NULL){
add_cmd(&cmds, _cmd);
_cmd = strtok_r(NULL, "|", &sptr1);
}
// Execute commands
int fdd = 0;
int fd[2];
ptr = cmds;
while(ptr != NULL){
// Create pipe between process
pipe(fd);
t_process = get_type_process(ptr->cmd);
pid = fork();
if(pid < 0){
perror("fork");
exit(0);
}else if(pid == 0){
//Child
//Signals
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
signal(SIGTTIN, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
signal(SIGCHLD, SIG_DFL);
//PGID
setpgid(0, pid);
// Child process gets input from output of previous process
// if exists, else it gets input from stdin
dup2(fdd, 0);
// If there is a next process, change it output to the pipe
// between them, else output is stdout
if(ptr->prox != NULL){
dup2(fd[1], 1);
}
// Close reading pipe
close(fd[0]);
// Arguments parser
if(parse_args(ptr->cmd, args, t_process) < 0){
perror(args[0]);
exit(0);
}
// Process command
switch(t_process){
case 1:
_export(args, shmem);
break;
case 2:
_cd(args, shmem);
break;
case 3:
_history(hpath);
break;
case 4:
_kill(args, shmem);
break;
case 5:
_jobs(jobs);
break;
case 6:
_fg(args, shmem);
break;
case 7:
_bg(args, shmem);
break;
case 8:
_echo(args);
break;
case 9:
_set();
break;
case 0:
if(execv(args[0], args) < 0){
perror(args[0]);
exit(0);
}
break;
}
}else{
//Parent
//SIGNALS
signal(SIGCHLD, handle_sigchld);
//PGID
setpgid(pid, pid);
if(bg){
// Background
insert(&jobs, pid, 1);
fprintf(stdout, "[%d]\n", pid);
}else{
// Foreground
// Send job to fg
tcsetpgrp(0, pid);
// Wait it finish
int status;
waitpid(pid, &status, WUNTRACED);
// return shell to fg
signal(SIGTTOU, SIG_IGN);
tcsetpgrp(0, getpid());
signal(SIGTTOU, SIG_DFL);
// Close writing pipe
close(fd[1]);
// Save the actual pipe for the next command parsed
fdd = fd[0];
if(WIFEXITED(status)){
if(t_process == 1){
// export
char* s = (char*) shmem;
int sz = strlen(s);
int i = 0, j = 0;
for(; i < sz && s[i] != '='; i++){
aux[i] = s[i];
}
aux[i++] = '\0';
for(; i < sz; i++, j++){
aux2[j] = s[i];
}
aux2[j] = '\0';
if(setenv(aux, aux2, 1) < 0){
perror("export");
}
}else if(t_process == 2){
// cd
if(chdir(shmem) != -1){
setenv("PWD", shmem, 1);
}else{
perror("cd");
}
}else if(t_process == 4){
// kill
int pid_sig, sig;
sscanf(shmem, "%d %d", &pid_sig, &sig);
if(kill(pid_sig, sig) < 0){
perror("kill");
}
sleep(1);
}else if(t_process == 6){
// fg
int pid_fg;
sscanf(shmem, "%d", &pid_fg);
if(kill(pid_fg, SIGCONT) == 0){
del(&jobs, pid_fg);
// Send job to fg
tcsetpgrp(0, pid_fg);
// wait it finish
waitpid(pid_fg, &status, WUNTRACED);
if(WIFSTOPPED(status)){
fprintf(stdout, "PID [%d] stopped\n", pid_fg);
insert(&jobs, pid_fg, 0);
if(pid_changed == pid_fg){
pid_changed = pid_action = -1;
}
}
// Return shell to fg
signal(SIGTTOU, SIG_IGN);
tcsetpgrp(0, getpid());
signal(SIGTTOU, SIG_DFL);
}else{
perror("fg");
}
sleep(1);
}else if(t_process == 7){
// bg
int pid_bg;
sscanf(shmem, "%d", &pid_bg);
if(kill(pid_bg, SIGCONT) == 0){
update_status(&jobs, pid_bg, 1);
}else{
perror("bg");
}
sleep(1);
}
}else if(WIFSTOPPED(status)){
fprintf(stdout, "PID [%d] stopped\n", pid);
insert(&jobs, pid, 0);
}
}
// Go to next command parsed
ptr = ptr->prox;
}
}
clear_cmd(cmds);
}
return 0;
}