MiniDevil As beautiful as a shell
executor_redir.c File Reference

Redirection execution (opens files & applies redirections) More...

#include <fcntl.h>
#include <stdio.h>
#include "executor.h"
#include "libft.h"
+ Include dependency graph for executor_redir.c:

Detailed Description

Redirection execution (opens files & applies redirections)

Definition in file executor_redir.c.

Functions

int open_redir_file (char *file, t_node_type type)
 Open a file for redirection (based on the redirection type) More...
 
int setup_redirection (int fd, t_node_type type)
 Redirect STDIN or STDOUT to fd, saving the original. More...
 
void restore_fd (int saved_fd, t_node_type type)
 Restore a previously saved file descriptor. More...
 
int handle_redir (t_ast *node, t_shell *shell)
 Execute a redirection node. More...
 

Function Documentation

◆ open_redir_file()

int open_redir_file ( char *  file,
t_node_type  type 
)

Open a file for redirection (based on the redirection type)

Opens the file with appropriate flags:

  • < O_RDONLY for input
  • > O_WRONLY | O_CREAT | O_TRUNC for output
  • >> O_WRONLY | O_CREAT | O_APPEND for append
Parameters
fileFilename to open
typeRedirection node type
Returns
File descriptor on success or -1 on failure

Definition at line 30 of file executor_redir.c.

References NODE_REDIR_APPEND, NODE_REDIR_IN, and NODE_REDIR_OUT.

Referenced by handle_redir().

+ Here is the caller graph for this function:

◆ setup_redirection()

int setup_redirection ( int  fd,
t_node_type  type 
)

Redirect STDIN or STDOUT to fd, saving the original.

  • Saves the original fd via dup and replaces it with dup2
  • Closes the source fd after duplication
Parameters
fdFile descriptor to redirect to
typeRedirection type (to determine STDIN or STDOUT target)
Returns
Saved original fd or -1 on error

Definition at line 59 of file executor_redir.c.

References NODE_REDIR_HEREDOC, and NODE_REDIR_IN.

Referenced by handle_redir().

+ Here is the caller graph for this function:

◆ restore_fd()

void restore_fd ( int  saved_fd,
t_node_type  type 
)

Restore a previously saved file descriptor.

Parameters
saved_fdSaved fd from setup_redirection()
typeRedirection type

Definition at line 90 of file executor_redir.c.

References NODE_REDIR_HEREDOC, and NODE_REDIR_IN.

Referenced by handle_redir().

+ Here is the caller graph for this function:

◆ handle_redir()

int handle_redir ( t_ast node,
t_shell shell 
)

Execute a redirection node.

  • Opens the redirection target (file or heredoc)
  • Saves the original fd
  • Executes the child command with the redirected fd
  • Restores the original fd
Parameters
nodeAST redirection node
shellShell context
Returns
Exit status of the child command or 1 on error
Note
For precollected heredocs, uses the cached heredoc_fd

Definition at line 117 of file executor_redir.c.

References t_redir_node::cmd, t_ast::data, executor(), t_redir_node::file, t_redir_node::heredoc_fd, NODE_REDIR_HEREDOC, open_redir_file(), t_ast_data::redir, restore_fd(), setup_redirection(), and t_ast::type.

Referenced by executor().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Go to the source code of this file.