diff --git a/Makefile b/Makefile index 36d2a9e..5ac383a 100644 --- a/Makefile +++ b/Makefile @@ -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 \ @@ -100,6 +101,7 @@ all: $(NAME) $(NAME): $(minishell_objs) $(LIBFT) $(CC) $(CFLAGS) -o $@ $(minishell_objs) $(LINCLUDE) $(LDLIBS) +$(LIBFT): CFLAGS+=-DBUFFER_SIZE=1 $(LIBFT): +$(MAKE) -C $(LIBFTDIR) diff --git a/src/executing/simple_cmd/simple_cmd_execute.c b/src/executing/simple_cmd/simple_cmd_execute.c index e483186..65612bf 100644 --- a/src/executing/simple_cmd/simple_cmd_execute.c +++ b/src/executing/simple_cmd/simple_cmd_execute.c @@ -6,32 +6,18 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/27 16:21:56 by khais #+# #+# */ -/* Updated: 2025/04/01 16:37:31 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 #include - -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); -} +#include static void command_not_found(t_simple_cmd *cmd) { @@ -56,7 +42,7 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app) } pid = fork(); if (pid == 0) - execve(exe, argv_from_wordlist(cmd->words), envp_from_env(app->env)); + execute_subprocess(exe, cmd, app); free(exe); waitpid(pid, NULL, 0); return ; diff --git a/src/executing/simple_cmd/subprocess.c b/src/executing/simple_cmd/subprocess.c new file mode 100644 index 0000000..9d7b882 --- /dev/null +++ b/src/executing/simple_cmd/subprocess.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* subprocess.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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); +} diff --git a/src/executing/simple_cmd/subprocess.h b/src/executing/simple_cmd/subprocess.h new file mode 100644 index 0000000..3e877d4 --- /dev/null +++ b/src/executing/simple_cmd/subprocess.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* subprocess.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 diff --git a/src/subst/simple_filename_exp.c b/src/subst/simple_filename_exp.c index 3e1451b..0f8bd66 100644 --- a/src/subst/simple_filename_exp.c +++ b/src/subst/simple_filename_exp.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/02 13:40:10 by jguelen #+# #+# */ -/* Updated: 2025/03/27 18:39:23 by khais ### ########.fr */ +/* Updated: 2025/04/01 18:38:35 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,8 +77,6 @@ static char *select_path(char *filepath, char **oldpath, char **path, else return (free(*oldpath), path_split_destroy(path), filepath); } - if (*oldpath != filepath) - free(filepath); return (NULL); } diff --git a/test.sh b/test.sh index e6fcefd..2b647bc 100755 --- a/test.sh +++ b/test.sh @@ -335,6 +335,13 @@ status=1 hello=hi EOF +when_run <