Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions inc/invoke_cmd/pipeline_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 14:32:27 by dayano #+# #+# */
/* Updated: 2025/05/04 19:18:38 by ttsubo ### ########.fr */
/* Updated: 2025/05/09 11:33:04 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,7 +18,6 @@
# include <stdbool.h>

// pipeline_helper.c
void redirect_stdout(t_cmd *cmd);
bool is_redirect(t_cmd *cmd);
bool is_head(t_cmd *cmd, t_cmd *cmd_head);
bool is_tail(t_cmd *cmd);
Expand Down
8 changes: 4 additions & 4 deletions inc/invoke_cmd/redirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/28 15:20:03 by dayano #+# #+# */
/* Updated: 2025/05/04 19:19:04 by ttsubo ### ########.fr */
/* Updated: 2025/05/09 11:32:11 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,8 +15,8 @@

# include "common.h"

void redirect(t_cmd *cmd);
void redirect_stdout(t_cmd *cmd);
void redirect_stdin(t_cmd *cmd);
bool redirect(t_cmd *cmd);
bool redirect_stdout(t_cmd *cmd);
bool redirect_stdin(t_cmd *cmd);

#endif
14 changes: 9 additions & 5 deletions src/invoke_cmd/invoke_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* invoke_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/20 21:19:33 by dayano #+# #+# */
/* Updated: 2025/05/05 17:39:02 by dayano ### ########.fr */
/* Updated: 2025/05/09 11:43:50 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -65,12 +65,14 @@ int execute_builtin(t_cmd *cmd, t_minish *minish)
int exec_unit_builtin(t_cmd *cmd, t_minish *minish)
{
t_cmd *builtin_cmd;
bool redir_result;

redir_result = true;
builtin_cmd = cmd;
if (cmd && is_redirect(cmd))
{
redirect(cmd);
if (cmd->next)
redir_result = redirect(cmd);
if (redir_result && cmd->next)
{
cmd = cmd->next;
builtin_cmd = cmd;
Expand All @@ -79,7 +81,9 @@ int exec_unit_builtin(t_cmd *cmd, t_minish *minish)
return (print_error(cmd->argv[0]), EXIT_FAILURE);
}
if (cmd->next && is_redirect(cmd->next))
redirect(cmd->next);
redir_result = redirect(cmd->next);
if (!redir_result)
return (EXIT_FAILURE);
return (execute_builtin(builtin_cmd, minish));
}

Expand Down
55 changes: 31 additions & 24 deletions src/invoke_cmd/redirect.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
/* ::: :::::::: */
/* redirect.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/28 15:19:19 by dayano #+# #+# */
/* Updated: 2025/04/28 16:57:45 by dayano ### ########.fr */
/* Updated: 2025/05/09 14:23:25 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "main.h"

void redirect_stdout(t_cmd *cmd)
bool redirect_stdout(t_cmd *cmd)
{
int fd;
char *path;
Expand All @@ -23,19 +23,22 @@ void redirect_stdout(t_cmd *cmd)
else if (cmd->type == REDIR_APPEND)
fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
else
return ;
return (false);
if (fd < 0)
{
ft_putstr_fd("minish: ", STDERR_FILENO);
perror(path);
return ;
return (false);
}
if (dup2(fd, STDOUT_FILENO) < 0)
{
ft_putstr_fd("minish: ", STDERR_FILENO);
perror(path);
close(fd);
return (false);
}
close(fd);
return (true);
}

bool is_limiter(int fd, char *limiter)
Expand Down Expand Up @@ -81,39 +84,43 @@ void here_doc(char *limiter)
close(pipefd[0]);
}

void redirect_stdin(t_cmd *cmd)
bool redirect_stdin(t_cmd *cmd)
{
int fd;
char *path;

path = cmd->argv[0];
if (cmd->type == REDIR_HEREDOC)
return (here_doc(path), true);
if (cmd->type != REDIR_IN)
return (true);
fd = open(path, O_RDONLY);
if (fd < 0)
{
here_doc(cmd->argv[0]);
return ;
ft_putstr_fd("minish: ", STDERR_FILENO);
perror(path);
return (false);
}
else if (cmd->type == REDIR_IN)
if (dup2(fd, STDIN_FILENO) < 0)
{
fd = open(path, O_RDONLY);
if (fd < 0)
{
ft_putstr_fd("minish: ", STDERR_FILENO);
perror(path);
return ;
}
if (dup2(fd, STDIN_FILENO) < 0)
{
ft_putstr_fd("minish: ", STDERR_FILENO);
perror(path);
}
ft_putstr_fd("minish: ", STDERR_FILENO);
perror(path);
close(fd);
return (false);
}
close(fd);
return (true);
}

void redirect(t_cmd *cmd)
/**
* @note This function is only called if the caller's is_redirect is true
* @note false if the internal function is not executed.
*/
bool redirect(t_cmd *cmd)
{
if (cmd->type == REDIR_IN || cmd->type == REDIR_HEREDOC)
redirect_stdin(cmd);
return (redirect_stdin(cmd));
if (cmd->type == REDIR_OUT || cmd->type == REDIR_APPEND)
redirect_stdout(cmd);
return (redirect_stdout(cmd));
return (false);
}