-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
178 lines (172 loc) · 3.92 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
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
#include "header.h"
back_info back_job[50];
int back_index=0;
int over_mark=0;
char *prompt;
//char **signum_to_name;
int flag;
char *input_str;
int back_mark=0;
int pos=0;
int ind=0;
int exit_status;
int position=0;
int backexit=0;
int in_re;
int out_re;
int append;
int shell_pgid;
char infile[1000];
char outfile[1000];
char **args;
char copy[500];
int st[1000001];
char home[1024];
char cd[3] = {'c', 'd', '\0'};
char exitt[5]={'e','x','i','t','\0'};
char over[9]={'o','v','e','r','k','i','l','l','\0'};
char echoo[5]={'e','c','h','o','\0'};
char jobss[5]={'j','o','b','s','\0'};
char pinfoo[6]={'p','i','n','f','o','\0'};
char pwdd[4]={'p','w','d','\0'};
char quitt[5]={'q','u','i','t','\0'};
char pathh[1000];
int main(int argc, char *argv[])
{
//struct rusage usage;
int status,i,l,j,ii,length,l1,l2,controld,start,end,bpid;
char *input_str;
char c;
int internal=0;
int job_no;
char **ncomms;
char **args_copy;
char prompt[1000];
char user[100],host[100];
pid_t pid;
controld=0;
//createsignal();
shell_pgid=getpid();
if (setpgid (shell_pgid, shell_pgid) < 0)
{
perror ("Couldn't put the shell in its own process group");
exit (1);
}
// handle sigint with hello
signal(SIGINT, SIG_IGN);
if(getcwd(home, sizeof(home))!=NULL)
{
while(1)
{
position=0;
backexit=0;
// back_index=0;
flag=0;
// printf("<prachi@LenovoG500s");
if(controld==0)
printprompt();
controld=0;
for(i=0;i<1000;i++)
prompt[i]='\0';
if(getcwd(prompt, sizeof(prompt))!=NULL)
{
input_str=get_input();
if(input_str[0]!='\0')
{i=0;
ncomms=split_input3(input_str,1);
position=ind;
while(position--)
{
// printf("pid: %d\n",shell_pgid);
internal=0;
// args contains the "pipe" parsed commands
args=split_input3(ncomms[i],2);
if(args[0]!="\0");
{
// check for built in commands
if(args[0][0]=='c' && args[0][1]=='d')
internal=1;
else if(args[0][0]=='e' && args[0][1]=='x' && args[0][2]=='i' && args[0][3]=='t')
internal=1;
else if(args[0][0]=='e' && args[0][1]=='c' && args[0][2]=='h' && args[0][3]=='o')
internal=1;
else if(args[0][0]=='p' && args[0][1]=='w' && args[0][2]=='d')
internal=1;
else if(args[0][0]=='q' && args[0][1]=='u' && args[0][2]=='i' && args[0][3]=='t')
internal=1;
else if(args[0][0]=='f' && args[0][1]=='g')
{
// printf("fg\n");
internal=-1;
}
if(internal!=1 && internal!=-1)
{
int len;
len=strlen(args[0]);
if(args[0][len-1]=='&')
back_mark=1;
pid=fork();
if(pid==0)
loop_pipe(args);
else if(pid<0)
perror("myshell");
else
{
if(back_mark!=1)
waitpid(pid,&status, WUNTRACED);
else
{
signal(SIGCHLD, childhandler);
strcpy(copy,args[0]);
len=strlen(copy);
copy[len-1]='\0';
strcpy(back_job[back_index].processname,copy);
// printf("args: %s",args[0]);
back_job[back_index].pro_id=pid;
back_job[back_index].back_active=1;
back_index++;
back_mark=0;
// storing background processes data(pid , name) into an array
printf("%d\n",pid);
}
}
}
else if(internal==1)
{
args=split_input2(args[0]);
// if built-in commands
execute_internal(flag);
}
else if(internal==-1)
{
// execute fg
pid_t fpid;
int status;
args=split_input2(args[0]);
fpid = execute_fg(atoi(args[1]));
// printf("fpid: %d\n",fpid);
if(fpid>0)
{
kill(fpid, SIGCONT);
waitpid(fpid,&status,WCONTINUED);
}
}
}
i++;
}
free(input_str);
free(args);
}
else
controld=1;
}
else
perror("myshell:");
}
}
else
{ perror("myshell:");
return EXIT_SUCCESS;
}
return 0;
}