GCC Code Coverage Report


Directory: ./
File: src/separators/right_redirections.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 25 25 100.0%
Functions: 2 2 100.0%
Branches: 12 12 100.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the right redirections functions
6 */
7 /**
8 * @file right_redirections.c
9 * @brief The file containing the right redirections functions
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief Get the file descriptor
16 * @param mysh The shell structure
17 * @param input The input command
18 * @return <b>int</b> The file descriptor
19 */
20 83 static int get_file_descriptor(mysh_t *mysh, input_command_t *input)
21 {
22 int fd;
23
24
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 80 times.
83 if (input->right[0] == '\0') {
25 3 my_fprintf(2, "Missing name for redirect.\n");
26 3 mysh->exit_status = 1;
27 3 return -1;
28 }
29 80 fd = open(input->right, O_WRONLY | O_CREAT |
30
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 49 times.
80 ((input->right_type == 2) ? O_APPEND : O_TRUNC), 0644);
31
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 79 times.
80 if (fd == -1) {
32 1 mysh->exit_status = 1;
33 1 my_fprintf(2, "%s: %s.\n", input->right, strerror(errno));
34 }
35 80 return fd;
36 }
37
38 /**
39 * @brief Execute the right redirection
40 * @param mysh The shell structure
41 * @param input The input command
42 * @return <b>void</b>
43 */
44 83 void exec_right_redirection(mysh_t *mysh, input_command_t *input)
45 {
46 83 int fd = get_file_descriptor(mysh, input);
47
48
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 79 times.
83 if (fd == -1)
49 4 return;
50 79 mysh->saved_stdout = dup(1);
51 79 dup2(fd, 1);
52 79 close(fd);
53
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 59 times.
79 if (input->left_type == 1)
54 20 exec_left_simple_redirection(mysh, input);
55
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 58 times.
59 else if (input->left_type == 2)
56 1 exec_left_double_redirection(mysh, input);
57 else
58 58 analyse_parentheses(mysh, input);
59 79 dup2(mysh->saved_stdout, 1);
60 79 close(mysh->saved_stdout);
61 }
62