-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.cpp
More file actions
183 lines (177 loc) · 4.7 KB
/
Copy pathfunc.cpp
File metadata and controls
183 lines (177 loc) · 4.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
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
#include <iostream>
#include <string>
#include "func.hpp"
#include <chrono>
#include <ctime>
#include <sys/wait.h>
#include <iostream>
#include <unistd.h>
#include <cstring>
#include <errno.h>
#include <error.h>
#include <stdio.h>
#include <stdlib.h>
std::vector<std::string> tokenize(std::string cmd)
{
std::vector<std::string> tokenCmd;
int length = cmd.length();
std::vector<char> tempCmd;
bool hasCmd = false; // this is to allow more than 1 space between commands
for(int i = 0; i <= length; i++)
{
if(cmd[i] == ' '|| i == length) // the last iteration stores what ever is left in cmd
{
if(hasCmd) // this will add the command to the vector
{
std::string newCmd(tempCmd.begin(), tempCmd.end());
tokenCmd.push_back(newCmd);
hasCmd = false;
tempCmd.clear();
}
}
else
{
tempCmd.push_back(cmd[i]);
hasCmd = true;
}
}
return tokenCmd;
}
void printHistory(std::vector<std::string> history)
{
for(int i = 0; i < history.size(); i++)
{
std::cout << history[i] << std::endl;
}
}
void printHistory(std::vector<std::string> history, std::vector<std::string> tokenCmd)
{
if (tokenCmd.size() > 1)
{
int i = stoi(tokenCmd[1]);
i--;
std::cout << history[i] << std::endl;
}
else
{
std::cout << "Please specify history index like so '^ int' " << std::endl;
}
}
std::chrono::duration<double> forkExec(std::vector<std::string> tokenCmd, std::chrono::duration<double> ptime)
{
auto start = std::chrono::system_clock::now();
if(fork())
{
// parent waits for the child to finish
int status;
wait(&status);
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
ptime += elapsed_seconds;
}
else
{
int numArgs = tokenCmd.size();
char** args = new char*[numArgs];
for(int i = 0; i < numArgs; i++)
{
args[i] = strdup(tokenCmd[i].c_str());
}
args[numArgs] = NULL; //// execvp needs a null at the back.
//child, executes the user's input as a command
execvp(args[0], args);
//note if the command is successfully found the child will never
//execute the following error message
//command not found, or similar errors
std::cerr << args[0] << " did something wrong" << std::endl;
exit(1);
delete[] args;
}
return ptime;
}
std::chrono::duration<double> pipeForkExec(std::vector<std::string> tokenCmdL, std::vector<std::string> tokenCmdR,std::chrono::duration<double> ptime)
{
auto start = std::chrono::system_clock::now();
int p[2];
pipe(p);
if(fork()==0)
{
// this is the first child
// 0) read in the source feor this file and print it to the pipe
close(p[READ]);
dup2(p[WRITE],STDOUT_FILENO);
int numArgsL = tokenCmdL.size();
char** argsL = new char*[numArgsL];
for(int i = 0; i < numArgsL; i++)
{
argsL[i] = strdup(tokenCmdL[i].c_str());
}
argsL[numArgsL] = NULL; //// execvp needs a null at the back.
//child, executes the user's input as a command
execvp(argsL[0], argsL);
//note if the command is successfully found the child will never
//execute the following error message
//command not found, or similar errors
std::cerr << argsL[0] << " did something wrong" << std::endl;
exit(1);
delete[] argsL;
}
if(fork()==0)
{
// 2nd child
close(p[WRITE]);
dup2(p[READ], STDIN_FILENO);
int numArgsR = tokenCmdR.size();
char** argsR = new char*[numArgsR];
for(int i = 0; i < numArgsR; i++)
{
argsR[i] = strdup(tokenCmdR[i].c_str());
}
argsR[numArgsR] = NULL; //// execvp needs a null at the back.
//child, executes the user's input as a command
execvp(argsR[0], argsR);
//note if the command is successfully found the child will never
//execute the following error message
//command not found, or similar errors
std::cerr << argsR[0] << " did something wrong" << std::endl;
exit(1);
delete[] argsR;
}
close(p[READ]);
close(p[WRITE]);
int wstatus;
int kids = 2;
while (kids >0)
{
pid_t kiddo = waitpid(-1, &wstatus, 0);
std::cout << "Child proc " << kiddo << " exited with status " << wstatus << std::endl;
kids--;
}
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
return ptime;
}
bool check4Pipe(std::vector<std::string> tokenCmd)
{
bool isPipe = false;
for (int i = 0; i < tokenCmd.size(); i++)
{
if(tokenCmd[i] == "|")
{
isPipe = true;
}
}
return isPipe;
}
int getPipeIndex(std::vector<std::string> tokenCmd)
{
int pipeIndex;
for (int i = 0; i < tokenCmd.size(); i++)
{
if(tokenCmd[i] == "|")
{
pipeIndex = i;
}
}
return pipeIndex;
}