GCC Code Coverage Report


Directory: ./
File: src/parsing/condition.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 45 54 83.3%
Functions: 4 4 100.0%
Branches: 9 14 64.3%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the condition functions
6 */
7 /**
8 * @file condition.c
9 * @brief The file containing the condition functions
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief The child process who execute the command to get the result
16 * @param fd The pipe file descriptor
17 * @return <b>void</b>
18 */
19 14 static void child_process(int *fd)
20 {
21 static char *command[] = {"bc", "-l", NULL};
22
23 14 close(fd[1]);
24 14 dup2(fd[0], 0);
25 14 get_mysh()->args = command;
26 14 exec_command(get_mysh());
27 14 my_exit(get_mysh(), get_mysh()->exit_status, NULL);
28 }
29
30 /**
31 * @brief Read the result of the operation
32 * @param new_fd The pipe file descriptor to read
33 * @param pid The pid of the child process
34 * @param str The string to calculate
35 * @param saved_stdout The saved stdout
36 * @return <b>char *</b> The result of the operation
37 */
38 14 static char *read_result(int *new_fd, pid_t pid, char *str, int saved_stdout)
39 {
40 14 char *result = calloc(1024, sizeof(char));
41 14 int nb_bytes = 0;
42 14 int status = 0;
43
44 14 waitpid(pid, &status, 0);
45
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (status != 0)
46 return NULL;
47 14 nb_bytes = read(new_fd[0], result, 1024);
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (nb_bytes == -1)
49 my_fprintf(2, "%s\n", strerror(errno));
50
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 while (nb_bytes == 1024) {
51 result = realloc(result, sizeof(char) * (1024 + my_strlen(result)));
52 nb_bytes = read(new_fd[0], result + my_strlen(result), 1024);
53 }
54 14 result[my_strlen(result) - 1] = '\0';
55 14 dup2(saved_stdout, 1);
56 14 close(saved_stdout);
57 14 return result;
58 }
59
60 /**
61 * @brief Get the result of an operation
62 * @param str The string to calculate
63 * @return <b>char *</b> The result of the string
64 */
65 14 static char *get_result(char *str)
66 {
67 14 int fd[2] = {0};
68 14 int new_fd[2] = {0};
69 14 pid_t pid = 0;
70 14 int saved_stdout = dup(1);
71
72 14 pipe(new_fd);
73 14 dup2(new_fd[1], 1);
74 14 close(new_fd[1]);
75 14 pipe(fd);
76 14 pid = fork();
77
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
28 if (pid == 0)
78 14 child_process(fd);
79 14 close(fd[0]);
80 14 my_fprintf(fd[1], "%s\n", str);
81 14 close(fd[1]);
82 14 return read_result(new_fd, pid, str, saved_stdout);
83 }
84
85 /**
86 * @brief Get the condition result from a string
87 * @param str The string to calculate
88 * @return <b>int</b> The result of the condition : 0 if false, 1 if true
89 * and -1 if an error occurred
90 */
91 14 int get_condition(char *str)
92 {
93 14 char *result = get_result(str);
94 14 int nb = 0;
95
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (result == NULL) {
97 my_fprintf(2, "if: Expression Syntax.\n");
98 return -1;
99 }
100 14 nb = my_super_number(result, (NB){0, 0, 0, 1});
101 14 FREE(result);
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (my_errno) {
103 my_fprintf(2, "if: Expression Syntax.\n");
104 return -1;
105 }
106
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 5 times.
14 if (nb == 0)
107 9 return 0;
108 5 return 1;
109 }
110