/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/26 09:08:46 by ljiriste #+# #+# */
-/* Updated: 2024/08/30 09:16:51 by ljiriste ### ########.fr */
+/* Updated: 2024/08/30 17:30:11 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
int last_was_builtin;
int last_builtin_ret_val;
t_vars *vars;
+ t_vec fds_to_close;
t_vec child_pids;
} t_execution_env;
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/21 21:24:52 by ljiriste #+# #+# */
-/* Updated: 2024/08/28 09:36:37 by ljiriste ### ########.fr */
+/* Updated: 2024/08/30 17:31:12 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (1);
res = init_vars(env->vars, envp);
res = (ft_vec_init(&env->child_pids, sizeof(pid_t)) != success) || res;
+ res = (ft_vec_init(&env->fds_to_close, sizeof(int)) != success) || res;
if (res)
clean_env(env);
return (res);
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/21 08:57:54 by ljiriste #+# #+# */
-/* Updated: 2024/08/30 14:48:10 by ljiriste ### ########.fr */
+/* Updated: 2024/08/30 17:31:53 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (1);
if (dup2(env->stdout_fd, STDOUT_FILENO) == -1)
return (1);
+ if (env->stdin_fd != STDIN_FILENO)
+ close(env->stdin_fd);
+ if (env->stdout_fd != STDOUT_FILENO)
+ close(env->stdout_fd);
return (0);
}
return (0);
}
+void close_fds(t_vec *fds_to_close)
+{
+ int fd;
+ size_t i;
+
+ i = 0;
+ while (i < fds_to_close->size)
+ {
+ fd = *(int *)ft_vec_access(fds_to_close, i);
+ close(fd);
+ ++i;
+ }
+ return ;
+}
+
int ex_fields(char **fields, t_vec *assignments, const t_vec *redirections, t_execution_env *env)
{
pid_t pid;
pid = fork();
if (pid == 0)
{
+ close_fds(&env->fds_to_close);
dup_pipes(env);
dup_redirections(redirections);
add_exported(assignments, env);
{
if (pipe(pipe_fds))
return (1);
+ ft_vec_append(&env->fds_to_close, &pipe_fds[0]);
+ ft_vec_append(&env->fds_to_close, &env->stdout_fd);
ft_swap_int(&pipe_fds[1], &env->stdout_fd);
ex_pipeline(ft_vec_access(&pipeline->children, 0), env, depth + 1);
ft_swap_int(&pipe_fds[1], &env->stdout_fd);
close(pipe_fds[1]);
+ ft_vec_forget(&env->fds_to_close, env->fds_to_close.size - 1);
+ ft_vec_forget(&env->fds_to_close, env->fds_to_close.size - 1);
ft_swap_int(&pipe_fds[0], &env->stdin_fd);
ex_command(ft_vec_access(&pipeline->children, 2), env);
ft_swap_int(&pipe_fds[0], &env->stdin_fd);