fix(exec/redirections): did not correctly restore fds when executing builtins

This caused redirections to keep stdout redirected to the outfile, which caused
problems with displaying the prmpt.

The shell hangs, but exits with C-d

Builtins affected:
echo hi > outfile
pwd > outfile
env > outfile
This commit is contained in:
Khaïs COLIN 2025-04-18 14:07:48 +02:00
parent 1499eaa985
commit 2ae001e00b
3 changed files with 23 additions and 21 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
/* Updated: 2025/04/17 17:51:25 by khais ### ########.fr */
/* Updated: 2025/04/18 14:03:39 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,7 +26,6 @@
#include "../../postprocess/fieldsplit/fieldsplit.h"
#include "../../postprocess/expansion/expand_wildcard.h"
#include "simple_cmd_execute_debug.h"
#include "../../parser/simple_cmd/simple_cmd.h"
#include "handle_redirections.h"
static void command_not_found(t_simple_cmd *cmd, t_minishell *app)
@ -53,21 +52,11 @@ static t_simple_cmd *post_process_command(t_simple_cmd *cmd, t_minishell *app)
return (cmd);
}
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app,
bool should_exit)
static void exec_external_cmd(t_simple_cmd *cmd, t_minishell *app)
{
char *exe;
int pid;
if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL)
return ;
if (post_process_command(cmd, app) == NULL)
return ;
simple_cmd_execute_debug(cmd, app);
if (handle_redirections(cmd, app) == NULL)
return ;
if (execute_builtin(cmd, app, should_exit) != BUILTIN_INVALID)
return ;
exe = get_cmdpath(cmd->words->word->word, app);
if (exe == NULL)
return (command_not_found(cmd, app));
@ -77,6 +66,20 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app,
restore_std_fds(app);
free(exe);
do_waitpid(app, pid);
}
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app,
bool should_exit)
{
if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL)
return ;
if (post_process_command(cmd, app) == NULL)
return ;
simple_cmd_execute_debug(cmd, app);
if (handle_redirections(cmd, app) == NULL)
return ;
if (execute_builtin(cmd, app) == BUILTIN_INVALID)
exec_external_cmd(cmd, app);
if (should_exit)
exit(app->last_return_value);
}