-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprueba.c
163 lines (151 loc) · 3.15 KB
/
prueba.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "minishell.h"
#include "executor.h"
#include <limits.h>
int error_exe(int er, char *cmd)
{
char *msg;
msg = NULL;
//De momento no hay error 0, 1, 2, 3, 4, 5 y más
if (er == 0)
msg = ft_strjoin("Error: Need 4 arguments:\n", cmd);
else if (er == 1)
ft_putstr_fd("Error: Pipe not open\n", 2);
else if (er == 2)
ft_putstr_fd("Error: PID incorrect\n", 2);
else if (er == 3)
msg = ft_strjoin("Error: No such file or directory: ", cmd);
else if (er == 4)
msg = ft_strjoin("Error: Permission denied: ", cmd);
else if (er == 5)
msg = ft_strjoin("Error. Command not found: ", cmd);
else if (er > 5)
perror("Error");
if (msg)
{
ft_putstr_fd(msg, 2);
free (msg);
}
return (1);
}
/**
* Busca el array que comienza con "PATH" (donde se encuentran las rutas de los
* ejecutables) y devuelve su posición.
*/
int search_path(char **envp)
{
int pos;
char *subpath;
pos = 0;
while (envp[pos])
{
subpath = ft_substr(envp[pos], 0, 4);
if (ft_strcmp(subpath, "PATH") == 0)
{
free(subpath);
return (pos);
}
free(subpath);
pos++;
}
return (0);
}
/**
* Se encarga de comprobar todas las posibles rutas, indicadas en envp, en las
* que se puede encontrar el comando ejecutable al que se está haciendo
* referencia.
* El modo de comprobar si existe e utilizando la función access con los modos
* F_OK y X_OK que evalúan si el archivo existe y si es ejecutable,
* respectivamente.
*/
char *com_path(char *cmd, char **envp)
{
char *path;
char *slash;
char **enpath;
int pos;
int i;
i = 1;
pos = search_path(envp);
enpath = ft_split(envp[pos], ':');
while (enpath[i])
{
slash = ft_strjoin(enpath[i], "/");
path = ft_strjoin(slash, cmd);
free(slash);
if (access (path, F_OK | X_OK) == 0)
{
ft_free_double(enpath);
return (path);
}
i++;
free(path);
}
ft_free_double(enpath);
return (NULL);
}
int create_child(t_task *task, char **envp)
{
int pid;
char *pathcmd;
pathcmd = com_path(task->cmd, envp);
if (pathcmd == NULL)
return (5);//error en reserva de memoria?
pid = fork();
if (pid == -1)
return (2);
if (pid == 0)
{
execve(pathcmd, task->argv, envp);
exit(EXIT_FAILURE);
}
task->pid = pid;
return(0);
}
int execpipe(t_pipe *pipe_node, char **envp)
{
int previous_stdin;
int original_stdout;
int pipefd[2];
previous_stdin = dup(STDIN_FILENO);
original_stdout = dup(STDOUT_FILENO);
pipe(pipefd);
if (pipe_node->left)
{
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[1]);
executor((t_tree *)pipe_node->left, envp);
dup2(original_stdout, STDOUT_FILENO);
}
if (pipe_node->rigth)
{
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[0]);
executor(pipe_node->rigth, envp);
dup2(previous_stdin, STDIN_FILENO);
}
close(previous_stdin);
close(original_stdout);
}
int executor(t_tree *node, char **envp)
{
t_pipe *pipe_node;
t_task *task;
int err;
if (!node)
return (0);
if (node->type == PIPE)
{
pipe_node = (t_pipe *)node;
err = exec_pipe(node, envp);
if (err != 0)
return (err);
}
else if (node->type == TASK)
{
task = (t_task *)node;
err = create_child(task, envp);
if (err != 0)
return (err);
}
return (0);
}