-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
241 lines (213 loc) · 4.74 KB
/
shell.c
File metadata and controls
241 lines (213 loc) · 4.74 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
231
232
233
234
235
236
237
238
239
240
241
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "header.h"
#define MAX_PAR 10 //max number of parameters
#define MAX_CMD_LENGTH 100 //max length of the command
char * tempcommand;
int redirect=0;
void handler()//handler function
{
pid_t pid;
int status;
if(pid=waitpid(-1,&status,WNOHANG)>0)
if(WIFEXITED(status))
printf("program %d terminated successfully\n",pid);
}
int main()
{
char * pwd="~";//relative current directory
while(1)
{
char * username=getenv("USER");//user of the system
char * home=getenv("PWD");//calling directory
printf("%s@UBUNTU %s >",username,pwd); //prints the prompt
char command[MAX_CMD_LENGTH];//stores the command
scanf("%[^\n]s",command);//reads the entire command
tempcommand=strdup(command);
char * indv_command[MAX_PAR];
int iccount=0;
char *token;
token=strtok(command,";");
redirect=0;
while(token!=NULL)
{
indv_command[iccount]=strdup(token);
iccount++;
token=strtok(NULL,";");
}
int i=0;
for(i=0;i<iccount;i++)
{
char * pipe_command[MAX_PAR];
int pccount=0;
char *token1;
token1=strtok(indv_command[i],"|");
while(token1!=NULL)
{
pipe_command[pccount]=strdup(token1);
pccount++;
token1=strtok(NULL,"|");
}
pipe_command[pccount]=NULL;
redirect=check_redirect(pipe_command[0]);
int index=0;
int bg_flag=0;
while(argv[index]!=NULL)
{
index++;
}
if(strcmp(argv[index-1],"&")==0)//if the last argument is &
{
argv[index-1]=NULL;//making the arg null
bg_flag=1;//background process wanted
}
else if(argv[index-1][strlen(argv[index-1])-1]=='&')//if the last character of the arg is &
{
argv[index-1]=strndup(argv[index-1],strlen(argv[index-1])-1);//delete the &
bg_flag=1;//background process wanted
}
if(bg_flag==0)//if background process is not wanted
argv[index]=NULL;
if (strcmp(argv[0],"exit")==0)//if the command is exit
{
printf("exiting the program\n");
exit(0);
}
else if(strcmp(argv[0],"cd")==0)//if the command is cd
{
char temppwd[MAX_CMD_LENGTH];
getcwd(temppwd,MAX_CMD_LENGTH);//gets the absolute current directory
if (argv[1]==NULL)//if change directory to home
{
chdir(home);//changes to relative home directory
pwd="~";
}
else if(strcmp(argv[1],"~")==0)
{
chdir(home);
pwd="~";
}
else if (strcmp(home,temppwd)==0 && strcmp(argv[1],"..")==0)//change to parent directory of home then change to root
{
chdir("/");
pwd="/";
}
else if(chdir(argv[1])==0)//otherwise
{
getcwd(temppwd,MAX_CMD_LENGTH);
if(strcmp(temppwd,home)!=0)//compares if the new directory is not home directory
{
pwd=strdup(temppwd);
pwd=pwd+strlen(home);//change the pwd
}
else
pwd="~";
}
else
perror("MY SHELL");
}
else if(strcmp(argv[0],"echo")==0)//echo command
{
char str[MAX_CMD_LENGTH]="";
int k=1;
while(argv[k]!=NULL)//til the arguments of echo are not null
{
strcat(str,argv[k]);//storing it as a string
k++;
}
int flag=-1;//to check the termination of ""
char input[1000]="";//final output to be printed
k=0;
int j;
for(j=0;j<strlen(str);j++)
{
if (str[j]=='\"')
flag*=-1;
else
{
input[k]=str[j];//stores the string except ""
k++;
}
}
while(flag==1)
{
printf(" > ");//if "" not terminated, generates the prompt
char c;
getchar();
while ((c=getchar())!='\n')
{
if(c=='\"')
flag*=-1;
else
{
input[k]=c;
k++;
}
}
}
printf("%s\n",input);//outputs the entire input
}
else if(strcmp(argv[0],"pwd")==0)//if command is pwd
{
printf("%s\n",pwd);//display the pwd
}
else//otherwise
{
int pid=fork();//create child process
if(pid<0)
printf("child process could not be created\n");
else if(pid==0)
{
if(pccount!=0)
{
piping(pipe_command,pccount);
}
else
{
int f1=0,f2=1;
if (redirect)
{
if(in!=NULL)
{
f1=open(in, O_RDONLY);
dup2(f1,0);
}
if(out!=NULL)
{
f2=open(out,O_WRONLY|O_CREAT,S_IRWXU);
dup2(f2,1);
}
}
if(f1!=0)
close(f1);
if(f2!=1)
close(f2);
signal(SIGCHLD,handler);
execvp(argv[0],argv);//execute command
}
}
else
{
printf("bg_flag: %d\n",bg_flag);
if(bg_flag==0)
{
waitpid(pid,0,0);
}
}
}
i=0;
int k;
for( k=0;k<MAX_PAR;k++)
argv[k]=NULL;
}
getchar();
}
return 0;
}