summaryrefslogtreecommitdiffstats
path: root/src/p_redirs.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-09-26 20:12:09 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-09-26 20:12:09 +0200
commit04d0fd969ba2c766928393be9482f33a2acf0774 (patch)
tree7b8f729e49481fb18053fdcd2c984933d967936a /src/p_redirs.c
parentFixed qwe>~/qweqwe (diff)
download42-minishell-04d0fd969ba2c766928393be9482f33a2acf0774.tar.gz
42-minishell-04d0fd969ba2c766928393be9482f33a2acf0774.tar.bz2
42-minishell-04d0fd969ba2c766928393be9482f33a2acf0774.tar.xz
42-minishell-04d0fd969ba2c766928393be9482f33a2acf0774.tar.zst
42-minishell-04d0fd969ba2c766928393be9482f33a2acf0774.zip
<<EOF in progress
Diffstat (limited to 'src/p_redirs.c')
-rw-r--r--src/p_redirs.c58
1 files changed, 30 insertions, 28 deletions
diff --git a/src/p_redirs.c b/src/p_redirs.c
index 581ac48..c978212 100644
--- a/src/p_redirs.c
+++ b/src/p_redirs.c
@@ -17,6 +17,7 @@
#include <limits.h>
#include "d_define.h"
+#include "p_redirs.h"
#include "s_lredir.h"
#include "s_struct.h"
#include "s_lredir.h"
@@ -37,38 +38,38 @@ static void p_append_redir(const char path[],
return ;
}
s_lredir_add_back(&com->rdr, new);
-
+ if (redir == -2)
+ p_get_heredoc(path, msh->fd);
}
static size_t p_get_path(char path[],
- char *ptr,
- const char word[],
+ struct s_rdr_tmp tmp,
t_msh *msh)
{
- char home[PATH_MAX];
+ char h[PATH_MAX];
size_t pos[2];
size_t hlen;
size_t len;
hlen = 0;
- while (ft_ischarset("<>", *ptr) == TRUE)
- ptr++;
- while (*ptr != C_NUL && ft_iswhitespace(*ptr) == TRUE)
- ptr++;
- if (*ptr == C_TILDE && u_get_var_value(home, "$HOME", PATH_MAX, msh) == 0)
+ tmp.redir = (tmp.redir < 0) ? (-tmp.redir) : (tmp.redir);
+ tmp.ptr += tmp.redir;
+ while (*tmp.ptr != C_NUL && ft_iswhitespace(*tmp.ptr) == TRUE)
+ tmp.ptr++;
+ if (*tmp.ptr == C_TILDE && u_get_var_value(h, "$HOME", PATH_MAX, msh) == 0)
{
- ft_strlcpy(path, home, PATH_MAX);
- hlen = ft_strlen(home);
- ptr++;
+ ft_strlcpy(path, h, PATH_MAX);
+ hlen = ft_strlen(h);
+ tmp.ptr++;
}
- pos[0] = (ptr - word);
- while (*ptr != C_NUL && ft_iswhitespace(*ptr) == FALSE &&
- u_is_not_escaped(word, ptr) == TRUE)
- ptr++;
- pos[1] = (ptr - word);
+ pos[0] = (tmp.ptr - tmp.word);
+ while (*tmp.ptr != C_NUL && ft_iswhitespace(*tmp.ptr) == FALSE &&
+ u_is_not_escaped(tmp.word, tmp.ptr) == TRUE)
+ tmp.ptr++;
+ pos[1] = (tmp.ptr - tmp.word);
len = (pos[1] - pos[0]);
len = ((hlen + len + 1) > PATH_MAX) ? (PATH_MAX - 1) : (len);
- ft_strlcpy(path + hlen, word + pos[0], len + 1);
+ ft_strlcpy(path + hlen, tmp.word + pos[0], len + 1);
return (pos[1]);
}
@@ -95,15 +96,15 @@ static int32_t p_get_fd(const char word[], char *ptr)
return (ft_atoi(digit));
}
-static int8_t p_get_redir(char word[], char *ptr, t_com *com, t_msh *msh)
+static void p_get_redir(char word[], char *ptr, t_com *com, t_msh *msh)
{
- char path[PATH_MAX];
- size_t pos[2];
- int32_t fd;
- int8_t redir;
+ struct s_rdr_tmp tmp;
+ char path[PATH_MAX];
+ size_t pos[2];
+ int32_t fd;
+ int8_t redir;
pos[0] = 0;
- pos[1] = 0;
if ((fd = p_get_fd(word, ptr)) <= 0)
fd = STDOUT_FILENO;
redir = (*ptr == '>') ? (1) : (-1);
@@ -113,11 +114,13 @@ static int8_t p_get_redir(char word[], char *ptr, t_com *com, t_msh *msh)
pos[0] = (ptr - word);
else
pos[0] = (ptr - word) - ft_intlen(fd);
- pos[1] = p_get_path(path, ptr, word, msh);
+ tmp.word = word;
+ tmp.ptr = ptr;
+ tmp.redir = redir;
+ pos[1] = p_get_path(path, tmp, msh);
(void)ft_memmove(word + pos[0],
word + pos[1], (ft_strlen(word + pos[1]) + 1) * sizeof(char));
p_append_redir(path, fd, redir, com);
- return (0);
}
int8_t p_redirs(char word[], t_com **com, t_msh *msh)
@@ -135,8 +138,7 @@ int8_t p_redirs(char word[], t_com **com, t_msh *msh)
mode = u_meet_squote(word, ptr, mode);
else if (mode == Q_NONE && (*ptr == '<' || *ptr == '>') == 1)
{
- if (p_get_redir(word, ptr, *com, msh) != 0)
- return (1);
+ p_get_redir(word, ptr, *com, msh);
ptr = word;
}
ptr++;