From 187534464b68243687d95a52bbfa6f3507987107 Mon Sep 17 00:00:00 2001 From: Kyungjun Lee Date: Tue, 9 Apr 2024 14:03:51 +0900 Subject: [PATCH] heredoc: restore stdout --- codes/builtins/builtin_pwd.c | 13 ++++++++++--- codes/includes/types.h | 3 ++- codes/loader/ldpre_ast_redir2.c | 28 +++++++++++++++++++++------- codes/loader/loader_wrapper.c | 3 ++- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/codes/builtins/builtin_pwd.c b/codes/builtins/builtin_pwd.c index 8ea1a07..4195214 100644 --- a/codes/builtins/builtin_pwd.c +++ b/codes/builtins/builtin_pwd.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* builtin_pwd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: nicknamemohaji +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/27 06:04:45 by nicknamemoh #+# #+# */ -/* Updated: 2024/03/27 06:07:28 by nicknamemoh ### ########.fr */ +/* Updated: 2024/04/09 13:47:00 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,13 @@ int builtin_pwd(char *args[], t_ld_map_env *env) (void) args; pwd = ldpre_env_fetch("PWD", env); - printf("%s\n", pwd); + if (pwd == NULL) + { + pwd = getcwd(NULL, 0); + printf("%s\n", pwd); + ldpre_env_add(ft_strdup("PWD"), pwd, env); + } + else + printf("%s\n", pwd); return (EXIT_SUCCESS); } diff --git a/codes/includes/types.h b/codes/includes/types.h index f7885e6..7f70a52 100644 --- a/codes/includes/types.h +++ b/codes/includes/types.h @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/12 22:18:03 by kyungjle #+# #+# */ -/* Updated: 2024/04/08 20:16:16 by kyungjle ### ########.fr */ +/* Updated: 2024/04/09 13:54:30 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,7 @@ typedef struct s_ld_heredoc { char *heredoc_name; int stdin_fd; + int stdout_fd; } t_ld_heredoc; // used by loader.preprocessor.param.expansion diff --git a/codes/loader/ldpre_ast_redir2.c b/codes/loader/ldpre_ast_redir2.c index 609a179..dd21358 100644 --- a/codes/loader/ldpre_ast_redir2.c +++ b/codes/loader/ldpre_ast_redir2.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ldpre_ast_redir.c :+: :+: :+: */ +/* ldpre_ast_redir2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/04 18:41:08 by kyungjle #+# #+# */ -/* Updated: 2024/04/08 18:06:29 by kyungjle ### ########.fr */ +/* Updated: 2024/04/09 14:02:04 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,11 +40,28 @@ t_bool ldpre_ast_redir_outfile(char *filename, enum e_node_type mode) t_bool ldpre_ast_redir_infile(char *filename, t_ld_heredoc heredoc, enum e_node_type mode, t_ld_map_env *env) { + int stdout_fd; + int ret; + close(STDIN_FD); if (mode == EXP_PRE_RHEREDOC) - return (infile_heredoc(filename, env, heredoc)); + { + if (dup(heredoc.stdin_fd) < 0) + do_exit("ldpre_ast_redir.dup"); + stdout_fd = dup(STDOUT_FD); + if (stdout_fd < 0) + do_exit("ldpre_ast_redir.dup"); + close(STDOUT_FD); + if (dup(heredoc.stdout_fd) < 0) + do_exit("ldpre_ast_redir.dup"); + ret = infile_heredoc(filename, env, heredoc); + close(STDOUT_FD); + if (dup(stdout_fd) < 0) + do_exit("ldpre_ast_redir.dup"); + } else - return (infile_read(filename)); + ret = infile_read(filename); + return (ret); } static t_bool infile_read(char *filename) @@ -75,9 +92,6 @@ static t_bool infile_heredoc(char *filename, delim_len = ft_strlen(filename); filename = ldpre_param_quote_f(ft_strdup(filename), env, NULL); expansion = (ft_strlen(filename) == delim_len); - fd = dup(heredoc.stdin_fd); - if (fd < 0) - do_exit("ldpre_ast_redir.dup"); fd = open(heredoc.heredoc_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd < 0) do_exit("ldpre_ast_redir.open"); diff --git a/codes/loader/loader_wrapper.c b/codes/loader/loader_wrapper.c index 1d191a6..2b7a90d 100644 --- a/codes/loader/loader_wrapper.c +++ b/codes/loader/loader_wrapper.c @@ -6,7 +6,7 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/01 13:16:24 by nicknamemoh #+# #+# */ -/* Updated: 2024/04/09 12:05:45 by kyungjle ### ########.fr */ +/* Updated: 2024/04/09 13:59:56 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,6 +32,7 @@ void loader_wrapper(char *input, t_ld_map_env *env) ldexec_sigign_setup(oldacts); heredoc.heredoc_name = ldexec_heredoc_assign_f(); heredoc.stdin_fd = stdin_fd; + heredoc.stdout_fd = stdout_fd; if (heredoc.heredoc_name == NULL) { printf("cannot open heredoc :tried %d times :(\n", HEREDOC_MAX);