-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicroShell.c
More file actions
117 lines (94 loc) · 2.32 KB
/
MicroShell.c
File metadata and controls
117 lines (94 loc) · 2.32 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
short ft_print_error(char *str)
{
while (*str)
write (2, str++, 1);
return (1);
}
short ft_handle_cd(char **av, int i)
{
if (i != 2)
return ft_print_error("Error cd: bad arguments\n");
else if (chdir(av[1]) == -1)
{
ft_print_error("Error no such file or directory: ");
ft_print_error(av[1]);
ft_print_error("\n");
return (1);
}
return 0;
}
char *ft_strcat(char *dst, char *src)
{
short i;
short j;
char *res;
res = malloc(strlen(dst) + strlen(src) + 1);
if (!res)
return (NULL);
i = -1;
while (dst[++i])
res[i] = dst[i];
j = -1;
while (src[++j])
res[i++] = src[j];
res[i] = '\0';
return (res);
}
short ft_execute_command(char **av, char **env, int i)
{
int fd[2];
int status;
int pid;
int has_pipe;
has_pipe = 0;
if (av[i] && !strcmp(av[i], "|"))
has_pipe = 1;
if (has_pipe && pipe(fd) == -1)
return (ft_print_error("Error fatal\n"));
pid = fork();
if (!pid)
{
av[i] = NULL;
if (has_pipe && (dup2(fd[1], 1) == -1 || close(fd[0]) == -1 || close(fd[1]) == -1))
return (ft_print_error("Error fatal\n"));
execve(ft_strcat("/bin/", *av), av, env);
ft_print_error("Error Command Not Found: ");
ft_print_error(*av);
ft_print_error("\n");
return (1);
}
waitpid(pid, &status, 0);
if (has_pipe && (dup2(fd[0], 0) == -1 || close(fd[0]) == -1 || close(fd[1]) == -1))
return (ft_print_error("Error fatal\n"));
return (WIFEXITED(status) && WEXITSTATUS(status));
}
int main(int ac, char **av, char **env)
{
short i;
short j;
short status;
status = 0;
i = 1;
if (ac > 1)
{
while (av[i])
{
j = i;
while (av[j] && strcmp(av[j], "|") && strcmp(av[j], ";"))
j++;
if (!strcmp(av[i], "cd"))
status = ft_handle_cd(&av[i], j - i);
else if (j > i)
status = ft_execute_command(&av[i], env, j - i);
if (av[j])
j++;
i = j;
}
}
return (status);
}