summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--Makefile2
-rw-r--r--src/e_builtins.c4
-rw-r--r--src/e_externs.c3
-rw-r--r--src/e_externs_pipes.c4
-rw-r--r--src/e_redirs.c85
-rw-r--r--src/e_redirs.h (renamed from src/m_redirs.h)4
-rw-r--r--src/m_redirs.c61
-rw-r--r--src/p_redirs.c18
-rw-r--r--src/s_com.c2
9 files changed, 108 insertions, 75 deletions
diff --git a/Makefile b/Makefile
index bc77895..c6b8646 100644
--- a/Makefile
+++ b/Makefile
@@ -38,6 +38,7 @@ SRCS_NAME += e_externs
SRCS_NAME += e_externs_pipes
SRCS_NAME += e_line
SRCS_NAME += e_pipes
+SRCS_NAME += e_redirs
SRCS_NAME += f_alloc
SRCS_NAME += f_chdir
SRCS_NAME += f_com
@@ -57,7 +58,6 @@ SRCS_NAME += m_loop_next
SRCS_NAME += m_minishell
SRCS_NAME += m_mshrc
SRCS_NAME += m_prompt
-SRCS_NAME += m_redirs
SRCS_NAME += s_destroy
SRCS_NAME += s_com
SRCS_NAME += s_init
diff --git a/src/e_builtins.c b/src/e_builtins.c
index 92af2c3..90f3775 100644
--- a/src/e_builtins.c
+++ b/src/e_builtins.c
@@ -19,8 +19,8 @@
#include "b_builtins.h"
#include "b_export_next.h"
#include "b_export_mute.h"
+#include "e_redirs.h"
#include "m_loop.h"
-#include "m_redirs.h"
#include "s_com.h"
#include "s_destroy.h"
#include "s_line.h"
@@ -50,7 +50,7 @@ static void
if (ptr->env_fork != NULL)
e_export_env_fork(ptr, msh);
- dup_redirs(ptr, msh);
+ e_dup_redirs(ptr, msh);
ret = 0;
if (bu_id == FT_ID_H && msh->fd == STDIN_FILENO)
ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh);
diff --git a/src/e_externs.c b/src/e_externs.c
index 8761986..f4b608c 100644
--- a/src/e_externs.c
+++ b/src/e_externs.c
@@ -22,7 +22,7 @@
#include "b_export_next.h"
#include "d_define.h"
#include "f_fail.h"
-#include "m_redirs.h"
+#include "e_redirs.h"
#include "s_com.h"
#include "s_destroy.h"
#include "s_line.h"
@@ -72,6 +72,7 @@ static void
{
if (ptr->env_fork != NULL)
e_export_env_fork(ptr, msh);
+ e_dup_redirs(ptr, msh);
e_extern_child(fullpath, ptr, msh);
}
else if (pid < 0)
diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c
index ca843d0..64581e4 100644
--- a/src/e_externs_pipes.c
+++ b/src/e_externs_pipes.c
@@ -21,7 +21,7 @@
#include "d_define.h"
#include "f_fail.h"
-#include "m_redirs.h"
+#include "e_redirs.h"
#include "s_destroy.h"
#include "s_line.h"
#include "s_lpipes.h"
@@ -38,7 +38,7 @@ static void
uint8_t bu_id;
uint8_t ret;
- dup_redirs(ptr, msh);
+ e_dup_redirs(ptr, msh);
if (ft_strncmp(fullpath[pipe_id], "builtin", 8) == 0)
{
bu_id = u_get_builtin_id(ptr->bin);
diff --git a/src/e_redirs.c b/src/e_redirs.c
new file mode 100644
index 0000000..474e043
--- /dev/null
+++ b/src/e_redirs.c
@@ -0,0 +1,85 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* e_redirs.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "f_fail.h"
+#include "s_destroy.h"
+#include "s_line.h"
+#include "s_struct.h"
+
+static void e_redir_minus_one(struct s_lredir *ptr, t_msh *msh)
+{
+ int32_t fd;
+
+ if ((fd = open(ptr->path, O_RDONLY)) == -1)
+ {
+ f_redir(ptr->path, msh);
+ return ;
+ }
+ dup2(fd, STDIN_FILENO);
+ close(fd);
+}
+
+static void e_redir_plus_one(struct s_lredir *ptr, t_msh *msh)
+{
+ int32_t fd;
+
+ if ((fd = open(ptr->path, O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1)
+ {
+ f_redir(ptr->path, msh);
+ return ;
+ }
+ dup2(fd, ptr->fd);
+ close(fd);
+}
+
+static void e_redir_plus_two(struct s_lredir *ptr, t_msh *msh)
+{
+ int32_t fd;
+
+ if ((fd = open(ptr->path, O_CREAT | O_APPEND | O_WRONLY, 0644)) == -1)
+ {
+ f_redir(ptr->path, msh);
+ return ;
+ }
+ dup2(fd, ptr->fd);
+ close(fd);
+}
+
+void e_dup_redirs(const t_com *com, t_msh *msh)
+{
+ struct s_lredir *ptr;
+
+ ptr = com->rdr;
+ while (ptr != NULL)
+ {
+ ft_printf("PATH: [%s]; FD: [%d]; RDR: [%hhd]\n", ptr->path, ptr->fd, ptr->redir);
+ if (ptr->redir == -1)
+ {
+ e_redir_minus_one(ptr, msh);
+ }
+ else if (ptr->redir == 1)
+ {
+ e_redir_plus_one(ptr, msh);
+ }
+ else if (ptr->redir == 2)
+ {
+ e_redir_plus_two(ptr, msh);
+ }
+ ptr = ptr->next;
+ }
+}
diff --git a/src/m_redirs.h b/src/e_redirs.h
index fc33758..7fddea0 100644
--- a/src/m_redirs.h
+++ b/src/e_redirs.h
@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* m_redirs.h :+: :+: :+: */
+/* e_redirs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
@@ -15,6 +15,6 @@
#include "s_struct.h"
-void dup_redirs(const t_com *ptr, t_msh *msh);
+void e_dup_redirs(const t_com *com, t_msh *msh);
#endif
diff --git a/src/m_redirs.c b/src/m_redirs.c
deleted file mode 100644
index 40a6646..0000000
--- a/src/m_redirs.c
+++ /dev/null
@@ -1,61 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* m_redirs.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
-/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
-/* */
-/* ************************************************************************** */
-
-#include <libft.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <errno.h>
-
-#include "f_fail.h"
-#include "s_destroy.h"
-#include "s_line.h"
-#include "s_struct.h"
-
-void dup_redirs(const t_com *ptr, t_msh *msh)
-{
- (void)ptr;
- (void)msh;
- /* int32_t fd; */
-
- /* if (ptr->redir == -1) */
- /* { */
- /* if ((fd = open(ptr->rdrpath, O_RDONLY)) == -1) */
- /* { */
- /* f_redir(ptr->rdrpath, msh); */
- /* return ; */
- /* } */
- /* dup2(fd, STDIN_FILENO); */
- /* close(fd); */
- /* } */
- /* else if (ptr->redir == 1) */
- /* { */
- /* if ((fd = open(ptr->rdrpath, */
- /* O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) */
- /* { */
- /* f_redir(ptr->rdrpath, msh); */
- /* return ; */
- /* } */
- /* dup2(fd, ptr->rdrfd); */
- /* close(fd); */
- /* } */
- /* else if (ptr->redir == 2) */
- /* { */
- /* if ((fd = open(ptr->rdrpath, */
- /* O_CREAT | O_APPEND | O_WRONLY, 0644)) == -1) */
- /* { */
- /* f_redir(ptr->rdrpath, msh); */
- /* return ; */
- /* } */
- /* dup2(fd, ptr->rdrfd); */
- /* close(fd); */
- /* } */
-}
diff --git a/src/p_redirs.c b/src/p_redirs.c
index 3f9e866..cf4cf1d 100644
--- a/src/p_redirs.c
+++ b/src/p_redirs.c
@@ -13,11 +13,13 @@
#include <libft.h>
#include <stdint.h>
#include <stddef.h>
+#include <unistd.h>
#include <limits.h>
#include "d_define.h"
#include "s_lredir.h"
#include "s_struct.h"
+#include "s_lredir.h"
#include "u_utils.h"
#include "u_parse.h"
@@ -26,7 +28,15 @@ static void p_append_redir(const char path[],
int8_t redir,
t_com *com)
{
- struct s_lredir *rdr;
+ struct s_lredir *new;
+
+ new = s_lredir_new(path, fd, redir);
+ if (new == NULL)
+ {
+ return ;
+ }
+ s_lredir_add_back(&com->rdr, new);
+
}
static size_t p_get_path(char path[],
@@ -88,12 +98,12 @@ static int8_t p_get_redir(char word[], char *ptr, t_com *com)
pos[0] = 0;
pos[1] = 0;
- if ((fd = p_get_fd(word, ptr)) < 0)
- fd = 0;
+ if ((fd = p_get_fd(word, ptr)) <= 0)
+ fd = STDOUT_FILENO;
redir = (*ptr == '>') ? (1) : (-1);
redir = (redir == 1 && *(ptr + 1) == '>') ? (2) : (redir);
redir = (redir == -1 && *(ptr + 1) == '<') ? (-2) : (redir);
- if (fd == 0)
+ if (fd == STDOUT_FILENO)
pos[0] = (ptr - word);
else
pos[0] = (ptr - word) - ft_intlen(fd);
diff --git a/src/s_com.c b/src/s_com.c
index 45a6304..527829d 100644
--- a/src/s_com.c
+++ b/src/s_com.c
@@ -109,10 +109,8 @@ t_com
com->rdr = NULL;
nword[0] = C_NUL;
ft_strlcpy(nword, word, ARG_MAX);
- ft_printf("BEFORE: [%s]\n", nword);
if (p_redirs(nword, &com) != 0)
return (NULL);
- ft_printf("AFTER: [%s]\n", nword);
if (msh->alias != NULL)
{
ret = p_subst_alias(nword, TRUE, msh);