simple_cmd_execute: handle redirections

I am very uncertain on if this is actually correct, but I am unable to test it
very much atm
This commit is contained in:
Khaïs COLIN 2025-04-07 11:59:44 +02:00
parent 6f75f2d181
commit bb1390aac5
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
/* Updated: 2025/04/04 17:09:39 by khais ### ########.fr */
/* Updated: 2025/04/04 19:56:34 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,7 @@
#include "libft.h"
#include "../../subst/subst.h"
#include "../common/do_waitpid.h"
#include "../../minishell.h"
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
@ -27,6 +28,40 @@ static void command_not_found(t_simple_cmd *cmd)
cmd->words->word->word);
}
static t_redirect *do_redirection(t_redirect *redirection)
{
int fd;
if (redirection->type == FT_HEREDOC)
fd = redirection->redirectee.dest;
else
{
fd = open(redirection->redirectee.filename->word,
redirection->open_flags, redirection->c_flags);
if (fd < 0)
return (perror("minishell: open"), NULL);
}
if (dup2(fd, redirection->source) < 0)
return (perror("minishell: dup2"), NULL);
close(fd);
return (redirection);
}
static t_simple_cmd *handle_redirections(t_simple_cmd *cmd, t_minishell *app)
{
t_redirect *redirections;
(void)app;
redirections = cmd->redirections;
while (redirections != NULL)
{
if (do_redirection(redirections) == NULL)
return (NULL);
redirections = redirections->next;
}
return (cmd);
}
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
{
char *exe;
@ -34,6 +69,8 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL)
return ;
if (handle_redirections(cmd, app) == NULL)
return ;
if (execute_builtin(cmd, app) != BUILTIN_INVALID)
return ;
exe = get_cmdpath(cmd->words->word->word, app);