-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmmd.c
executable file
·96 lines (88 loc) · 2.43 KB
/
cmmd.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pmoreno- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/11 09:20:12 by potero #+# #+# */
/* Updated: 2022/08/11 10:32:30 by pmoreno- ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void direction(t_data *data)
{
char **path;
int i;
t_argv *aux;
aux = *data->argv;
path = ft_split(cmmd_find_path(data->myenv_str), ':');
i = 0;
while (aux)
{
aux->direction = cmmd_path(path, aux->split[0]);
if ((aux->direction == NULL && path != 0)
|| (ft_strcmp(aux->split[0], "minishell") == 0))
aux->error_code = 127;
else
aux->error_code = 100;
i++;
aux = aux->next;
}
if (path != 0)
free_env_char(path);
else
free(path);
}
static void child(int fd[2])
{
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
}
static void files(t_argv *arg)
{
if (arg->infile == 0)
arg->infile = ft_strdup("/dev/fd/0");
if (arg->outfile == 0)
arg->outfile = ft_strdup("/dev/fd/1");
}
static void file_des(int fd[2], t_argv *arg)
{
fd[0] = open(arg->infile, O_RDONLY);
if (fd[0] < 0)
fd_error(arg->infile);
if (arg->out == 2)
fd[1] = open(arg->outfile, O_CREAT | O_RDWR | O_APPEND, 0644);
else
fd[1] = open(arg->outfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd[1] < 0)
fd_error(arg->outfile);
}
int execute_cmmd(t_data *data)
{
int fd[2];
int status;
pid_t pid;
t_argv *arg;
arg = *data->argv;
pid = fork();
g_sign[0] = pid;
files(arg);
if (pid == -1)
return (1);
else if (pid == 0)
{
if (g_sign[1] == 2)
exit (0);
file_des(fd, arg);
child(fd);
if (min_builtins(arg, data) == 0)
exit (data->error_no);
if (execve(arg->direction, arg->split, data->myenv_str) < 0)
exit(child_error(arg, (int) errno));
}
wait(&status);
return (WEXITSTATUS(status));
}