summaryrefslogtreecommitdiffstats
path: root/src/e_externs_pipes.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/e_externs_pipes.c64
1 files changed, 27 insertions, 37 deletions
diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c
index 8556cbb..ca843d0 100644
--- a/src/e_externs_pipes.c
+++ b/src/e_externs_pipes.c
@@ -16,51 +16,42 @@
#include <stdint.h>
#include <unistd.h>
#include <string.h>
+#include <limits.h>
#include <errno.h>
#include "d_define.h"
-#include "e_externs_next.h"
#include "f_fail.h"
#include "m_redirs.h"
#include "s_destroy.h"
#include "s_line.h"
#include "s_lpipes.h"
#include "s_struct.h"
-
-static uint8_t
- get_builtin_id(const char com[],
- t_msh *msh)
-{
- uint8_t i;
-
- i = 0;
- while (msh->bu_ref[i] && ft_strncmp(com, msh->bu_ref[i],
- ft_strlen(msh->bu_ref[i]) + 1) != 0)
- {
- i++;
- }
- return (i);
-}
+#include "u_path.h"
+#include "u_utils.h"
static void
- e_extern_child(const char *fullpath,
- t_com *ptr,
- t_msh *msh)
+ e_pipe_child(char *fullpath[],
+ uint8_t pipe_id,
+ t_com *ptr,
+ t_msh *msh)
{
uint8_t bu_id;
uint8_t ret;
dup_redirs(ptr, msh);
- if (ft_strncmp(fullpath, "builtin", 8) == 0)
+ if (ft_strncmp(fullpath[pipe_id], "builtin", 8) == 0)
{
- bu_id = get_builtin_id(ptr->bin, msh);
+ bu_id = u_get_builtin_id(ptr->bin);
ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh);
+ u_eof_fd(msh->fd);
+ s_lpipes_clear(&msh->pipes);
s_line_clear(&msh->curr);
s_destroy(msh);
+ ft_delwords(fullpath);
exit(ret);
}
else
- execve(fullpath, ptr->argv, msh->envp);
+ execve(fullpath[pipe_id], ptr->argv, msh->envp);
/* TODO: handle execve failed */
}
@@ -81,14 +72,14 @@ static size_t
}
static void
- exec_path(const char *fullpath[],
- struct s_lpipes *head,
- t_msh *msh)
+ e_pipe_exec_path(char *fullpath[],
+ struct s_lpipes *head,
+ t_msh *msh)
{
+ int fd[256][2];
size_t pipes;
size_t i;
size_t j;
- int fd[256][2];
int pid;
int status;
@@ -116,7 +107,7 @@ static void
close(fd[j][FT_READ_END]);
j++;
}
- e_extern_child(fullpath[i], head->com, msh);
+ e_pipe_child(fullpath, i, head->com, msh);
}
head = head->next;
i++;
@@ -138,8 +129,8 @@ void
{
struct s_lpipes *head;
struct s_lpipes *rptr;
- char **envpath;
char **fullpath;
+ char tmp[PATH_MAX];
size_t i;
size_t pipes;
uint8_t bu_id;
@@ -147,7 +138,7 @@ void
head = ptr;
rptr = ptr;
pipes = e_get_pipes_count(head);
- if (!(fullpath = (char **)malloc((pipes + 2) * sizeof(char *))))
+ if ((fullpath = (char **)malloc((pipes + 2) * sizeof(char *))) == NULL)
f_alloc_and_destroy_msh(msh);
fullpath[pipes + 1] = NULL;
i = 0;
@@ -158,25 +149,24 @@ void
if ((fullpath[i] = ft_strdup(rptr->com->bin)) == NULL)
f_alloc_and_destroy_msh(msh);
}
- else if ((envpath = get_env_path(msh)) != NULL)
+ else
{
- if ((bu_id = get_builtin_id(rptr->com->bin, msh))
+ if ((bu_id = u_get_builtin_id(rptr->com->bin))
< FT_BUILTINS_COUNT)
{
if ((fullpath[i] = ft_strdup("builtin")) == NULL)
f_alloc_and_destroy_msh(msh);
}
else
- fullpath[i] = search_in_path(rptr->com->bin, envpath, msh);
- ft_delwords(envpath);
+ {
+ u_search_in_path(tmp, rptr->com->bin, PATH_MAX, msh);
+ fullpath[i] = ft_strdup(tmp);
+ }
}
i++;
rptr = rptr->next;
}
i = 0;
- while (fullpath[i] != NULL)
- i++;
- if (i == (pipes + 1))
- exec_path((const char**)fullpath, head, msh);
+ e_pipe_exec_path(fullpath, head, msh);
ft_delwords(fullpath);
}