-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.c
127 lines (112 loc) · 3.71 KB
/
pipe.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
//libraries
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <dirent.h>
#include <sys/dir.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include<sys/utsname.h>
#include "declarations.h"
void pipefunc()
{
strcpy(pipecmd,cmd);
int pipepoint=0;
char*t=strtok(pipecmd,"|");
while(t!=NULL)
{
pipecmdstorage[pipepoint]=t;
t=strtok(NULL,"|");
pipepoint++;
}
pipecmdstorage[pipepoint]=NULL;
pipecheck=pipepoint;//checks if pipe or not(no of commands)
pipepoint=0;
//printf("%d\n",pipecheck);
if(pipecheck==1)//no pipe then normal code
{
strcpy(cmd,pipecmdstorage[pipepoint]);
if(!strncmp(cmd,c1,3) || !strncmp(cmd,c2,4) || !strncmp(cmd,c3,2) || !strncmp(cmd,c4,2) || !strncmp(cmd,c5,5) || !strncmp(cmd,c7,5) || !strncmp(cmd,c6,8) || !strncmp(cmd,c8,6) || !strncmp(cmd,c9,8) || !strncmp(cmd,c10,4) || !strncmp(cmd,c11,4) || !strncmp(cmd,c12,8) || !strncmp(cmd,c13,4) || !strncmp(cmd,c14,2) || !strncmp(cmd,c15,2))
{
processbuilt_cmd();
redirectioncheck();
if(!builtinredirecterror)
builtin_check();
}
else
{
process_cmd();
execute();
free(buf);
}
}
else//if pipe then::
{
if(pipe(fd) != 0)
perror("pipe failed");
pid_t one =fork();
if(one==0)
{
dup2(fd[1],1);
close(fd[0]);
strcpy(cmd,pipecmdstorage[pipepoint]);
if(!strncmp(cmd,c1,3) || !strncmp(cmd,c2,4) || !strncmp(cmd,c3,2) || !strncmp(cmd,c4,2) || !strncmp(cmd,c5,5) || !strncmp(cmd,c7,5) || !strncmp(cmd,c6,8) || !strncmp(cmd,c8,6) || !strncmp(cmd,c9,8) || !strncmp(cmd,c10,4) || !strncmp(cmd,c11,4) || !strncmp(cmd,c12,8) || !strncmp(cmd,c13,4) || !strncmp(cmd,c14,2) || !strncmp(cmd,c15,2))
{
processbuilt_cmd();
redirectioncheck();
builtin_check();
exit(3);
}
else
{
process_cmd();
redirectioncheck();
if(execvp(buf[0],buf)==-1)//-1 if failed
printf("%s: command not found\n",buf[0]);
free(buf);
}
}
else
{
wait(NULL);
close(fd[1]);
pid_t two=fork();
if(two==0)
{
dup2(fd[0],0);
close(fd[0]);
close(fd[1]);
dup2(pipeout,1);
free(buf);
strcpy(cmd,pipecmdstorage[pipepoint+1]);
if(!strncmp(cmd,c1,3) || !strncmp(cmd,c2,4) || !strncmp(cmd,c3,2) || !strncmp(cmd,c4,2) || !strncmp(cmd,c5,5) || !strncmp(cmd,c7,5) || !strncmp(cmd,c6,8) || !strncmp(cmd,c8,6) || !strncmp(cmd,c9,8) || !strncmp(cmd,c10,4) || !strncmp(cmd,c11,4) || !strncmp(cmd,c12,8) || !strncmp(cmd,c13,4) || !strncmp(cmd,c14,2) || !strncmp(cmd,c15,2))
{
processbuilt_cmd();
redirectioncheck();
builtin_check();
}
else
{
process_cmd();
redirectioncheck();
if(execvp(buf[0],buf)==-1)//-1 if failed
printf("%s: command not found\n",buf[0]);
}
}
else
{
wait(NULL);
}
}
}
//printf("finish\n");
}