-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpseudo.txt
More file actions
90 lines (75 loc) · 2.68 KB
/
pseudo.txt
File metadata and controls
90 lines (75 loc) · 2.68 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
#includes
function declarations
while true :
reap children processes (catch possible zombie processes)
get { hostname, username, current directory, $OLDPWD(NULL to start) }
print username@hostname prompt
create 3 arg lists (allocate mem properly, initialize to NULL)
read sommand line input
parse out whitespace,
do input error handling (malformed/ambiguous commands)
fill first argslist with full command line
check for IOacct/background process commands, adjust first argslist
if cmd = built-in (cd, ioacct, exit) :
if cd :
chdir(), handle special cases (-, ~, blank, &)
if exit[n] :
reap child processes, return n;
else :
parse out possible pipes in first argslist
fill second and third argslists with piped commands
if no pipes:
parse first arglist for redirects, adjust array, store redirect stream
search PATH for cmd
child = fork()
if child == 0 (inside child) :
check redirects:
if in redirect, open file RDONLY, dup STDIN, close
if out redirect, creat() file 0644 perms, dup STDOUT, close
if append redirect, open file WRONLY|APPEND, dup STDOUT, close
execv(cmd path, args)
else if background:
waitpid(-1, (int *)NULL, WNOHANG);
else if ioacct:
read from proc/[pid]/io file while waitpid(WNOHANG) == 0
print bytes read/written
else (inside parent):
waitpid(-1, (int *)NULL, 0)
if 1 pipe:
create pipe int array pipefd[2], call pipe()
parse second argslist for redirects
child = fork()
if child == 0 (inside second argument):
set input fd to pipefd[0], close fd's
check redirects:
if in redirect, open file RDONLY, dup STDIN, close
if out redirect, creat() file 0644 perms, dup STDOUT, close
if append redirect, open file WRONLY|APPEND, dup STDOUT, close
execv(2nd cmd path, args)
else (in parent):
child2 = fork()
if child == 0:
set output fd to pipefd[1], close fd's
execv(1st cmd path, args)
close pipes
call waitpid(-1, (int*)NULL, 0) on both processes
if 2 pipes:
create pipe in array pipefd[4], call pipe twice
parse third arglist for redirects
child = fork()
if child == 0 (1st cmd):
set output to pipefd[1], close fd's
execv(1st cmd path, args)
else:
child2 = fork()
if child2 == 0 (2nd cmd):
set input fd to pipefd[0], output to pipefd[3], close fd's
execv(cmd path 2, args)
else:
child3 = fork()
if child3 == 0 (3rd cmd):
set input to pipefd[2], close fd's
do redirect fd dup2's
execv(cmd path 3, args)
close pipes
call waitpid(-1, (int*)NULL, 0) on all 3 processes