2025-03-27 16:10:41 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* simple_cmd_execute.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
|
2025-04-02 18:17:37 +02:00
|
|
|
/* Updated: 2025/04/02 18:19:57 by khais ### ########.fr */
|
2025-03-27 16:10:41 +01:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#include "simple_cmd_execute.h"
|
2025-03-31 14:53:05 +02:00
|
|
|
#include "builtins.h"
|
2025-04-02 18:17:37 +02:00
|
|
|
#include "subprocess.h"
|
2025-03-27 16:10:41 +01:00
|
|
|
#include "libft.h"
|
|
|
|
|
#include "../../subst/subst.h"
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/wait.h>
|
2025-04-01 16:28:06 +02:00
|
|
|
#include <stdio.h>
|
2025-03-27 16:10:41 +01:00
|
|
|
|
2025-03-28 16:57:00 +01:00
|
|
|
static void command_not_found(t_simple_cmd *cmd)
|
|
|
|
|
{
|
|
|
|
|
ft_dprintf(STDERR_FILENO, "minishell: %s: command not found\n",
|
|
|
|
|
cmd->words->word->word);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-27 16:10:41 +01:00
|
|
|
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
|
|
|
|
{
|
|
|
|
|
char *exe;
|
|
|
|
|
int pid;
|
|
|
|
|
|
|
|
|
|
if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL)
|
|
|
|
|
return ;
|
2025-03-31 14:53:05 +02:00
|
|
|
if (execute_builtin(cmd, app) != BUILTIN_INVALID)
|
|
|
|
|
return ;
|
2025-03-27 16:10:41 +01:00
|
|
|
exe = get_cmdpath(cmd->words->word->word, app);
|
|
|
|
|
if (exe == NULL)
|
2025-03-28 16:57:00 +01:00
|
|
|
{
|
|
|
|
|
command_not_found(cmd);
|
2025-03-27 16:10:41 +01:00
|
|
|
return ;
|
2025-03-28 16:57:00 +01:00
|
|
|
}
|
2025-03-27 16:10:41 +01:00
|
|
|
pid = fork();
|
|
|
|
|
if (pid == 0)
|
2025-04-01 16:28:06 +02:00
|
|
|
execute_subprocess(exe, cmd, app);
|
2025-03-27 16:10:41 +01:00
|
|
|
free(exe);
|
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
|
return ;
|
|
|
|
|
}
|