-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpipes_redirection.c
More file actions
166 lines (134 loc) · 2.85 KB
/
pipes_redirection.c
File metadata and controls
166 lines (134 loc) · 2.85 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//pipes_redirection.c
#include "headers.h"
int pipes_(char *a[], char *b[])
{
int fd_A[2];
int status;
int i;
pipe(fd_A);
pid_t pid_a, pid_b;
if (!(pid_a = fork()))
{
close(1);
dup(fd_A[WRITE_END]);
close(fd_A[0]);
close(fd_A[1]);
execvp(a[0], a);
}
else
{
//wait for the first command to end
wait(&status);
if (!(pid_b = fork()))
{
close(0);
dup(fd_A[READ_END]);
//the second process
close(fd_A[0]);
close(fd_A[1]);
execvp(b[0], b);
}
close(fd_A[WRITE_END]);
close(fd_A[READ_END]);
}
wait(&status);
//wait for 2nd process to end
return 0;
}
int redirection_(char *a[], char type_red[], char *b[])
{
int err = -1;
int fileDescriptor;
int option = 0;
pid_t pid;
if (strcmp(type_red, ">") == 0)
option = 0;
if (strcmp(type_red, "<") == 0)
option = 1;
if (strcmp(type_red, ">>") == 0)
option = 2;
if ((pid = fork()) == -1)
{
printf("Child process could not be created\n");
return;
}
if (pid == 0)
{
if (option == 0)
{
fileDescriptor = open(b[0], O_CREAT | O_TRUNC | O_WRONLY, 0600);
dup2(fileDescriptor, STDOUT_FILENO);
close(fileDescriptor);
}
else if (option == 1)
{
fileDescriptor = open(b[0], O_RDONLY, 0600);
dup2(fileDescriptor, STDIN_FILENO);
close(fileDescriptor);
}
else if (option == 2)
{
fileDescriptor = open(b[0], O_CREAT | O_WRONLY | O_APPEND, 0600);
dup2(fileDescriptor, STDOUT_FILENO);
close(fileDescriptor);
}
if (execvp(a[0], a) == err)
{
printf("err");
kill(getpid(), SIGTERM);
}
}
waitpid(pid, NULL, 0);
return 0;
}
int pipes_redirection(char *argv[], int argc)
{
char *command1[10];
char *command2[10];
int pipe_count = 0;
int i;
int temp_count1, temp_count2;
int flag_pipe, flag_redirection;
temp_count1 = 0;
temp_count2 = 0;
char type_red[3];
memset(command1, 0, sizeof(command1));
memset(command2, 0, sizeof(command2));
for (i = 0; i < argc; i++)
{
if (strchr(argv[i], '<') != NULL || strchr(argv[i], '|') != NULL || strchr(argv[i], '>') != NULL)
{
pipe_count = pipe_count + 1;
if (strchr(argv[i], '<') != NULL || strchr(argv[i], '>') != NULL)
{
flag_redirection = 1;
strcpy(type_red, argv[i]);
}
if (strcmp(argv[i], "|") == 0)
flag_pipe = 1;
continue;
}
if (pipe_count == 0)
{
command1[temp_count1] = (char *)malloc(sizeof(char) * (strlen(argv[i]) + 1));
command1[temp_count1] = argv[i];
temp_count1++;
}
if (pipe_count == 1)
{
command2[temp_count2] = (char *)malloc(sizeof(char) * (strlen(argv[i]) + 1));
command2[temp_count2] = argv[i];
temp_count2++;
}
}
if (flag_pipe == 1 && flag_redirection == 1)
{
printf("This is a basic shell nesting of pipes and redirection not allowed\n");
return -1;
}
if (flag_pipe == 1)
pipes_(command1, command2);
if (flag_redirection == 1)
redirection_(command1, type_red, command2);
return 0;
}