-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirections.cpp
183 lines (161 loc) Β· 4.52 KB
/
redirections.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
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
//PANAGIOTIS KONTOEIDIS
//1115201900266
#include "libs.h"
tokens redirections_pipes_in_cmd(tokens parameters) // tokenize down given tokens from the parameters and finds hidden >,<, | chars
{
tokens new_parameters;
int flag = 0;
string temp;
if(parameters.size()==0){
return parameters;
}
for (int i = 0; i < parameters.size(); i++)
{
temp.clear();
flag = 0;
if (strcmp(parameters[i], ">") == 0 || strcmp(parameters[i], "<") == 0 || strcmp(parameters[i], ">>") == 0 || strcmp(parameters[i], "|")==0)
{
new_parameters.push_back(parameters[i]);
continue;
}
for (int a = 0; a < strlen(parameters[i]); a++)
{
if (parameters[i][a] == '>')
{
if (temp.size() != 0)
{
char * p = new char[temp.size()];
strcpy(p, temp.c_str());
new_parameters.push_back(p);
temp.clear();
}
char * sc = new char[1];
strcpy(sc, ">");
new_parameters.push_back(sc);
flag = 1;
}
else if (parameters[i][a] == '<')
{
if (temp.size() != 0)
{
char * p = new char[temp.size()+1];
strcpy(p, temp.c_str());
new_parameters.push_back(p);
temp.clear();
}
char * sc = new char[1];
strcpy(sc, "<");
new_parameters.push_back(sc);
flag = 1;
}
else if (parameters[i][a] == '|')
{
if (temp.size() != 0)
{
char *p = new char[temp.size() + 1];
strcpy(p, temp.c_str());
new_parameters.push_back(p);
temp.clear();
}
char *sc = new char[1];
strcpy(sc, "|");
new_parameters.push_back(sc);
flag = 1;
}
else
{
temp.push_back(parameters[i][a]);
}
}
if (flag == 1) // edw shmainei oti egine allagh
{
char *p = new char[temp.size() + 1];
strcpy(p, temp.c_str());
new_parameters.push_back(p);
temp.clear();
continue;
}
else
{ // an den egine kamia allagh sto string
if(strlen(parameters[i])!=0)
new_parameters.push_back(parameters[i]);
}
}
for (auto it = new_parameters.begin(); it != new_parameters.end(); ) { //delete all blank vector elements
if (strlen(*it) == 0) {
it = new_parameters.erase(it);
} else {
++it;
}
}
return new_parameters;
}
void call_for_redirection(tokens command) // call for redirections
{
for (int i = 0; i < command.size(); i++)
{
if (strcmp(command[i], ">") == 0)
{
rpos.push_back(i);
redirection_flag++;
output_redirection(command[i + 1]);
}
else if (strcmp(command[i], "<") == 0)
{
rpos.push_back(i);
redirection_flag++;
input_redirection(command[i + 1]);
}
else if (strcmp(command[i], ">>") == 0)
{
rpos.push_back(i);
redirection_flag++;
append_redirection(command[i + 1]);
}
}
}
void output_redirection(const char *output_file) // >
{
int fd = open(output_file, O_CREAT | O_WRONLY, 0666);
if (fd == -1)
{
cout<<"error with open"<<endl;
exit(-1);
}
if (dup2(fd, STDOUT_FILENO) == -1)
{
cout<<"error with dup2"<<endl;
exit(-1);
}
close(fd);
}
void input_redirection(const char *output_file) // <
{
int fd = open(output_file, O_RDONLY);
if (fd == -1)
{
cout<<"error with open"<<endl;
exit(-1);
}
if (dup2(fd, STDIN_FILENO) == -1)
{
cout<<"error with dup2"<<endl;
exit(-1);
}
close(fd);
}
void append_redirection(const char *output_file) // >>
{
int fd = open(output_file, O_CREAT | O_WRONLY | O_APPEND, 0666);
if (fd == -1)
{
cout<<"error with open"<<endl;
exit(-1);
}
if (dup2(fd, STDOUT_FILENO) == -1)
{
cout<<"error with dup2"<<endl;
exit(-1);
}
close(fd);
}