🔨」 fix: fixed split cmd when there is no path var, added split cmd execution to the main

This commit is contained in:
y-syo
2024-06-04 22:28:13 +02:00
parent b7601617cc
commit 5642c267f0
4 changed files with 49 additions and 32 deletions

View File

@ -6,14 +6,17 @@
/* 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/06/03 15:26:03 by mmoussou ### ########.fr */ /* Updated: 2024/06/04 21:43:34 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef EXECUTION_H #ifndef EXECUTION_H
# define EXECUTION_H # define EXECUTION_H
# include "env.h"
char **get_path(char *path); char **get_path(char *path);
int exec_split_cmd(t_list *list_cmd, t_env *env);
/** /**
* @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/06/02 21:15:03 by mmoussou ### ########.fr */ /* Updated: 2024/06/04 22:03:56 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,7 +19,9 @@ char *get_cmd_path(char *cmd, t_env *env)
char **path_list; char **path_list;
path = NULL; path = NULL;
path_list = get_path(env_get_value("PATH2", env)); path_list = get_path(env_get_value("PATH", env));
if (!path_list)
return (NULL);
i = 0; i = 0;
while (path_list[i]) while (path_list[i])
{ {
@ -36,6 +38,7 @@ char *get_cmd_path(char *cmd, t_env *env)
int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t) int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t)
{ {
int fork_pid;
int status; int status;
status = dup2(cmd->infile, STDIN_FILENO); status = dup2(cmd->infile, STDIN_FILENO);
@ -51,8 +54,37 @@ int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t)
cmd->cmd = get_cmd_path(cmd->cmd, env_t); cmd->cmd = get_cmd_path(cmd->cmd, env_t);
if (!(cmd->cmd)) if (!(cmd->cmd))
return (-1); return (-1);
fork_pid = fork();
if (fork_pid == -1)
return (-1);
if (!fork_pid)
{
status = execve(cmd->cmd, cmd->argv, env);
exit(-1);
}
else
waitpid(fork_pid, NULL, 0);
if (status == -1)
return (-1);
return (0);
}
int fork_pid = fork(); int exec_last_cmd(t_cmd *cmd, char **env, t_env *env_t)
{
int fork_pid;
int status;
status = dup2(cmd->infile, STDIN_FILENO);
if (status == -1)
return (-1);
status = dup2(STDOUT_FILENO, cmd->outfile);
if (status == -1)
return (-1);
if (cmd->cmd[0] != '/')
cmd->cmd = get_cmd_path(cmd->cmd, env_t);
if (!(cmd->cmd))
return (-1);
fork_pid = fork();
if (fork_pid == -1) if (fork_pid == -1)
return (-1); return (-1);
if (!fork_pid) if (!fork_pid)
@ -71,7 +103,6 @@ int exec_split_cmd(t_list *list_cmd, t_env *env)
{ {
char **env_array; char **env_array;
int status; int status;
t_cmd *cmd;
env_array = env_get(env); env_array = env_get(env);
if (!env_array) if (!env_array)
@ -86,27 +117,8 @@ int exec_split_cmd(t_list *list_cmd, t_env *env)
} }
list_cmd = list_cmd->next; list_cmd = list_cmd->next;
} }
cmd = list_cmd->content; status = exec_last_cmd(list_cmd->content, env_array, env);
status = dup2(cmd->infile, STDIN_FILENO); ft_free("a", &env_array);
if (status == -1)
return (-1);
status = dup2(STDOUT_FILENO, cmd->outfile);
if (status == -1)
return (-1);
if (cmd->cmd[0] != '/')
cmd->cmd = get_cmd_path(cmd->cmd, env);
if (!(cmd->cmd))
return (-1);
int fork_pid = fork();
if (fork_pid == -1)
return (-1);
if (!fork_pid)
{
status = execve(cmd->cmd, cmd->argv, env_array);
exit(-1);
}
else
waitpid(fork_pid, NULL, 0);
if (status == -1) if (status == -1)
return (-1); return (-1);
return (0); return (0);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 01:42:17 by mmoussou #+# #+# */ /* Created: 2024/05/19 01:42:17 by mmoussou #+# #+# */
/* Updated: 2024/06/02 21:05:25 by mmoussou ### ########.fr */ /* Updated: 2024/06/04 21:53:30 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -67,6 +67,8 @@ char **get_path(char *path)
t_list *list_entry; t_list *list_entry;
int i; int i;
if (!path)
return (NULL);
list_entry = ft_lstnew(NULL); list_entry = ft_lstnew(NULL);
path_dir = ft_split(path, ':'); path_dir = ft_split(path, ':');
if (!path_dir) if (!path_dir)

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */ /* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
/* Updated: 2024/05/31 13:29:26 by adjoly ### ########.fr */ /* Updated: 2024/06/04 21:42:55 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -67,7 +67,6 @@ int main(int ac, char **av, char **env)
char **lll; char **lll;
t_list *piped; t_list *piped;
t_env env_l; t_env env_l;
t_cmd *cmd;
t_list *cmd_list; t_list *cmd_list;
(void)ac; (void)ac;
@ -90,12 +89,13 @@ int main(int ac, char **av, char **env)
piped = tokenizer(test); piped = tokenizer(test);
check_redir(((t_token *)(piped->content))->redirection, av); check_redir(((t_token *)(piped->content))->redirection, av);
cmd_list = get_cmd_list(piped); cmd_list = get_cmd_list(piped);
while (cmd_list) exec_split_cmd(cmd_list, &env_l);
/*while (cmd_list)
{ {
cmd = cmd_list->content; cmd = cmd_list->content;
cmd_list = cmd_list->next; cmd_list = cmd_list->next;
} }
print_cmd(cmd); print_cmd(cmd);*/
free(test); free(test);
ft_lstclear(&piped, free_token); ft_lstclear(&piped, free_token);
ft_free("a", &lll); ft_free("a", &lll);