simple_cmd refactor: put subprocess functions in own file

This clears some space for new functions later
This commit is contained in:
Khaïs COLIN 2025-04-02 18:17:37 +02:00
parent 74f013e5c6
commit 3661feefaa
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
4 changed files with 73 additions and 36 deletions

View file

@ -36,6 +36,7 @@ srcs = \
src/executing/simple_cmd/builtin_pwd.c \
src/executing/simple_cmd/builtins.c \
src/executing/simple_cmd/simple_cmd_execute.c \
src/executing/simple_cmd/subprocess.c \
src/ft_errno.c \
src/get_command.c \
src/parser/cmdgroup/cmdgroup.c \

View file

@ -6,34 +6,18 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
/* Updated: 2025/04/01 18:08:02 by khais ### ########.fr */
/* 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 "../../env/env_convert.h"
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#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)
{
@ -41,24 +25,6 @@ static void command_not_found(t_simple_cmd *cmd)
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;

View file

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* subprocess.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/02 18:19:23 by khais #+# #+# */
/* Updated: 2025/04/02 18:20:54 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "subprocess.h"
#include "../../env/env_convert.h"
#include "../../parser/simple_cmd/simple_cmd.h"
#include "../../subst/path_split.h"
#include <stdio.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 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);
}
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);
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* subprocess.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/02 18:17:48 by khais #+# #+# */
/* Updated: 2025/04/02 18:21:30 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SUBPROCESS_H
# define SUBPROCESS_H
# include "../../minishell.h"
void execute_subprocess(char *exe, t_simple_cmd *cmd, t_minishell *app);
#endif // SUBPROCESS_H