5#ifndef MYSHELL_MSH_REDIRECT_H
6#define MYSHELL_MSH_REDIRECT_H
8#include "internal/msh_error.h"
20using redirects_t = std::vector<redirect>;
47 int open_redirect(std::vector<int> *fd_to_close,
int flags,
int mode)
const {
51 }
else if (!path.empty()) {
52 res = open(path.c_str(), flags, mode);
54 msh_error(
"cannot open " + path +
": " + strerror(errno));
57 if (fd_to_close !=
nullptr) {
58 fd_to_close->push_back(res);
75 bool both_err_out =
false;
85 switch (redirect_token.type) {
87 case TokenType::IN_AMP:
91 case TokenType::AMP_OUT:
94 case TokenType::OUT_AMP:
97 in.fd = STDOUT_FILENO;
99 case TokenType::AMP_APPEND:
102 case TokenType::OUT_APPEND:
104 in.fd = STDOUT_FILENO;
122 [[nodiscard]]
int do_redirect(std::vector<int> *fd_to_close)
const {
130 flags = O_WRONLY | O_CREAT | O_TRUNC;
131 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
134 flags = O_WRONLY | O_CREAT | O_APPEND;
135 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
148 if (in_fd != -1 && out_fd != -1) {
149 if (dup2(out_fd, in_fd) == -1) {
150 msh_error(
"cannot redirect: " + std::string(strerror(errno)));
156 if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1) {
157 msh_error(
"cannot redirect: " + std::string(strerror(errno)));
void msh_error(const std::string &msg)
Print an error message to stderr with a myshell: prefix.
Definition msh_error.cpp:37
Token structure and related types.
Structure representing a token.
Definition msh_token.h:74
Structure representing a single redirection.
Definition msh_redirect.h:72
int do_redirect(std::vector< int > *fd_to_close) const
Opens the redirectees with respect to the redirection type and duplicates the appropriate file descri...
Definition msh_redirect.h:122
Structure representing a single redirectee.
Definition msh_redirect.h:29
int open_redirect(std::vector< int > *fd_to_close, int flags, int mode) const
Opens the redirection target.
Definition msh_redirect.h:47