minishell/src/executing/simple_cmd/simple_cmd_execute.c
2025-04-15 14:41:44 +02:00

49 lines
1.6 KiB
C

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