🔨」 fix(exec/exec_cmds): fixed forks and all

This commit is contained in:
yosyo
2024-07-15 21:01:30 +02:00
parent 1183a9f835
commit 01e0f4fd92
3 changed files with 76 additions and 10 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */ /* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */
/* Updated: 2024/07/14 17:39:35 by adjoly ### ########.fr */ /* Updated: 2024/07/15 19:28:57 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,6 +37,7 @@ void print_return_value(int return_code);
void __wait(int i); void __wait(int i);
void __close(void *content); void __close(void *content);
int check_file(char *cmd, char *input); int check_file(char *cmd, char *input);
void free_exec(t_env *env, char **env_array);
/** /**
* @brief spawn a heredoc * @brief spawn a heredoc

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */ /* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */
/* Updated: 2024/07/15 15:47:01 by mmoussou ### ########.fr */ /* Updated: 2024/07/15 20:58:56 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -40,8 +40,11 @@ void exec_cmd(char *cmd, char **argv, char **env, t_env *env_t)
ft_exit(argv, ft_arrlen(argv), env, env_t); ft_exit(argv, ft_arrlen(argv), env, env_t);
} }
else else
{
ft_envclear(&env_t, free);
execve(cmd, argv, env); execve(cmd, argv, env);
} }
}
void __fork_single_cmd(t_cmd *cmd, char **env, t_env *env_t, t_exec exec) void __fork_single_cmd(t_cmd *cmd, char **env, t_env *env_t, t_exec exec)
{ {
@ -62,6 +65,35 @@ void __fork_single_cmd(t_cmd *cmd, char **env, t_env *env_t, t_exec exec)
exit(-1); exit(-1);
} }
int exec_fork_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2])
{
t_exec exec;
int fork_pid;
char *input;
input = ft_strdup(cmd->cmd);
exec.pipe_fd[0] = pipe_fd[0];
exec.pipe_fd[1] = pipe_fd[1];
exec.status = switch_cmd_path(cmd, env_t);
if (exec.status == -1 || !input || check_file(cmd->cmd, input))
{
if (exec.status == -1)
printf("minishell : command not found: %s\n", input);
get_exit_code(127);
free(input);
return (-1);
}
free(input);
fork_pid = fork();
if (!fork_pid)
{
__fork_single_cmd(cmd, env, env_t, exec);
free_exec(env_t, env);
exit(get_exit_code(-1));
}
return (fork_pid);
}
int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2]) int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2])
{ {
t_exec exec; t_exec exec;
@ -76,23 +108,37 @@ int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2])
{ {
if (exec.status == -1) if (exec.status == -1)
printf("minishell : command not found: %s\n", input); printf("minishell : command not found: %s\n", input);
get_exit_code(127);
free(input); free(input);
return (-1); return (-1);
} }
free(input); free(input);
if (is_in_builtins(cmd->cmd) > 0) if (is_in_builtins(cmd->cmd) > 0)
{
exec_cmd(cmd->cmd, cmd->argv, env, env_t); exec_cmd(cmd->cmd, cmd->argv, env, env_t);
if (is_in_builtins(cmd->cmd) > 0)
return (0); return (0);
}
fork_pid = fork(); fork_pid = fork();
if (!fork_pid) if (!fork_pid)
__fork_single_cmd(cmd, env, env_t, exec); __fork_single_cmd(cmd, env, env_t, exec);
return (fork_pid); return (fork_pid);
} }
t_exec exec_pipe_unforked(t_exec exec, t_list *list_cmd, t_env *env)
{
exec.status = exec_single_cmd(list_cmd->content, exec.env_array, \
env, exec.pipe_fd);
__sig(exec.status);
if (((t_cmd *)(list_cmd->content))->outfile != STDOUT_FILENO)
close(((t_cmd *)(list_cmd->content))->outfile);
if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO)
close(((t_cmd *)(list_cmd->content))->infile);
return (exec);
}
t_exec exec_pipe(t_exec exec, t_list *list_cmd, t_env *env) t_exec exec_pipe(t_exec exec, t_list *list_cmd, t_env *env)
{ {
if (!list_cmd->next)
return (exec_pipe_unforked(exec, list_cmd, env));
while (list_cmd->next) while (list_cmd->next)
{ {
exec.status = pipe(exec.pipe_fd); exec.status = pipe(exec.pipe_fd);
@ -103,20 +149,17 @@ t_exec exec_pipe(t_exec exec, t_list *list_cmd, t_env *env)
((t_cmd *)(list_cmd->content))->outfile = exec.pipe_fd[1]; ((t_cmd *)(list_cmd->content))->outfile = exec.pipe_fd[1];
if (((t_cmd *)(list_cmd->next->content))->infile == STDIN_FILENO) if (((t_cmd *)(list_cmd->next->content))->infile == STDIN_FILENO)
((t_cmd *)(list_cmd->next->content))->infile = exec.pipe_fd[0]; ((t_cmd *)(list_cmd->next->content))->infile = exec.pipe_fd[0];
exec.status = exec_single_cmd(list_cmd->content, exec.env_array, \ exec.status = exec_fork_cmd(list_cmd->content, exec.env_array, \
env, exec.pipe_fd); env, exec.pipe_fd);
__close(list_cmd->content); __close(list_cmd->content);
if (exec.status != -1) if (exec.status != -1)
exec.i++; exec.i++;
list_cmd = list_cmd->next; list_cmd = list_cmd->next;
} }
exec.status = exec_single_cmd(list_cmd->content, exec.env_array, \ exec.status = exec_fork_cmd(list_cmd->content, exec.env_array, \
env, exec.pipe_fd); env, exec.pipe_fd);
__sig(exec.status); __sig(exec.status);
if (((t_cmd *)(list_cmd->content))->outfile != STDOUT_FILENO) __close(list_cmd->content);
close(((t_cmd *)(list_cmd->content))->outfile);
if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO)
close(((t_cmd *)(list_cmd->content))->infile);
return (exec); return (exec);
} }

22
src/exec/free_exec.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free_exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/15 19:27:55 by mmoussou #+# #+# */
/* Updated: 2024/07/15 20:58:08 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void free_exec(t_env *env, char **env_array)
{
rl_clear_history();
ft_envclear(&env, free);
ft_lstclear(get_list(NULL), &free_cmd);
if (env_array)
ft_free("a", &env_array);
}