output redirections
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
|
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/04/02 17:53:51 by tomoron ### ########.fr */
|
/* Updated: 2024/04/03 00:15:39 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/04/02 17:39:29 by tomoron ### ########.fr */
|
/* Updated: 2024/04/03 01:24:10 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -99,7 +99,7 @@ void remove_command_from_msh(t_msh *msh);
|
|||||||
void ft_exit(t_msh *msh, int error_code);
|
void ft_exit(t_msh *msh, int error_code);
|
||||||
void signal_handler_command(int signum);
|
void signal_handler_command(int signum);
|
||||||
void ft_exit(t_msh *msh, int exit_code);
|
void ft_exit(t_msh *msh, int exit_code);
|
||||||
void redirect_output(t_msh *msh, int i);
|
void redirect_output(t_msh *msh);
|
||||||
char **split_paths_from_env(t_env *env);
|
char **split_paths_from_env(t_env *env);
|
||||||
int add_return_code_to_str(char *res);
|
int add_return_code_to_str(char *res);
|
||||||
void redirect_input(t_msh *msh);
|
void redirect_input(t_msh *msh);
|
||||||
|
@ -6,15 +6,14 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/05 19:10:52 by marde-vr #+# #+# */
|
/* Created: 2024/03/05 19:10:52 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/04/02 02:08:43 by tomoron ### ########.fr */
|
/* Updated: 2024/04/03 01:25:03 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
void redirect_output(t_msh *msh, int i)
|
void redirect_output(t_msh *msh)
|
||||||
{
|
{
|
||||||
(void)i;
|
|
||||||
if (msh->out_type != PIPE)
|
if (msh->out_type != PIPE)
|
||||||
{
|
{
|
||||||
if (dup2(msh->out_fd, 1) < 0)
|
if (dup2(msh->out_fd, 1) < 0)
|
||||||
@ -27,15 +26,12 @@ void redirect_output(t_msh *msh, int i)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void open_out_file(t_msh *msh, t_cmd **cur_cmd)
|
void open_out_file(t_msh *msh, t_cmd **cur_cmd, char *filename)
|
||||||
{
|
{
|
||||||
msh->out_type = (*cur_cmd)->cmd_type;
|
|
||||||
if (msh->out_type == RED_O)
|
if (msh->out_type == RED_O)
|
||||||
msh->out_fd = open((*cur_cmd)->next->value,
|
msh->out_fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
|
||||||
O_CREAT | O_WRONLY | O_TRUNC, 0644);
|
|
||||||
if (msh->out_type == RED_O_APP)
|
if (msh->out_type == RED_O_APP)
|
||||||
msh->out_fd = open((*cur_cmd)->next->value, O_CREAT | O_RDWR | O_APPEND,
|
msh->out_fd = open(filename, O_CREAT | O_RDWR | O_APPEND, 0644);
|
||||||
0644);
|
|
||||||
if (msh->out_fd == -1)
|
if (msh->out_fd == -1)
|
||||||
{
|
{
|
||||||
g_return_code = 1;
|
g_return_code = 1;
|
||||||
@ -44,10 +40,9 @@ void open_out_file(t_msh *msh, t_cmd **cur_cmd)
|
|||||||
}
|
}
|
||||||
if ((*cur_cmd)->cmd_type != PIPE)
|
if ((*cur_cmd)->cmd_type != PIPE)
|
||||||
{
|
{
|
||||||
while ((*cur_cmd)->next && (*cur_cmd)->next->cmd_type == CMD)
|
while ((*cur_cmd)->next && is_cmd_type((*cur_cmd)->next))
|
||||||
*cur_cmd = (*cur_cmd)->next;
|
*cur_cmd = (*cur_cmd)->next;
|
||||||
if ((*cur_cmd)->next && ((*cur_cmd)->next->cmd_type == RED_O
|
if ((*cur_cmd)->next && is_output_type((*cur_cmd)->next))
|
||||||
|| (*cur_cmd)->next->cmd_type == RED_O_APP))
|
|
||||||
get_out_type(msh, *cur_cmd);
|
get_out_type(msh, *cur_cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,15 +50,22 @@ void open_out_file(t_msh *msh, t_cmd **cur_cmd)
|
|||||||
void get_out_type(t_msh *msh, t_cmd *cmds)
|
void get_out_type(t_msh *msh, t_cmd *cmds)
|
||||||
{
|
{
|
||||||
t_cmd *cur_cmd;
|
t_cmd *cur_cmd;
|
||||||
|
t_token *filename;
|
||||||
|
|
||||||
msh->out_type = CMD;
|
msh->out_type = CMD;
|
||||||
msh->out_fd = 0;
|
msh->out_fd = 0;
|
||||||
cur_cmd = cmds;
|
cur_cmd = cmds;
|
||||||
while (cur_cmd && cur_cmd->next && cur_cmd->cmd_type != AND
|
while (cur_cmd && cur_cmd->next && !is_operand_type(cur_cmd))
|
||||||
&& cur_cmd->cmd_type != OR)
|
|
||||||
cur_cmd = cur_cmd->next;
|
cur_cmd = cur_cmd->next;
|
||||||
if (cur_cmd->cmd_type == CMD || cur_cmd->cmd_type == PAREN)
|
if (cur_cmd->cmd_type == CMD || cur_cmd->cmd_type == PAREN)
|
||||||
msh->out_type = 0;
|
msh->out_type = 0;
|
||||||
else
|
else
|
||||||
open_out_file(msh, &cur_cmd);
|
{
|
||||||
|
msh->out_type = cur_cmd->cmd_type;
|
||||||
|
filename = parse_command(cur_cmd->value, msh->env);
|
||||||
|
if(!filename)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
open_out_file(msh, &cur_cmd, filename->value);
|
||||||
|
free_token(filename);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */
|
/* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/03/27 14:57:21 by tomoron ### ########.fr */
|
/* Updated: 2024/04/02 23:39:04 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -71,9 +71,6 @@ void get_path(t_msh *msh, int *found)
|
|||||||
|
|
||||||
char *remove_path(char *token)
|
char *remove_path(char *token)
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (ft_strchr(token, '/'))
|
while (ft_strchr(token, '/'))
|
||||||
token++;
|
token++;
|
||||||
return (token);
|
return (token);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */
|
/* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/04/02 13:24:17 by babonnet ### ########.fr */
|
/* Updated: 2024/04/03 01:25:26 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ void child(t_msh *msh, char **cmd_args, int i)
|
|||||||
redirect_input(msh);
|
redirect_input(msh);
|
||||||
if (msh->out_type == PIPE || msh->out_type == RED_O
|
if (msh->out_type == PIPE || msh->out_type == RED_O
|
||||||
|| msh->out_type == RED_O_APP)
|
|| msh->out_type == RED_O_APP)
|
||||||
redirect_output(msh, i);
|
redirect_output(msh);
|
||||||
close_pipe_fds(msh, i);
|
close_pipe_fds(msh, i);
|
||||||
execute_command(msh, cmd_args);
|
execute_command(msh, cmd_args);
|
||||||
close(0);
|
close(0);
|
||||||
|
Reference in New Issue
Block a user