summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-10-05 15:43:33 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-10-05 15:43:33 +0200
commit7c82cd4d8f2d78adca2db968edfec2381db108df (patch)
tree90b7e1886134b614d73dfc26a18af29655921a6a /src
parentPipes rework incomming (diff)
download42-minishell-7c82cd4d8f2d78adca2db968edfec2381db108df.tar.gz
42-minishell-7c82cd4d8f2d78adca2db968edfec2381db108df.tar.bz2
42-minishell-7c82cd4d8f2d78adca2db968edfec2381db108df.tar.xz
42-minishell-7c82cd4d8f2d78adca2db968edfec2381db108df.tar.zst
42-minishell-7c82cd4d8f2d78adca2db968edfec2381db108df.zip
pipes update
Diffstat (limited to 'src')
-rw-r--r--src/e_pipes.c107
-rw-r--r--src/e_pipes_next.c35
-rw-r--r--src/e_pipes_next.h2
3 files changed, 83 insertions, 61 deletions
diff --git a/src/e_pipes.c b/src/e_pipes.c
index 1b4e314..0177870 100644
--- a/src/e_pipes.c
+++ b/src/e_pipes.c
@@ -14,6 +14,7 @@
#include <libft.h>
#include <stdlib.h>
#include <stdint.h>
+#include <limits.h>
#include <unistd.h>
#include "e_builtins.h"
@@ -24,23 +25,50 @@
#include "u_utils.h"
#include "u_path.h"
+static uint8_t e_get_current_path(char fullpath[],
+ struct s_lpipes *ptr,
+ t_msh *msh)
+{
+ uint8_t fp_ret;
+
+ fp_ret = 0;
+ if (ptr->com->bin != NULL && ft_ischarset("/.", ptr->com->bin[0]) == TRUE)
+ {
+ ft_strlcpy(fullpath, ptr->com->bin, PATH_MAX);
+ }
+ else if (ptr->com->bin != NULL)
+ {
+ if (u_get_builtin_id(ptr->com->bin) < B_BUILTINS_COUNT)
+ {
+ ft_strlcpy(fullpath, "builtin", 8);
+ }
+ else
+ {
+ fp_ret = u_search_in_path(fullpath, ptr->com->bin, PATH_MAX, msh);
+ }
+ }
+ return (fp_ret);
+}
+
/*
** TODO: handle fork() failed, etc
*/
static int32_t e_unroll_pipes(int32_t fd[256][2],
- char *fullpath[],
size_t pipes,
t_msh *msh)
{
struct s_lpipes *head;
+ char fullpath[PATH_MAX];
int32_t pid;
uint16_t i;
+ uint8_t fp_ret;
head = msh->pipes;
i = 0;
while (i <= pipes && i < 255)
{
+ fp_ret = e_get_current_path(fullpath, head, msh);
if ((pid = fork()) == 0)
{
if (i != 0)
@@ -48,7 +76,7 @@ static int32_t e_unroll_pipes(int32_t fd[256][2],
if (i != pipes)
dup2(fd[i][E_READ_END], STDOUT_FILENO);
e_close_unused_fds(fd, pipes);
- e_pipe_child(fullpath, i, head->com, msh);
+ e_pipe_child(fullpath, fp_ret, head->com, msh);
}
head = head->next;
i++;
@@ -60,9 +88,7 @@ static int32_t e_unroll_pipes(int32_t fd[256][2],
** TODO: error mgmnt
*/
-static void e_pipe_exec_path(char *fullpath[],
- size_t pipes,
- t_msh *msh)
+static void e_pipe_exec(size_t pipes, t_msh *msh)
{
int32_t fd[256][2];
int32_t pid;
@@ -75,7 +101,7 @@ static void e_pipe_exec_path(char *fullpath[],
pipe(fd[i]);
i++;
}
- pid = e_unroll_pipes(fd, fullpath, pipes, msh);
+ pid = e_unroll_pipes(fd, pipes, msh);
i = 0;
while (i < pipes && i < 255)
{
@@ -97,61 +123,30 @@ static void e_pipe_exec_path(char *fullpath[],
/* exit(127); */
/* } */
-static char *e_get_current_path(struct s_lpipes *rptr, t_msh *msh)
-{
- char tmp[PATH_MAX];
- char *path;
- uint8_t fp_ret;
-
- path = NULL;
- if (rptr->com->bin != NULL && ft_ischarset("/.", rptr->com->bin[0]) == TRUE)
- {
- if ((path = ft_strdup(rptr->com->bin)) == NULL)
- f_alloc_and_destroy_msh(msh);
- }
- else
- {
- if (u_get_builtin_id(rptr->com->bin) < B_BUILTINS_COUNT)
- {
- if ((path = ft_strdup("builtin")) == NULL)
- f_alloc_and_destroy_msh(msh);
- }
- else
- {
- fp_ret = u_search_in_path(tmp, rptr->com->bin, PATH_MAX, msh);
- if ((path = ft_strdup(tmp)) == NULL)
- f_alloc_and_destroy_msh(msh);
- }
- }
- return (path);
-}
-
-static char **e_get_fullpath(size_t pipes, t_msh *msh)
-{
- struct s_lpipes *rptr;
- char **fullpath;
- size_t i;
+/* static char **e_get_fullpath(size_t pipes, t_msh *msh) */
+/* { */
+/* struct s_lpipes *rptr; */
+/* char **fullpath; */
+/* size_t i; */
- rptr = msh->pipes;
- if ((fullpath = (char **)malloc((pipes + 2) * sizeof(char *))) == NULL)
- f_alloc_and_destroy_msh(msh);
- fullpath[pipes + 1] = NULL;
- i = 0;
- while (rptr != NULL)
- {
- fullpath[i] = e_get_current_path(rptr, msh);
- i++;
- rptr = rptr->next;
- }
- return (fullpath);
-}
+/* rptr = msh->pipes; */
+/* if ((fullpath = (char **)malloc((pipes + 2) * sizeof(char *))) == NULL) */
+/* f_alloc_and_destroy_msh(msh); */
+/* fullpath[pipes + 1] = NULL; */
+/* i = 0; */
+/* while (rptr != NULL) */
+/* { */
+/* fullpath[i] = e_get_current_path(rptr, msh); */
+/* i++; */
+/* rptr = rptr->next; */
+/* } */
+/* return (fullpath); */
+/* } */
void e_pipes(t_msh *msh)
{
const size_t pipes = e_get_pipes_count(msh->pipes);
- fullpath = e_get_fullpath(pipes, msh);
- e_pipe_exec_path(fullpath, pipes, msh);
- ft_delwords(fullpath);
+ e_pipe_exec(pipes, msh);
s_lpipes_clear(&msh->pipes);
}
diff --git a/src/e_pipes_next.c b/src/e_pipes_next.c
index b3482c3..90e64f9 100644
--- a/src/e_pipes_next.c
+++ b/src/e_pipes_next.c
@@ -14,8 +14,11 @@
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
+#include <errno.h>
#include "e_redirs.h"
+#include "f_fail.h"
+#include "s_com.h"
#include "s_destroy.h"
#include "s_line.h"
#include "s_lpipes.h"
@@ -23,29 +26,53 @@
#include "u_utils.h"
#include "u_utils.h"
+static void e_fullpath_not_found(t_com *ptr, t_msh *msh)
+{
+ f_command_not_found(ptr->bin);
+ u_eof_fd(msh->fd);
+ s_com_destroy(&msh->com);
+ s_lpipes_clear(&msh->pipes);
+ s_line_clear(&msh->curr);
+ s_destroy(msh);
+ exit(127);
+}
+
/*
** TODO: handle execve failed
*/
-void e_pipe_child(char *fullpath[], uint8_t pipe_id, t_com *ptr, t_msh *msh)
+void e_pipe_child(char fullpath[], uint8_t fp_ret, t_com *ptr, t_msh *msh)
{
uint8_t bu_id;
uint8_t ret;
e_dup_redirs(ptr, msh);
- if (ft_strncmp(fullpath[pipe_id], "builtin", 8) == 0)
+ if (ft_strncmp(fullpath, "builtin", 8) == 0)
{
bu_id = u_get_builtin_id(ptr->bin);
ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh);
u_eof_fd(msh->fd);
+ s_com_destroy(&msh->com);
s_lpipes_clear(&msh->pipes);
s_line_clear(&msh->curr);
s_destroy(msh);
- ft_delwords(fullpath);
exit(ret);
}
else
- execve(fullpath[pipe_id], ptr->argv, msh->envp);
+ {
+ if (fp_ret == 2)
+ e_fullpath_not_found(ptr, msh);
+ if (execve(fullpath, ptr->argv, msh->envp) == -1)
+ {
+ f_exec(fullpath, ptr->bin);
+ u_eof_fd(msh->fd);
+ s_com_destroy(&msh->com);
+ s_lpipes_clear(&msh->pipes);
+ s_line_clear(&msh->curr);
+ s_destroy(msh);
+ exit(errno);
+ }
+ }
}
size_t e_get_pipes_count(struct s_lpipes *ptr)
diff --git a/src/e_pipes_next.h b/src/e_pipes_next.h
index fe042d6..d8c1aac 100644
--- a/src/e_pipes_next.h
+++ b/src/e_pipes_next.h
@@ -18,7 +18,7 @@
# include "s_struct.h"
-void e_pipe_child(char *fullpath[], uint8_t pipe_id, t_com *ptr, t_msh *msh);
+void e_pipe_child(char fullpath[], uint8_t fp_ret, t_com *ptr, t_msh *msh);
size_t e_get_pipes_count(struct s_lpipes *ptr);
void e_close_unused_fds(int32_t fd[256][2], size_t pipes);