-
Notifications
You must be signed in to change notification settings - Fork 3
/
redirection.c
155 lines (133 loc) · 3.59 KB
/
redirection.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
#include "main.h"
// Permissions we want to give to the output file
#define PERMISSIONS 0644
// This is the function for redirection
int redirectionHandler(ll totalArgsInEachCommand, char *listOfArgs[]) {
char inputFile[10000], outputFile[10000];
int position1 = 0, position2 = 0, position3 = 0;
int inputDup, outputDup;
int pos = 100;
int inputfd, outputfd;
// Check for the first positions of all the <, >, >>.
for(int i = 0; i < totalArgsInEachCommand; i++) {
if(strcmp(listOfArgs[i], "<") == 0) position1 = i;
if(strcmp(listOfArgs[i], ">") == 0) position2 = i;
if(strcmp(listOfArgs[i], ">>") == 0) position3 = i;
}
// Case - 1
if(position1 != 0) {
strcpy(inputFile, listOfArgs[position1 + 1]);
pos = position1;
listOfArgs[pos] = NULL;
struct stat tmp;
if(stat(inputFile, &tmp) < 0) {
perror("");
return -1;
}
// Storing the input file
inputDup = dup(STDIN_FILENO);
if (inputDup < 0) {
perror("Dup ");
return -1;
}
// Open the input file
inputfd = open(inputFile, O_RDONLY, PERMISSIONS);
if (inputfd < 0) {
printf("File doesn't exist!\n");
return -1;
}
if (dup2(inputfd, STDIN_FILENO)) {
perror("Dup2 ");
return -1;
}
}
// Case - 2
if(position2 != 0 || position3 != 0) {
if(position2 + position3 <= pos)
pos = position2 + position3;
strcpy(outputFile, listOfArgs[position2 + position3 + 1]);
listOfArgs[pos] = NULL;
outputDup = dup(STDOUT_FILENO);
if (outputDup < 0) {
perror("Dup ");
return -1;
}
if(position2 != 0)
outputfd = open(outputFile, O_WRONLY | O_CREAT | O_TRUNC, PERMISSIONS);
else
outputfd = open(outputFile, O_WRONLY | O_CREAT | O_APPEND, PERMISSIONS);
if (outputfd < 0) {
printf("File doesn't exist!\n");
return -1;
}
if(dup2(outputfd, STDOUT_FILENO) < 0)
{
perror("Dup2 ");
return -1;
}
}
// if(position1 != 0) {
// struct stat tmp;
// if(stat(inputFile, &tmp) < 0)
// perror("");
// inputDup = dup(STDIN_FILENO);
// if (inputDup < 0) {
// perror("Dup ");
// exit(1);
// }
// inputfd = open(inputFile, O_RDONLY, PERMISSIONS);
// if (dup2(inputfd, STDIN_FILENO)) {
// perror("Dup2 ");
// exit(1);
// }
// }
// if(position2 != 0 || position3 != 0)
// {
// outputDup = dup(STDOUT_FILENO);
// if (outputDup < 0) {
// perror("Dup ");
// exit(1);
// }
// if(position2 != 0)
// outputfd = open(outputFile, O_WRONLY | O_CREAT | O_TRUNC, PERMISSIONS);
// else
// outputfd = open(outputFile, O_WRONLY | O_CREAT | O_APPEND, PERMISSIONS);
// if(dup2(outputfd, STDOUT_FILENO) < 0)
// {
// perror("Dup2 ");
// exit(1);
// }
// }
// Forking the process
pid_t pidValue;
pidValue = fork();
if(pidValue < 0) {
close(outputfd);
perror("Fork ");
return -1;
}
if(pidValue == 0) {
if(execvp(listOfArgs[0], listOfArgs) < 0) {
perror("Execvp ");
return -1;
}
}
else {
wait(NULL);
dup2(inputDup, STDIN_FILENO);
dup2(outputDup, STDOUT_FILENO);
}
return 0;
}
// This function checks if redirection is possible
int checkRedirection(ll totalArgsInEachCommand, char *listOfArgs[]) {
for (int i = 0; i < totalArgsInEachCommand; i++) {
if (strcmp(listOfArgs[i], "<") == 0)
return 1;
else if(strcmp(listOfArgs[i], ">") == 0)
return 1;
else if(strcmp(listOfArgs[i], ">>") == 0 )
return 1;
}
return 0;
}