-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest1.c
More file actions
158 lines (144 loc) · 2.97 KB
/
Copy pathtest1.c
File metadata and controls
158 lines (144 loc) · 2.97 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
typedef struct Node{
pid_t pid;
char pname[100];
struct Node *next;
}node;
int spawn_proc (int in, int out, char *com)
{
pid_t pid;
char *par[1000];
// int andPresent=0; //variable to check if the process is background
char * Split_temp = strtok(com," "); // strtok to parse the command and storing them in params array
for(int i = 0; i < 1000; i++) {
par[i] = Split_temp;
Split_temp = strtok(NULL," ");
if(par[i] == NULL) break;
}
if ((pid = fork ()) == 0)
{
if (in != 0)
{
dup2 (in, 0);
close (in);
}
if (out != 1)
{
dup2 (out, 1);
close (out);
}
return execvp (par[0], par);
}
return pid;
}
int piping(char *comd)
{
// printf("reasched\n");
int save_in, save_out;
int tempin=dup(0);
int tempout=dup(1);
save_in = dup(0);
save_out = dup(1);
int i;
// pisd_t pid;
int in, fd [2];
char * split;
char com[100][100];
if(comd[strlen(comd)-1] == '\n')
{
comd[strlen(comd)-1] = '\0';
}
int temp=0;
split = strtok(comd,"|"); //seperating different ; seperated commands
while(split)
{
strcpy(com[temp],split);
split = strtok(NULL,"|");
temp++;
}
/* The first process should get its input from the original file descriptor 0. */
in = 0;
/* Note the loop bound, we spawn here all, but the last stage of the pipeline. */
char *par[1000];
for (i = 0; i < temp-1; ++i)
{
if(i==0)
{
parseCmm(com[i],par);
setInput(par,tempin);
}
pipe (fd);
spawn_proc (in, fd [1], com[i]);
close (fd [1]);
in = fd [0];
}
// printf("com=%s\n",com[i]);
// char * Split_temp = strtok(com[i]," "); // strtok to parse the command and storing them in params array
// printf("string=\n");
// for(int i = 0; i < 1000; i++)
// {
// par[i] = Split_temp;
// // printf("%s\n",Split_temp );
// Split_temp = strtok(NULL," ");
// if(par[i] == NULL) break;
// }
//new
parseCmm(com[i],par);
// printf("setout\n");
setOutput(par,tempout);
// i=0;
// while(par[i]!=NULL)
// {
// printf("%s ",par[i] );
// i++;
// }
// setOutput(par,tempout);
pid_t pid = fork(); // fork process
// Error
if (pid == -1) {
char* error = strerror(errno);
printf("fork: %s\n", error);
dup2(save_in, 0);
dup2(save_out,1);
close(save_in);
close(save_out);
return 1;
}
// Child process
else if (pid == 0) {
// execute execvp
if (in != 0)
dup2 (in, 0);
execvp(par[0], par);
dup2(save_in, 0);
dup2(save_out,1);
close(save_in);
close(save_out);
// if error occured
char* error = strerror(errno);
printf("shell: %s: %s\n", par[0], error);
return 0;
}
// Parent process
else {
int childStatus;
waitpid(pid, &childStatus, 0);
dup2(save_in, 0);
dup2(save_out,1);
close(save_in);
close(save_out);
return 1;
}
/* Time passes, STDIN_FILENO isn't what it used to be. */
dup2(save_in, 0);
dup2(save_out,1);
close(save_in);
close(save_out);
}