-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute_cmd.c
69 lines (63 loc) · 1.74 KB
/
execute_cmd.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execute_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dvan-der <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/14 13:55:14 by dvan-der #+# #+# */
/* Updated: 2022/02/09 11:45:43 by dvan-der ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
static void error_handling(char *cmd)
{
if (ft_strchr(cmd, '/'))
{
ft_putstr_fd("zsh: no such file or directory: ", 2);
ft_putstr_fd(cmd, 2);
ft_putstr_fd("\n", 2);
exit(2);
}
else
{
ft_putstr_fd(cmd, 2);
ft_putstr_fd(": command not found\n", 2);
exit(127);
}
}
static void execute_with_path(t_pipe *p)
{
char *str;
str = p->mycmd[0];
if (execve(str, p->mycmd, p->envp) == 1)
{
perror("");
exit(errno);
}
return ;
}
void execute_cmd(t_pipe *p)
{
int i;
char *str;
if (access(p->mycmd[0], F_OK && X_OK) == 0)
execute_with_path(p);
i = 0;
while ((p->path)[i])
{
str = ft_strjoin((p->path)[i], (p->mycmd)[0]);
ft_check_malloc(str);
if (access(str, F_OK && X_OK) == 0)
{
if (execve(str, p->mycmd, p->envp) == -1)
{
perror("");
exit(errno);
}
}
free(str);
i++;
}
error_handling(p->mycmd[0]);
}