-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDupShell.c
167 lines (147 loc) · 3.75 KB
/
DupShell.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
/*****************************************************************************************************************
*
* NAME : VINU CHARANYA ATHANGUDI PURUSHOTHAMAN
* PERSON # : 37648013
* PROGRAM 8 : DupShell.c
* HEADER FILE : osheaders.h
*
* DESCRIPTION : Program Forks a child to execute commands with arguments like a UNIX Shell.
* It forks child processes for every input to the MoreShell>.
* Using execlp, it executes the command array to run the given shell command
*
* EXECUTION : ./DupShell
*
* DATE : 13 FEB 2011
*
******************************************************************************************************************/
#include "osheaders.h"
char inp[25];
char *cmd1[5];
char *cmd2[5];
int f;
int mypipe[2];
/*****************************************************************************************************************
*
* Function : prompt
*
* Parameters : NONE
*
* Description : It displays the prompt 'MoreShell>' and gets the input from the user and calls parseLine
* It parses if the given command has a pipe and pass it to parseline as seperate arguments.
*
*
******************************************************************************************************************/
static void prompt()
{
printf("\nDupShell > ");
fgets(inp,25,stdin);
inp[strlen(inp)-1]='\0';
int count =0;
char line1[5];
if(strchr(inp,'|')!=NULL)
{
f=1;
char *t[2];
char *p;
p = strtok(inp, "|");
while (p) {
t[count] = p;
count++;
p = strtok(NULL,"|");
}
parseline(t[0],cmd1);
parseline(t[1],cmd2);
}
else
{
f=0;
parseline(inp,cmd1);
}
}
/*****************************************************************************************************************
*
* Function : parseline
*
* Parameters : <line> which is the input given by the user at MoreShell.
*
* Description : It parses the input and stores it in the cmd[]
*
*
******************************************************************************************************************/
int parseline(char * line , char * cmd[]) {
char * p;
int count = 0;
p = strtok(line, " ");
while (p && strcmp(p,"|") != 0) {
cmd[count] = p;
count++;
p = strtok(NULL," ");
}
cmd[count]=NULL;
return count;
}
/*****************************************************************************************************************
*
* Function : Main
*
* Parameters : NONE
*
* Description : It forks two child process and uses pipe to execute command lines connected with pipes.
* It uses dup2 to redirect IO.
*
*
*
******************************************************************************************************************/
int main(int argc,char **argv)
{
prompt();
pid_t ForkPID,ForkPID1;
while(strcmp(inp,"exit")!=0)
{
if (pipe(mypipe)) {
printf("\n Sorry, Your pipe Failed \n");
return (0);
}
ForkPID = fork();
switch (ForkPID) {
case -1: printf("Error: Failed to fork.\n"); break;
case 0:
if(f==1)
{
dup2(mypipe[1],STDOUT_FILENO);//this command redirects the outpue of the executed command into the pipe
close(mypipe[0]);
close(mypipe[1]);
}
if (execvp(cmd1[0],cmd1) == -1) {
printf("Error: running command: '%s'\n",cmd1[0]);
exit(0);
}
exit(0);
break;
default:
ForkPID1 = fork();
switch (ForkPID1) {
case -1: printf("Error: Failed to fork.\n"); break;
case 0:
dup2(mypipe[0],STDIN_FILENO); //this command reads from the pipe and execvp executes it
close(mypipe[0]);
close(mypipe[1]);
if(f==1)
{
if (execvp(cmd2[0],cmd2) == -1) {
printf("Error: running command: '%s'\n",cmd2[0]);
exit(0);
}
}
exit(0);
break;
default:
close(mypipe[0]);
close(mypipe[1]);
waitpid(ForkPID,NULL,0);
waitpid(ForkPID1,NULL,0);
prompt();
}
}
}
}