GCC Code Coverage Report


Directory: ./
File: src/separators/right_redirections.c
Date: 2024-05-07 14:54:03
Exec Total Coverage
Lines: 24 25 96.0%
Functions: 2 2 100.0%
Branches: 11 12 91.7%

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 101 static int get_file_descriptor(mysh_t *mysh, input_command_t *input)
21 {
22 int fd;
23
24
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 97 times.
101 if (input->right[0] == '\0') {
25 4 my_fprintf(2, "Missing name for redirect.\n");
26 4 mysh->exit_status = 1;
27 4 return -1;
28 }
29 97 fd = open(input->right, O_WRONLY | O_CREAT |
30
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 59 times.
97 ((input->right_type == 2) ? O_APPEND : O_TRUNC), 0644);
31
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 90 times.
97 if (fd == -1) {
32 7 mysh->exit_status = 1;
33 7 my_fprintf(2, "%s: %s.\n", input->right, strerror(errno));
34 }
35 97 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 101 void exec_right_redirection(mysh_t *mysh, input_command_t *input)
45 {
46 101 int fd = get_file_descriptor(mysh, input);
47
48
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 90 times.
101 if (fd == -1)
49 11 return;
50 90 mysh->saved_stdout = dup(1);
51 90 dup2(fd, 1);
52 90 close(fd);
53
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 78 times.
90 if (input->left_type == 1)
54 12 exec_left_simple_redirection(mysh, input);
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 else if (input->left_type == 2)
56 exec_left_double_redirection(mysh, input);
57 else
58 78 analyse_parentheses(mysh, input);
59 90 dup2(mysh->saved_stdout, 1);
60 90 close(mysh->saved_stdout);
61 }
62