-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
109 lines (89 loc) · 2.21 KB
/
main.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
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
size_t ft_strlen(const char *str)
{
size_t i = 0;
while (str[i]) { ++i; }
return (i);
}
int fatal_error(void)
{
write(STDERR_FILENO, "error: fatal\n", 13);
return (1);
}
int cd(char **cmd)
{
int i = 0;
while (cmd[i]) { i++; }
if (i != 2)
write(STDERR_FILENO, "error: cd: bad arguments\n", 25);
else if (chdir(cmd[1]) == -1)
{
write(STDERR_FILENO, "error: cd: cannot change directory to ", 38);
write(STDERR_FILENO, cmd[1], ft_strlen(cmd[1]));
write(STDERR_FILENO, "\n", 1);
}
else
return (0);
return (1);
}
void child_execute_command(char **cmd, char **env)
{
if (!strcmp(cmd[0], "cd")) { exit(cd(cmd)); }
execve(cmd[0], cmd, env);
write(STDERR_FILENO, "error: cannot execute ", 22);
write(STDERR_FILENO, cmd[0], ft_strlen(cmd[0]));
write(STDERR_FILENO, "\n", 1);
exit(1);
}
void execute_command(char **cmd, char **env)
{
int i, j, last, readFrom, writeTo;
int dup_in, dup_out, pid;
int fd[2];
j = 0;
while (cmd[j] && strcmp(cmd[j], "|")) { ++j; }
if (!cmd[j] && !strcmp(cmd[0], "cd")) { cd(cmd); return ; }
if ((readFrom = dup(STDIN_FILENO)) == -1) { fatal_error(); }
if ((dup_in = dup(STDIN_FILENO)) == -1) { fatal_error(); }
if ((dup_out = dup(STDOUT_FILENO)) == -1) { fatal_error(); }
last = i = j = 0;
while (!last)
{
while (cmd[j] && strcmp(cmd[j], "|")) { ++j; }
if (!cmd[j]) { last = 1; }
cmd[j] = NULL;
if (pipe(fd) == -1) { fatal_error(); }
if (last) { writeTo = dup_out; }
else { writeTo = fd[1]; }
if (dup2(readFrom, STDIN_FILENO) == -1) { fatal_error(); }
close(readFrom);
if (dup2(writeTo, STDOUT_FILENO) == -1) { fatal_error(); }
close(writeTo);
if ((pid = fork()) == -1) { fatal_error(); }
if (pid == 0)
child_execute_command(cmd + i, env);
else
readFrom = fd[0];
i = ++j;
}
while (wait(NULL) != -1);
if (dup2(dup_in, STDIN_FILENO) == -1) { fatal_error(); }
close(dup_in); close(fd[1]); close(readFrom);
}
int main(int argc, char **argv, char **env)
{
int i = 1;
int j = 1;
while (i < argc)
{
while (argv[j] && strcmp(argv[j], ";")) { j++; }
argv[j] = NULL;
if (argv[i]) { execute_command(argv + i, env); }
i = ++j;
}
return (0);
}