-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys_cmd.c
More file actions
80 lines (71 loc) · 1.7 KB
/
sys_cmd.c
File metadata and controls
80 lines (71 loc) · 1.7 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
#include "headers.h"
void sys_cmd(char * input_str)
{
long long int i,j,k,cnt,flag_and=0,child_pid,exe_stat,child_child_pid,pid;
// int status;
char * command[1000];
char * arg_list[1000];
char cmd[1000];
for(i=0;i<1000;i++)
{
arg_list[i] = (char*)malloc(sizeof(char)*1000);
}
char * token = strtok(input_str, " ");
cnt=0;
while (token!=NULL)
{
if(strcmp(token,"&")!=0)
{
arg_list[cnt]=token;
cnt++;
}
else
{
flag_and=1;
}
token = strtok(NULL," ");
}
// command = arg_list[0];
arg_list[cnt] = NULL;
// for(i=0;i<cnt;i++)
// {
// printf("%s\n",arg_list[i]);
// }
child_pid = fork();
if(child_pid<0)
{
perror("ERROR");
return;
}
if(child_pid==0)
{
exe_stat = execvp(arg_list[0],arg_list);
if(exe_stat==-1)
{
printf("Please enter a Valid Command\n");
}
exit(0);
}
if(child_pid>0)
{
if(flag_and==0)
{
int status;
foreground_job.pid = child_pid;
strcpy(foreground_job.command,arg_list[0]);
foreground_job.id = 0;
waitpid(child_pid,&status, WUNTRACED);
foreground_job.pid = -1;
}
else
{
printf("%s with process pid %lld started\n",arg_list[0],child_pid);
jobs[job_seq_no].pid = child_pid;
strcpy(jobs[job_seq_no].command,arg_list[0]);
jobs[job_seq_no].status = 1;
jobs[job_seq_no].id = job_seq_no;
job_seq_no++;
job_seq_no%=1000;
}
}
}