/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* simple_cmd_execute.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/27 16:21:56 by khais #+# #+# */ /* Updated: 2025/04/01 18:08:02 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "simple_cmd_execute.h" #include "builtins.h" #include "libft.h" #include "../../subst/subst.h" #include "../../env/env_convert.h" #include #include #include #include "../../subst/path_split.h" static char **argv_from_wordlist(t_wordlist *wordlist) { char **out; int i; out = ft_calloc(wordlist_size(wordlist) + 1, sizeof(char *)); i = 0; while (wordlist != NULL) { out[i++] = ft_strdup(wordlist->word->word); wordlist = wordlist->next; } return (out); } static void command_not_found(t_simple_cmd *cmd) { ft_dprintf(STDERR_FILENO, "minishell: %s: command not found\n", cmd->words->word->word); } static void ft_execve(char *exe, char **argv, char **envp) { execve(exe, argv, envp); ft_dprintf(STDERR_FILENO, "minishell: %s: ", argv[0]); perror(NULL); free(exe); path_split_destroy(argv); path_split_destroy(envp); } static void execute_subprocess(char *exe, t_simple_cmd *cmd, t_minishell *app) { ft_execve(exe, argv_from_wordlist(cmd->words), envp_from_env(app->env)); simple_cmd_destroy(cmd); env_destroy(app->env); exit(127); } 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 ; }