simple_cmd executing: show error in case execve fails

We don't have to check the return status, since execve only returns on failure.

Fixes #52
This commit is contained in:
Khaïs COLIN 2025-04-01 16:28:06 +02:00
parent 0a80b9fbe3
commit 1df6a6ad9a
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */ /* Created: 2025/03/27 16:21:56 by khais #+# #+# */
/* Updated: 2025/04/01 16:37:31 by khais ### ########.fr */ /* Updated: 2025/04/01 18:08:02 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,8 @@
#include "../../env/env_convert.h" #include "../../env/env_convert.h"
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <stdio.h>
#include "../../subst/path_split.h"
static char **argv_from_wordlist(t_wordlist *wordlist) static char **argv_from_wordlist(t_wordlist *wordlist)
{ {
@ -39,6 +41,24 @@ static void command_not_found(t_simple_cmd *cmd)
cmd->words->word->word); 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) void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
{ {
char *exe; char *exe;
@ -56,7 +76,7 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
} }
pid = fork(); pid = fork();
if (pid == 0) if (pid == 0)
execve(exe, argv_from_wordlist(cmd->words), envp_from_env(app->env)); execute_subprocess(exe, cmd, app);
free(exe); free(exe);
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
return ; return ;