-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiping.c
99 lines (91 loc) · 1.85 KB
/
piping.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
#include "headers.h"
bool pipe_Checking(char *command_1)
{
bool piping_Flag=false;
ll length_1=strlen(command_1);
for(ll i=0;i<length_1;i++)
{
if(command_1[i]=='|')
{
piping_Flag=true;
}
}
return piping_Flag;
}
bool check_Redirection(char *command_1)
{
bool redirection_Flag=false;
char *commands[100000];
int no_of_flags=0;
commands[0]= strtok(command_1," \t\n");
while(commands[no_of_flags] != NULL)
{
no_of_flags++;
commands[no_of_flags]=strtok(NULL," \t\n");
}
for(int i=0;i<no_of_flags;i++)
{
for(int j=0;j<strlen(commands[i]);j++)
{
if((commands[i][j]=='<')||(commands[i][j]=='>'))
{
redirection_Flag=true;
}
}
}
return redirection_Flag;
}
/*
void piping(char *command_2)
{
char *commands_1[100000];
int no_of_flags_1=0;
commands_1[0]=strtok(commands_2,"|");
while(commands_1[no_of_flags_1] != NULL)
{
no_of_flags_1++;
commands_1[no_of_flags_1]=strtok(NULL,"|");
}
for(int i=0;i<no_of_flags_1;i++)
{
int pipefd[100000][2];
pid_t cpid;
int z = pipe(pipefd);
if (z < 0)
{
perror("Error: pipe could not be created\n");
}
cpid = fork();
if (cpid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if(cpid==0)
{
if(i==0)
{
dup2(pipefd[i][1], 1);
close(pipefd[i][0]);
strcpy(temp, commands[i]);
}
else if(i==(no_of_flags_1-1))
{
dup2(pipefd[i][0],0);
}
else
{
dup2(pipes[0], 0);
// replace grep's stdout with write end of 2nd pipe
dup2(pipes[3], 1);
// close all ends of pipes
close(pipes[0]);
close(pipes[1]);
close(pipes[2]);
close(pipes[3]);
}
}
run1(command_1[i]);
}
}
*/