🔨」 fix(exec): fds for non-forked cmds

This commit is contained in:
y-syo
2024-07-18 14:43:30 +02:00
parent 705a49b6db
commit 6ac6f778e3
6 changed files with 26 additions and 25 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/15 21:03:31 by mmoussou #+# #+# */
/* Updated: 2024/07/18 14:27:01 by mmoussou ### ########.fr */
/* Updated: 2024/07/18 14:41:32 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,32 +14,32 @@
#include "builtins.h"
#include "error_msg.h"
void exec_cmd(char *cmd, char **argv, char **env, t_env *env_t)
void exec_cmd(t_cmd *cmd, char **env, t_env *env_t)
{
int i;
i = is_in_builtins(cmd);
i = is_in_builtins(cmd->cmd);
if (i)
{
if (i == 5)
ft_echo(argv + 1);
ft_echo(cmd->argv + 1, cmd->outfile);
if (i == 2)
ft_cd(env_t, argv[1]);
ft_cd(env_t, (cmd->argv)[1]);
if (i == 6)
ft_pwd();
if (i == 4)
ft_export(argv + 1, env_t);
ft_export(cmd->argv + 1, env_t, cmd->outfile);
if (i == 3)
ft_unset(&argv[1], env_t);
ft_unset(&(cmd->argv)[1], env_t);
if (i == 7)
ft_env(env_t);
if (i == 1)
ft_exit(argv, ft_arrlen(argv), env, env_t);
ft_exit(cmd->argv, ft_arrlen(cmd->argv), env, env_t);
}
else
{
ft_envclear(&env_t, free);
execve(cmd, argv, env);
execve(cmd->cmd, cmd->argv, env);
}
}
@ -58,7 +58,7 @@ void __fork_single_cmd(t_cmd *cmd, char **env, t_env *env_t, t_exec exec)
if (exec.pipe_fd[0] != -1)
close(exec.pipe_fd[1]);
if (exec.status != -1)
exec_cmd(cmd->cmd, cmd->argv, env, env_t);
exec_cmd(cmd, env, env_t);
}
int exec_single_cmd_execution(t_cmd *cmd, char **env, t_env *env_t, t_exec exec)
@ -66,14 +66,14 @@ int exec_single_cmd_execution(t_cmd *cmd, char **env, t_env *env_t, t_exec exec)
int fork_pid;
if (is_in_builtins(cmd->cmd) > 0 && is_in_builtins(cmd->cmd) < 5)
__fork_single_cmd(cmd, env, env_t, exec);
exec_cmd(cmd, env, env_t);
if (is_in_builtins(cmd->cmd) > 0 && is_in_builtins(cmd->cmd) < 5)
return (0);
fork_pid = fork();
if (!fork_pid)
{
__fork_single_cmd(cmd, env, env_t, exec);
free_exec(env_t, env);
free_exit(env_t, env);
exit(get_exit_code(-1));
}
return (fork_pid);