mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
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:
parent
1499eaa985
commit
2ae001e00b
3 changed files with 23 additions and 21 deletions
|
|
@ -6,13 +6,14 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/01 16:37:21 by khais #+# #+# */
|
||||
/* Updated: 2025/04/16 17:29:06 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/18 14:12:50 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "builtins.h"
|
||||
#include "libft.h"
|
||||
#include "simple_cmd_execute.h"
|
||||
#include "handle_redirections.h"
|
||||
|
||||
t_builtin_type get_builtin(t_simple_cmd *cmd)
|
||||
{
|
||||
|
|
@ -36,8 +37,7 @@ t_builtin_type get_builtin(t_simple_cmd *cmd)
|
|||
return (BUILTIN_INVALID);
|
||||
}
|
||||
|
||||
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app,
|
||||
bool should_exit)
|
||||
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app)
|
||||
{
|
||||
t_builtin_type type;
|
||||
static t_builtin_type (*builtins[])(t_simple_cmd *, t_minishell *) = {
|
||||
|
|
@ -53,7 +53,7 @@ t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app,
|
|||
|
||||
type = get_builtin(cmd);
|
||||
builtins[type](cmd, app);
|
||||
if (should_exit && type != BUILTIN_INVALID)
|
||||
exit(app->last_return_value);
|
||||
if (type != BUILTIN_INVALID)
|
||||
restore_std_fds(app);
|
||||
return (type);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/31 14:16:13 by khais #+# #+# */
|
||||
/* Updated: 2025/04/16 16:57:16 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/18 14:03:06 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -25,7 +25,6 @@ t_builtin_type builtin_env(t_simple_cmd *cmd, t_minishell *app);
|
|||
t_builtin_type builtin_unset(t_simple_cmd *cmd, t_minishell *app);
|
||||
|
||||
t_builtin_type get_builtin(t_simple_cmd *cmd);
|
||||
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app,
|
||||
bool should_exit);
|
||||
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app);
|
||||
|
||||
#endif // BUILTINS_H
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue