-
Notifications
You must be signed in to change notification settings - Fork 24
/
microshell.c
67 lines (60 loc) · 1.4 KB
/
microshell.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
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
void err(char *str)
{
while (*str)
write(2, str++, 1);
}
int cd(char **argv, int i)
{
if (i != 2)
return err("error: cd: bad arguments\n"), 1;
if (chdir(argv[1]) == -1)
return err("error: cd: cannot change directory to "), err(argv[1]), err("\n"), 1;
return 0;
}
void set_pipe(int has_pipe, int *fd, int end)
{
if (has_pipe && (dup2(fd[end], end) == -1 || close(fd[0]) == -1 || close(fd[1]) == -1))
err("error: fatal\n"), exit(1);
}
int exec(char **argv, int i, char **envp)
{
int has_pipe, fd[2], pid, status;
has_pipe = argv[i] && !strcmp(argv[i], "|");
if (!has_pipe && !strcmp(*argv, "cd"))
return cd(argv, i);
if (has_pipe && pipe(fd) == -1)
err("error: fatal\n"), exit(1);
if ((pid = fork()) == -1)
err("error: fatal\n"), exit(1);
if (!pid)
{
argv[i] = 0;
set_pipe(has_pipe, fd, 1);
if (!strcmp(*argv, "cd"))
exit(cd(argv, i));
execve(*argv, argv, envp);
err("error: cannot execute "), err(*argv), err("\n"), exit(1);
}
waitpid(pid, &status, 0);
set_pipe(has_pipe, fd, 0);
return WIFEXITED(status) && WEXITSTATUS(status);
}
int main(int argc, char **argv, char **envp)
{
(void)argc;
int i = 0, status = 0;
while (argv[i])
{
argv += i + 1;
i = 0;
while (argv[i] && strcmp(argv[i], "|") && strcmp(argv[i], ";"))
i++;
if (i)
status = exec(argv, i, envp);
}
return status;
}