-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.c
More file actions
129 lines (112 loc) · 2.56 KB
/
pipe.c
File metadata and controls
129 lines (112 loc) · 2.56 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
#include "header.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX_PAR 10 //max number of parameters
#define MAX_CMD_LENGTH 100 //max length of the command
extern int redirect;
void piping(char * pipe_command[],int count)
{
int i=0;
int fd[2];
int pipe_in=0;
pid_t pid;
for(i=0;i<count;i++)
{
if(i!=0)
redirect=check_redirect(pipe_command[i]);
int index=0;
while(argv[index]!=NULL)
{
index++;
}
if(strcmp(argv[index-1],"&")==0)//if the last argument is &
{
perror("wrong syntax near |\n");
}
else if(argv[index-1][strlen(argv[index-1])-1]=='&')
{
perror("wrong syntax near |\n");
}
dup2(pipe_in, 0); //change the input according to the old one
if (pipe_command[i+1] != NULL)
{
pipe(fd);
}
else
{
if(pipe_in!=0)
dup2(pipe_in,0);
int f1=0,f2=1;
if (redirect)
{
if(in!=NULL)
{
f1=open(in, O_RDONLY);
dup2(f1,0);
dup2(fd[1],1);
close(f1);
in=NULL;
}
if(out!=NULL)
{
f2=open(out,O_WRONLY|O_CREAT,S_IRWXU);
dup2(f2,1);
close(f2);
out=NULL;
}
}
execvp(argv[0],argv);
}
if ((pid = fork()) == -1)
{
exit(EXIT_FAILURE);
}
else if(pid==0)
{
if(!redirect)
{
if(fd[0]!=0)
{
dup2(pipe_in,0);
close(pipe_in);
}
if(fd[1]!=1)
{
dup2(fd[1],1);
close(fd[1]);
}
}
int f1=0,f2=1;
if (redirect)
{
if(in!=NULL)
{
f1=open(in, O_RDONLY);
dup2(f1,0);
dup2(fd[1],1);
close(f1);
}
if(out!=NULL)
{
f2=open(out,O_WRONLY|O_CREAT,S_IRWXU);
dup2(f2,1);
}
}
execvp(argv[0], argv);
exit(EXIT_FAILURE);
}
else
{
close(fd[1]);
wait(NULL);
pipe_in = fd[0]; //save the input for the next command
}
}
}