-
Notifications
You must be signed in to change notification settings - Fork 0
/
shellfunc.c
123 lines (122 loc) · 2.82 KB
/
shellfunc.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
void shellfunc(int arg_c,char *args[], int inp, int op, int pipe_flag)
{
int status;
pid_t child;
int exec_ret = 0;
if(inp>=0 || op>=0 || pipe_flag == 1){
fflush(stdout);
child = fork();
fflush(stdout);
if(child<0){
char error[150]="Not enough space for a new process.";
write(1,error,150);
}
else if(child!=0){
waitpid(child, &status, 0);
}
else{
char *new_args[arg_c+2];
new_args[0]="io";
int i=0;
for(i=0;i<arg_c+1;i++){
new_args[i+1]=args[i];
}
new_args[arg_c+1]==NULL;
fflush(stdout);
exec_ret=execve(new_args[0],new_args,NULL);
fflush(stdout);
exit(3);
}
}
else if((strcmp(args[0],"checkcpupercentage")==0)||(strcmp(args[0],"checkresidentmemory")==0)||(strcmp(args[0],"sortFile")==0)||(strcmp(args[0],"executeCommands")==0))
{
if(arg_c!=2){
printf("Illegal command or arguements\n");
return;
}
fflush(stdout);
child = fork();
fflush(stdout);
if(child<0){
char error[150]="Not enough space for a new process.";
write(1,error,150);
}
else if(child!=0){
waitpid(child, &status, 0);
}
else{
fflush(stdout);
exec_ret=execve(args[0],args,NULL);
fflush(stdout);
exit(3);
}
}
else if(strcmp(args[0],"listFiles")==0)
{
if(arg_c!=1){
printf("Illegal command or arguements\n");
return;
}
fflush(stdout);
child = fork();
fflush(stdout);
if(child<0){
char error[150]="Not enough space for a new process.";
write(1,error,150);
}
else if(child!=0){
waitpid(child, &status, 0);
}
else{
fflush(stdout);
exec_ret=execve(args[0],args,NULL);
fflush(stdout);
exit(3);
}
}
else{
fflush(stdout);
child = fork();
fflush(stdout);
if(child<0){
char error[150]="Not enough space for a new process.";
write(1,error,150);
}
else if(child!=0){
waitpid(child, &status, 0);
}
else{
fflush(stdout);
exec_ret=execve(args[0],args,NULL);
fflush(stdout);
if(exec_ret==-1){
if(strcmp(args[0],"grep")==0){
int q=0, need_trim=0;
while(args[1][q]!='\0'){
q++;
}
if(((args[1][0]=='\"')&&(args[1][q-1]=='\"'))||((args[1][0]=='\'')&&(args[1][q-1]=='\''))){
need_trim=1;
}
if(need_trim){
char trim[q-1];
int r=0;
while(r<q-2){
trim[r]=args[1][r+1];
r++;
}
trim[q-2]='\0';
strcpy(args[1],trim);
}
}
fflush(stdout);
exec_ret=execvp(args[0],args);
fflush(stdout);
}
if(exec_ret==-1){
printf("Illegal command or arguements\n");
exit(3);
}
}
}
}