-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmicroshell.c
105 lines (99 loc) · 3.12 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* microshell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: astein <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/15 12:15:17 by shackbei #+# #+# */
/* Updated: 2023/12/06 23:29:19 by astein ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
/*not needed in exam, but necessary if you want to use this tester:
https://github.com/Glagan/42-exam-rank-04/blob/master/microshell/test.sh*/
// #ifdef TEST_SH
// # define TEST 1
// #else
// # define TEST 0
// #endif
void ft_putstr_fd2(char *str, char *arg)
{
while (*str)
write(2, str++, 1);
if (arg)
while(*arg)
write(2, arg++, 1);
write(2, "\n", 1);
}
void ft_execute(char *argv[], int i, int tmp_fd, char *env[])
{
//overwrite ; or | or NULL with NULL to use the array as input for execve.
//we are here in the child so it has no impact in the parent process.
argv[i] = NULL;
dup2(tmp_fd, STDIN_FILENO);
close(tmp_fd);
execve(argv[0], argv, env);
ft_putstr_fd2("error: cannot execute ", argv[0]);
exit(1);
}
int main(int argc, char *argv[], char *env[])
{
int i;
int fd[2];
int tmp_fd;
(void)argc; // is needed in exam, because the exam tester compiles with -Wall -Wextra -Werror
i = 0;
tmp_fd = dup(STDIN_FILENO);
while (argv[i] && argv[i + 1]) //check if the end is reached
{
argv = &argv[i + 1]; //the new argv start after the ; or |
i = 0;
//count until we have all informations to execute the next child;
while (argv[i] && strcmp(argv[i], ";") && strcmp(argv[i], "|"))
i++;
if (strcmp(argv[0], "cd") == 0) //cd
{
if (i != 2)
ft_putstr_fd2("error: cd: bad arguments", NULL);
else if (chdir(argv[1]) != 0)
ft_putstr_fd2("error: cd: cannot change directory to ", argv[1] );
}
else if (i != 0 && (argv[i] == NULL || strcmp(argv[i], ";") == 0)) //exec in stdout
{
if ( fork() == 0)
ft_execute(argv, i, tmp_fd, env);
else
{
close(tmp_fd);
while(waitpid(-1, NULL, WUNTRACED) != -1)
;
tmp_fd = dup(STDIN_FILENO);
}
}
else if(i != 0 && strcmp(argv[i], "|") == 0) //pipe
{
pipe(fd);
if ( fork() == 0)
{
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
close(fd[1]);
ft_execute(argv, i, tmp_fd, env);
}
else
{
close(fd[1]);
close(tmp_fd);
tmp_fd = fd[0];
}
}
}
close(tmp_fd);
// if (TEST) // not needed in exam, but necessary if you want to use this tester:
// while (1); // https://github.com/Glagan/42-exam-rank-04/blob/master/microshell/test.sh
return (0);
}