-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspawn.c
78 lines (70 loc) · 2.06 KB
/
spawn.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
#include "osh.h"
void set_pipes(Exec * the) {
if(check_env("dbg")) printf("Pipe be.\n");
int piping[2];
pipe(piping);
the->fd[1] = piping[1];
the->pipee->fd[0] = piping[0];
if(check_env("dbg")) printf("Piped be: %d %d.\n", piping[0], piping[1]);
what_exec(the);
if(the->pipee->pipee) {set_pipes(the->pipee);}
if(check_env("dbg")) printf("Pipes set.\n");
}
void bb_wait(int f) {
while (waitpid(f, NULL, 0) == -1) if(check_env("dbg")) printf(".");
}
void pipist(Exec * the) {
int g = fork();
if(g) {
dup2(the->fd[0], 0);
dup2(the->fd[1], 1);
if(check_env("dbg")) printf("Output piped.\n");
execvp(the->args[0], the->args);
}
else {
if(the->pipee->pipee) {
if(check_env("dbg")) printf("Pipe chain.\n");
pipist(the->pipee);
return;
}
what_exec(the->pipee);
dup2(the->pipee->fd[0], 0);
if(check_env("dbg")) printf("Input piped.\n");
dup2(the->pipee->fd[1], 1);
if(check_env("dbg") && the->pipee->fd[1] != 1) printf("Pipe redirected: %d\n", the->pipee->fd[1]);
what_exec(the->pipee);
execvp(the->pipee->args[0], the->pipee->args);
}
}
// multiprocessing part.
void spawn(Exec * the) {
if(check_env("logs"))
printf("Spawn.\n");
if(check_env("dbg"))
printf("bg: %d\n", the->fgbg);
if(the->pipee) set_pipes(the);
int f = fork();
if(f) {
if(check_env("logs")) printf("Parent.\n");
if(the->fgbg) {
if(check_env("logs")) printf("No.\n");
}
else {
if(check_env("logs")) printf("Wait.\n");
bb_wait(f); // blank (no data) blocking wait
}
if(check_env("logs")) printf("Done.\n");
}
else {
if(check_env("logs")) printf("Child.\n");
what_exec(the);
if(the->pipee) {
pipist(the);
}
else {
dup2(the->fd[0], 0);
dup2(the->fd[1], 1);
execvp(the->args[0], the->args);
}
}
}