-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec_command.c
83 lines (73 loc) · 1.76 KB
/
exec_command.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
/*
** EPITECH PROJECT, 2018
** exec_command.c
** File description:
** execute command minishell
*/
#include "my.h"
void puts_env(char **envp)
{
for (int j = 0; envp[j] != NULL; j++) {
my_putstr(envp[j]);
my_putstr("\n");
}
}
int count_size_tab(char **tab)
{
int j = 0;
for(;tab[j] != NULL;j++);
return (j);
}
int verif_command(char *command, char **envp, char *path)
{
char **command_tab = my_str_to_wordtab(command);
int pid = 0;
if (my_strcmp(command_tab[0], "cd") == 0)
my_cd(command_tab);
else if (my_strcmp(command_tab[0], "env") == 0)
puts_env(envp);
else if (my_strcmp(command_tab[0], "setenv") == 0)
puts_env(envp);
else if ((pid = fork()) == -1)
return (84);
else
exec_command(command, path, envp, pid);
return (0);
}
char *my_cd(char **command_tab)
{
if (chdir(command_tab[1]) == -1) {
my_putstr(command_tab[1]);
write(2, ": No such file or directory.\n", my_strlen(": No such file or directory.\n"));
return ("echec");
}
my_putstr("\n");
return ("succes");
}
char *exec_command(char *command, char *path, char **envp, int pid)
{
char **new_path = my_str_to_wordtab(path);
char **new_command = my_str_to_wordtab(command);
int size = count_size_tab(new_command);
int i = 0;
int return_execve = 0;
char *filename = my_str_concast(new_path[i], "/");
new_command[size - 1] = NULL;
if (my_strcmp(command, "exit") == 0)
return (NULL);
if (pid == 0) {
while (new_path[i]) {
filename = my_str_concast(new_path[i], "/");
filename = my_str_concast(filename, new_command[0]);
return_execve = execve(filename, new_command, envp);
i++;
}
if (return_execve == -1) {
write(2, command, my_strlen(command));
write(2, ": Command not found.\n", my_strlen(": Command not found.\n"));
}
exit(0);
}
wait(0);
return (NULL);
}