-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdexe.cpp
144 lines (129 loc) · 3.12 KB
/
cmdexe.cpp
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
#include<iostream>
#include<unistd.h>
#include<fcntl.h>
#include<vector>
#include<algorithm>
#include<iterator>
#include<string.h>
#include<string>
#include<cmath>
#include<stdio.h>
#include<sys/wait.h> /* uses for wait*/
#include "headers.h"
using namespace std;
/*********************command calling implementation**********/
void cmdcallimpl(int ith){
vector <char * > argv;
//cout<<"in cmd exe"<<endl;
for(vector<string>::iterator loop = vvs[ith].begin(); loop != vvs[ith].end(); ++loop){
argv.push_back(&(*loop)[0]);
}
argv.push_back(NULL);
if (strcmp(vvs[ith].front().c_str(), "pwd") == 0) {
write(1,pwdimpl(),strlen(pwdimpl()));
cout<<endl;
exit(0);
}
else if (strcmp(vvs[ith].front().c_str(), "fg") == 0) {
//cout<<"in cd"<<endl;
fgimpl();
exit(0);
}
else if (strcmp(vvs[ith].front().c_str(), "cd") == 0) {
//cout<<"in cd"<<endl;
cdimpl(vvs[ith]);
exit(0);
}
else if (strcmp(vvs[ith].front().c_str(), "history") == 0) {
//cout<<"in history"<<endl;
historyimpl(vvs[ith]);
exit(0);
}
else if (strcmp(vvs[ith].front().c_str(), "echo") == 0) {
//cout<<"in echo"<<endl;
echoimpl(vvs[ith]);
exit(0);
}
else if (strcmp(vvs[ith].front().c_str(), "export") == 0) {
//cout<<"in export"<<endl;
exportimpl(vvs[ith]);
exit(0);
}
else if ((vvs[ith].front().at(0) == '!') && vvs[ith].front().length() >=2) {
cout<<vvs[ith].front()<<endl;
//cout<<"in bang"<<endl;
bangparser(vvs[ith].front().c_str());
exit(0);
}
else if (vvs[ith].front().find("=") != string::npos) {
localimpl(vvs[ith]);
exit(0);
}
else{
execvp (vvs[ith].front().c_str(), &argv[0]);
perror("execvp");
exit(EXIT_FAILURE);
}
}
/******************************* implementation*******************************/
int spawn_proc (int in, int out,int ith)
{
pid_t pid1;
if ((pid1 = fork ()) == 0)
{
if (in != 0)
{
dup2 (in, 0);
close (in);
}
if (out != 1)
{
dup2 (out, 1);
close (out);
}
cmdcallimpl(ith);
}
return pid1;
}
int fork_pipes(){
int i;
pid_t pid;
int in, out,fd [2];
in = 0;
if(inputflag ==1){
in=inputfd;
}
//close(inputfd);
for (i = 0; i < vvs.size()-1; ++i)
{
pipe (fd);
spawn_proc (in, fd [1],i);
close (fd [1]);
in = fd [0];
}
if (in != 0)
dup2 (in, 0);
if (outputflag == 1){
dup2 (outputfd, 1);
//close (outputfd);
}
cmdcallimpl(i);
}
void execute()
{
pid_t pid;
int status;
if ((pid = fork()) < 0) { /* fork a child process */
printf("*** ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0) { /* for the child process: */
fork_pipes();
}
else {
if(bcgroundflag == 0) /* for the parent: */
while (wait(&status) != pid); /* wait for completion */
//else
//setpgid(0, 0);
}
}