-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirection.c
More file actions
151 lines (151 loc) · 3.98 KB
/
Copy pathredirection.c
File metadata and controls
151 lines (151 loc) · 3.98 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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/utsname.h>
#include<unistd.h>
#include<dirent.h>
#include<fcntl.h>
#include<limits.h>
#include<signal.h>
#include<errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<sys/wait.h>
void redirect(void ** split,int input,int output,char * p,char * hl){
int first,append=0;// =0 for input and =1 for output
if(strcmp(split[output],">>\n")==0 || strcmp(split[output],">>")==0){
append=1;
}
if(input < output && input!=0 || output == 0){
first=0;
}
else if(output < input && output!=0 || input == 0){
first=1;
}
char ** function1=(char **)malloc(sizeof(char *)*10);
char ** function2=(char **)malloc(sizeof(char *)*10);
char ** function3=(char **)malloc(sizeof(char *)*10);
for(int i=0;i<10;i++){
function1[i]=(char *)malloc(sizeof(char));
function2[i]=(char *)malloc(sizeof(char));
function3[i]=(char *)malloc(sizeof(char));
}
int arr[2];
if(input<output){
arr[0]=input;
arr[1]=output;
}
else{
arr[0]=output;
arr[1]=input;
}
if(arr[0]==0){
arr[0]=arr[1];
arr[1]=9;
}
int temp1=0,temp2=0,temp3=0;
for(int i=0;i<10;i++){
if(i<arr[0]){
strcpy(function1[temp1++],split[i]);
}
else if(arr[0]<i && i<arr[1]){
strcpy(function2[temp2++],split[i]);
}
else if(i>arr[1]){
strcpy(function3[temp3++],split[i]);
}
}
for(int i=temp1;i<10;i++){
strcpy(function1[i],"");
}
for(int i=temp2;i<10;i++){
strcpy(function2[i],"");
}
for(int i=temp3;i<10;i++){
strcpy(function3[i],"");
}
if(first==1 && input==0){
int out;
if(append==0)
out = open(function2[0] , O_WRONLY | O_CREAT | O_TRUNC, 0644);
else
out = open(function2[0] , O_APPEND|O_WRONLY | O_CREAT , 0644);
if(strcmp(function1[0],"cd")==0||strcmp(function1[0],"cd\n")==0){
changeDIR(function1[1],p);
return;
}
int pid = fork();
if(pid==0){
dup2(out,1);
execute(function1);
exit(0);
}
else{
wait(NULL);
}
}
else if(first==0 && output==0){
if(strcmp(function1[0],"cd")==0||strcmp(function1[0],"cd\n")==0){
changeDIR(function1[1],p);
return;
}
int in = open(function2[0] , O_RDONLY);
if(in<0){
perror("");
return;
}
int pid = fork();
if(pid==0){
dup2(in,0);
execute(function1);
exit(0);
}
else{
wait(NULL);
}
}
else if(first==1 && input>0){
int out;
if(append==0)
out = open(function2[0] , O_WRONLY | O_CREAT | O_TRUNC, 0644);
else
out = open(function2[0] , O_APPEND | O_WRONLY | O_CREAT , 0644);
int in = open(function3[0] , O_RDONLY);
if(strcmp(function1[0],"cd")==0||strcmp(function1[0],"cd\n")==0){
changeDIR(function1[1],p);
return;
}
int pid = fork();
if(pid==0){
dup2(out,1);
execute(function1);
exit(0);
}
else{
wait(NULL);
}
}
else if(first==0 && output>0){
int in = open(function2[0] , O_RDONLY);
int out;
if(append==0)
out = open(function3[0] , O_WRONLY | O_CREAT | O_TRUNC, 0644);
else
out = open(function3[0] , O_APPEND| O_WRONLY | O_CREAT , 0644);
if(strcmp(function1[0],"cd")==0||strcmp(function1[0],"cd\n")==0){
changeDIR(function1[1],p);
return;
}
int pid = fork();
if(pid==0){
dup2(in,0);
dup2(out,1);
execute(function1);
exit(1);
}
else{
wait(NULL);
}
}
}