Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
heredoc: restore stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
nicknamemohaji committed Apr 9, 2024
1 parent 07872e5 commit 1875344
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
13 changes: 10 additions & 3 deletions codes/builtins/builtin_pwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* builtin_pwd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nicknamemohaji <nicknamemohaji@student. +#+ +:+ +#+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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);
}
3 changes: 2 additions & 1 deletion codes/includes/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions codes/loader/ldpre_ast_redir2.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ldpre_ast_redir.c :+: :+: :+: */
/* ldpre_ast_redir2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion codes/loader/loader_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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);
Expand Down

0 comments on commit 1875344

Please sign in to comment.