static int execute_stream_redirection(const struct conf_t* conf_info) { if(redirect_stream(conf_info->file_in,STDIN_FILENO,READ_FLAG) == -1 ||redirect_stream(conf_info->file_out,STDOUT_FILENO,WRITE_FLAG) == -1 ||redirect_stream(conf_info->file_err,STDERR_FILENO,WRITE_FLAG) == -1) { return RET_ERR; } return RET_OK; }
/** * Pipe: TODO */ static int do_pipe(CommandNode *node) { int pipefd[2]; if (pipe(pipefd) == 0) { int readfd = pipefd[0]; int writefd = pipefd[1]; pid_t source_pid; pid_t target_pid; if (((source_pid = fork())) == 0) { /* source child */ int retval; close(readfd); redirect_stream(writefd, STDOUT_FILENO); retval = execute(node->op1); close(writefd); exit(retval); } else { /* parent */ close(writefd); if (((target_pid = fork())) == 0) { /* target child */ int retval; redirect_stream(readfd, STDIN_FILENO); retval = execute(node->op2); close(readfd); exit(retval); } else { /* parent */ close(readfd); check_wait(source_pid); return check_wait(target_pid); } } } else { perror("do_pipe"); return 1; } }
/** * Redirect stdin/stdout to corresponding files */ static void redirect_to_files(const char *input, const char *output, int do_output_append) { if (input != NULL) { int fd = open(input, O_RDONLY); if (fd < 0) perror("Cannot open input file"); else redirect_stream(fd, STDIN_FILENO); } if (output != NULL) { int fd = open(output, O_WRONLY | O_CREAT | (do_output_append? O_APPEND : O_TRUNC), 0666); if (fd < 0) perror("Cannot open output file"); else redirect_stream(fd, STDOUT_FILENO); } }