ajout d'un indicateur pour ne pas fermer le file descripto du fichier, mauvaise idee il faut le rouvrir je pense

This commit is contained in:
hugogogo
2021-10-20 00:15:50 +02:00
parent 4d812cd346
commit f2c8898fcc
4 changed files with 14 additions and 10 deletions

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */ /* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
/* Updated: 2021/10/19 21:59:24 by hulamy ### ########.fr */ /* Updated: 2021/10/19 23:41:25 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -32,7 +32,7 @@ char *ft_strjoinfree_s2(const char *s1, char *s2);
void pipes_hugo(char *input, t_all *c); void pipes_hugo(char *input, t_all *c);
// parser hugo // parser hugo
void **cmd_path(char **argv, char **envp); void **cmd_path(char **argv, char **envp);
int handle_fd(char **input, int i, int fdin, int *fd_in, int *fd_out); int handle_fd(char **input, int i, int fdin, t_cmd *cmd);
t_list *parser(char *input, char **envp); t_list *parser(char *input, char **envp);
#endif #endif

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */ /* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */
/* Updated: 2021/10/19 21:58:02 by hulamy ### ########.fr */ /* Updated: 2021/10/19 23:59:00 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -25,6 +25,7 @@ typedef struct s_cmd
char **argv; char **argv;
pid_t pid; pid_t pid;
void *builtin; void *builtin;
int fd_redirect;
int fd_in; int fd_in;
int fd_out; int fd_out;
} t_cmd; } t_cmd;

View File

@@ -30,6 +30,7 @@ void exec_cmd(char **envp, t_list *cmd_list)
{ {
if (cmd->fd_in != 0) if (cmd->fd_in != 0)
close(cmd->fd_in); close(cmd->fd_in);
// if (cmd->fd_out != 1 && cmd->fd_redirect == 1)
if (cmd->fd_out != 1) if (cmd->fd_out != 1)
close(cmd->fd_out); close(cmd->fd_out);
} }

View File

@@ -23,20 +23,21 @@ void **cmd_path(char **argv, char **envp)
return (NULL); return (NULL);
} }
int handle_fd(char **input, int i, int fdin, int *fd_in, int *fd_out) int handle_fd(char **input, int i, int fdin, t_cmd *cmd)
{ {
int *pipes_fd; int *pipes_fd;
int next_in; int next_in;
char *tmp; char *tmp;
*fd_in = fdin; cmd->fd_in = fdin;
*fd_out = 1; cmd->fd_out = 1;
cmd->fd_redirect = 1;
next_in = 0; next_in = 0;
if (input[i + 1]) if (input[i + 1])
{ {
pipes_fd = calloc(2, sizeof(int)); pipes_fd = calloc(2, sizeof(int));
pipe(pipes_fd); pipe(pipes_fd);
*fd_out = pipes_fd[1]; cmd->fd_out = pipes_fd[1];
next_in = pipes_fd[0]; next_in = pipes_fd[0];
} }
tmp = ft_strchr(input[i], '>'); tmp = ft_strchr(input[i], '>');
@@ -44,8 +45,9 @@ int handle_fd(char **input, int i, int fdin, int *fd_in, int *fd_out)
{ {
tmp[0] = '\0'; tmp[0] = '\0';
tmp = ft_strtrim(tmp + 2, " "); // +2 for "> " tmp = ft_strtrim(tmp + 2, " "); // +2 for "> "
*fd_out = open(tmp, O_WRONLY | O_TRUNC); cmd->fd_out = open(tmp, O_WRONLY | O_TRUNC);
next_in = *fd_out; next_in = cmd->fd_out;
cmd->fd_redirect = cmd->fd_out;
} }
return (next_in); return (next_in);
} }
@@ -65,7 +67,7 @@ t_list *parser(char *input, char **envp)
while (input_split[i]) while (input_split[i])
{ {
element = calloc(1, sizeof(t_cmd)); element = calloc(1, sizeof(t_cmd));
tmp_fd = handle_fd(input_split, i, tmp_fd, &(element->fd_in), &(element->fd_out)); tmp_fd = handle_fd(input_split, i, tmp_fd, element);
element->argv = ft_split(input_split[i], ' '); element->argv = ft_split(input_split[i], ' ');
element->builtin = cmd_path(element->argv, envp); element->builtin = cmd_path(element->argv, envp);
ft_lstadd_back(&cmd, ft_lstnew(element)); ft_lstadd_back(&cmd, ft_lstnew(element));