-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_cmd.c
More file actions
230 lines (224 loc) · 7.05 KB
/
exec_cmd.c
File metadata and controls
230 lines (224 loc) · 7.05 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
#include "exec_cmd.h"
#include<time.h>
// clock_t start=clock();
// void cronjob(char * cmd, int timer, int period){
// clock_t time_now;
// while(1){
// time_now=clock()-start;
// if(time_now==timer ||(time_now - timer)%period == 0){
// exec_cmd(cmd);
// }
// }
// }
void exec_cmd(char * cmd){
// printf("\n-------------------exec_CMD execution----------------\n");
// printf("||||%s||||",cmd);
char ** WORDS=calloc(MAX,sizeof(char));
WORDS=Parse(cmd," ");
char * det=WORDS[0];
int valid_cd = !(strcmp(det,"cd") * strcmp(det,"cd\n"));
int valid_echo = !(strcmp(det,"echo") * strcmp(det,"echo\n"));
int valid_ls= !(strcmp("ls",det) * strcmp(det,"ls\n"));
int valid_pwd= !(strcmp(det,"pwd") * strcmp(det,"pwd\n"));
int valid_pinfo = !(strcmp(det,"pinfo") * strcmp(det,"pinfo\n"));
int valid_quit = !(strcmp(det,"quit") * strcmp(det,"quit\n"));
int valid_setenv = ! (strcmp(det,"setenv")*strcmp(det,"setenv\n"));
int valid_unsetenv = !(strcmp(det,"unsetenv")*strcmp(det,"unsetenv\n"));
int valid_kjob=!(strcmp(det,"kjob")*strcmp(det,"kjob\n"));
int valid_jobs=!(strcmp(det,"jobs")*strcmp(det,"jobs\n"));
int valid_fg=!(strcmp(det,"fg")*strcmp(det,"fg\n"));
int valid_bg=!(strcmp(det,"bg")*strcmp(det,"bg\n"));
int valid_overkill=!(strcmp(det,"overkill")*strcmp(det,"overkill\n"));
int valid_cronjob= !(strcmp(det,"cronjob")*strcmp(det,"cronjob\n"));
if(valid_pwd){
char * WorkingDir=malloc(MAX*sizeof(char));
WorkingDir=pwd();
if(!WorkingDir){
printf("Error!\n");
}
else{
printf("%s\n",WorkingDir);
}
}
else if(valid_echo){
char * DIR=malloc(sizeof(MAX));
DIR=Savior_convert(WORDS,1,0);
echo(DIR);
}
else if(valid_cd){
char * To_dir=malloc(MAX*sizeof(char));
if(WORDS[1]){
cd(WORDS[1]);
}
else{
cd("");
}
}
else if(valid_ls){
char * DIR=malloc(sizeof(MAX)* sizeof(char));
DIR=Savior_convert(WORDS,1,0);
ls(DIR);
}
else if(valid_pinfo){
pinfo(WORDS[1]);
}
else if(valid_setenv){
if(WORDS[3] || !WORDS[1]){
printf("Incorrect Number of parameters passed.\n");
}
else if(WORDS[1] && WORDS[2]){
setenv(WORDS[1],WORDS[2],1);
}
else if(WORDS[1]){
setenv(WORDS[1],"",0);
}
}
else if(valid_unsetenv){
if(! WORDS[1] || WORDS[2]){
printf("Incorrect Number of parameters passed.\n");
}
else{
unsetenv(WORDS[1]);
}
}
else if(valid_kjob){
if(!WORDS[2] || WORDS[3]){
printf("Incorrect Number of parameters passed.\n");
}
else{
if(atoi(WORDS[1])-1 >= Job_pos){
printf("INVALID JOBID PASSED.\n");
} // ERROR HANDLING
else{
int te=kill(Jobs[atoi(WORDS[1])-1].PID,atoi(WORDS[2]));
if(te){
printf("Signal can't be sent. Job id or Signal is invalid");
}
else{
printf("DONE\n");
}
}
}
}
else if(valid_jobs){
// printf("BOII\n");
// maintain array of background processes, when you exit the background process, remove from array.
if(WORDS[2]){
printf("Error!\n");
}
else{
pid_t proc_id;
char path_status[MAX];
char buf[MAX];
char SOL[MAX];
char state;
int cnt=0;
char * temp=malloc(sizeof(char)*1000);
for(int x=0;x<Job_pos;x++){
proc_id=Jobs[x].PID;
sprintf(path_status, "/proc/%d/status", proc_id);
FILE * fp=fopen(path_status,"r");
if(fp){
cnt++;
fgets(buf,256,fp);
fgets(buf,256,fp); // go ahead, by 2 lines.
fgets(buf,256,fp);
state=buf[8];
sscanf(buf,"State:\t%c",&state); // from buf, read in the format givn as arg2, and store the reqd %c in &state.
if(state=='S'){
strcpy(SOL,"Stopped");
}
else{
strcpy(SOL,"Running");
}
// printf("||%c||",state); // UNABLE TO READ. BLANK
printf("[%d]%s %s [%d]\n",cnt,SOL,Jobs[x].S,Jobs[x].PID);
fclose(fp);
}
else{ // Dont list it out. As it is terminated.
;
}
}
}
}
else if(valid_fg){
if(WORDS[2] || !WORDS[1]){
printf("Incorrect number of parameters sent.\n");
}
else{
if(atoi(WORDS[1])-1 >= Job_pos){
printf("INVALID JOBID PASSED.\n");
} // ERROR HANDLING
else{
// REMAINING ??
}
}
}
else if(valid_bg){
if(WORDS[2] || !WORDS[1]){
printf("Incorrect number of parameters sent.\n");
}
else{
if(atoi(WORDS[1])-1 >= Job_pos){
printf("INVALID JOBID PASSED.\n");
} // ERROR HANDLING
else{
int te=kill(Jobs[atoi(WORDS[1])-1].PID,19);
if(te){
printf("JOB ID IS INVALID.\n");
}
}
}
}
else if(valid_overkill){
for(int x=0;x<Job_pos;x++){
kill(Jobs[x].PID,9);
}
}
else if(valid_quit){
// printf("|Happend|");
exit(0);
}
// else if(valid_cronjob){
// char * CMD=malloc(sizeof(char)* MAX);
// int timer;int period;
// if(!WORDS[6]){
// printf("Incorrect number of parameters passed.");
// }
// else{
// for(int it=1;WORDS[it];it++){
// if(strcmp(WORDS[it],"-c")==0){
// CMD=WORDS[it+1];
// }
// else if(strcmp(WORDS[it],"-t")==0){
// timer=atoi(WORDS[it+1]);
// }
// else if(strcmp(WORDS[it],"-p")==0){
// period=atoi(WORDS[it+1]);
// }
// else{
// printf("STRANGE PARSING ERROR HAS OCCURED.\n");
// }
// }
// printf("%s\n%d\n%d\n",CMD,timer,period);
// cronjob(CMD,timer,period);
// }
// }
else{
char * DIR=malloc(sizeof(MAX));
DIR=Savior_convert(WORDS,0,0);
for(int x=0;WORDS[x];x++)
{
if(!WORDS[x+1])
{
if(strcmp(WORDS[x],"&")==0)
{
background(DIR);
}
else{
foreground(DIR);
}
}
}
}
}