-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
127 lines (91 loc) · 2.69 KB
/
main.c
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
#include "shell.h"
#include <stdbool.h>
#include <signal.h>
static void handler(int sig){
int pid=atoi(getenv("pid_father"));
pid_t pid_cur=getpid();
if(pid==pid_cur)return;
if(pid_cur!=pid){
kill(pid_cur,SIGSTOP);
}
}
static void sigttin_hand(int sig){
//printf("%d\n",getpid());
kill(getpid(),SIGSTOP);
}
static void sigusr_hand(int sig){
int fdnull =open("/dev/null",O_RDONLY);
dup2(fdnull,STDIN_FILENO);
}
static void sigint_hand(int sig){
int pid=atoi(getenv("pid_father"));
pid_t pid_cur=getpid();
if(pid==pid_cur)return;
if(pid_cur!=pid){
kill(pid_cur,SIGKILL);
}
}
int main(int argc , char** argv){
struct sigaction back_stdin;
back_stdin.sa_handler=sigttin_hand;
back_stdin.sa_flags=SA_RESTART|SA_NOCLDWAIT|SA_NOCLDSTOP;
sigaction(SIGTTIN,&back_stdin,NULL);
sigaction(SIGTTOU,&back_stdin,NULL);
if(getenv("pid_father")==NULL){
int fd=open("pid_list",O_CREAT|O_TRUNC,0644);
//printf("%d\n",fd);
close(fd);
}
pid_t pid= getpid();
char s[10];
sprintf(s,"%d",pid);
setenv("pid_father",s,0);
struct sigaction background;
background.sa_handler=sigusr_hand;
background.sa_flags=SA_RESTART|SA_NOCLDWAIT|SA_NOCLDSTOP;
sigaction(SIGUSR1,&background,NULL);
struct sigaction ctrlz;
ctrlz.sa_handler=handler;
ctrlz.sa_flags =SA_RESTART|SA_NOCLDWAIT|SA_NOCLDSTOP;
sigaction(SIGTSTP,&ctrlz,NULL);
struct sigaction ctrlc;
ctrlc.sa_handler=sigint_hand;
ctrlc.sa_flags =SA_RESTART|SA_NOCLDWAIT|SA_NOCLDSTOP;
sigaction(SIGINT,&ctrlc,NULL);
char* line;
char* dir;
if((line=(char*)malloc(1024))==NULL)error("malloc failed",EXIT_FAILURE);
if(( dir=(char*)malloc(124))==NULL)error("malloc failed",EXIT_FAILURE);
int nb;
int script;
if((script=open(argv[1],O_RDONLY))){
exec_bash(script);
}
char*** argv_2;
while((nb=read(STDIN_FILENO,line,1024))>0){
if(nb<0){
perror("read");
exit(EXIT_FAILURE);
}
printf("\n");
char* in;
char *out;
if((in=(char*)malloc(100))==NULL)error("malloc failed",EXIT_FAILURE);
if(( out=(char*)malloc(100))==NULL)error("malloc failed",EXIT_FAILURE);
getcwd(dir,124);
printf(" \x1B[32m%s$:\x1B[0m\n ",dir);
line[nb-1]=0;
scan_and_replace('>',&line);
scan_and_replace('<',&line);
scan_and_replace('|',&line);
parse_line_pipes(line,&argv_2,&in,&out);
redir_cmd(argv_2,in,out);
free(in);
free(out);
free_argv_2(&argv_2);
}
free(dir);
free(line);
unsetenv("pid_father");
return EXIT_SUCCESS;
}