GCC Code Coverage Report


Directory: ./
File: src/builtins/if.c
Date: 2024-05-07 14:54:03
Exec Total Coverage
Lines: 52 85 61.2%
Functions: 4 5 80.0%
Branches: 29 62 46.8%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the if builtin
6 */
7 /**
8 * @file if.c
9 * @brief The file containing the if builtin
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief Execute the commands specified when the condition is true
16 * @param mysh The shell structure
17 * @param args The command arguments
18 * @param start The index of the start of the command arguments
19 */
20 2 static int exec_true_condition(mysh_t *mysh, char **args, int start)
21 {
22 2 pid_t pid = 0;
23 2 int size = 0;
24 2 char *command = NULL;
25
26
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int index = start; args[index] != NULL; index++)
27 2 size += my_strlen(args[index]) + 1;
28 2 command = calloc(size, sizeof(char));
29
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int index = start; args[index] != NULL; index++) {
30 2 my_strcat(command, args[index]);
31
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (args[index + 1] != NULL)
32 my_strcat(command, " ");
33 }
34 2 pid = fork();
35
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (pid == 0) {
36 2 analyse_backticks(mysh, command);
37 2 my_exit(mysh, mysh->exit_status, NULL);
38 }
39 2 waitpid(pid, &mysh->exit_status, 0);
40 2 FREE(command);
41 2 return mysh->exit_status;
42 }
43
44 /**
45 * @brief Read the input of the user when the condition is false
46 * @param mysh The shell structure
47 * @return <b>char **</b> The command arguments
48 */
49 1 static char **read_input(mysh_t *mysh)
50 {
51 1 int size = 0;
52 1 char *line = NULL;
53 1 char **content = NULL;
54
55
5/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
6 while (size != EOF && (content == NULL ||
56
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 (my_strcmp(content[0], "endif") && my_strcmp(content[0], "else")))) {
57
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 IS_ATTY_PRINT("if? ");
58 3 free_str_and_tab(line, NULL);
59 3 size = my_getline(&line, stdin);
60 3 set_command_in_history(mysh, line);
61 3 free_str_and_tab(NULL, content);
62 3 content = str_to_array_inhibitors(line);
63 }
64
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1 if (size == EOF || (content != NULL && my_strcmp(content[0], "endif")
65 && my_strcmp(content[0], "else"))) {
66 1 my_putstr_error("then: then/endif not found.\n");
67 1 return free_str_and_tab(line, content);
68 }
69 free_str_and_tab(line, NULL);
70 return content;
71 }
72
73 /**
74 * @brief Execute the false condition who wait for an else or an endif
75 * @param mysh The shell structure
76 * @return <b>int</b> The exit status of the command executed
77 */
78 1 static int exec_false_condition(mysh_t *mysh)
79 {
80 1 char **line_content = read_input(mysh);
81 1 char **new_line_content = NULL;
82
83
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (line_content == NULL || line_content[0] == NULL) {
84 1 FREE_WORD_ARRAY(line_content);
85 1 return 1;
86 }
87 if (my_strcmp(line_content[0], "endif") == 0) {
88 FREE_WORD_ARRAY(line_content);
89 return 0;
90 }
91 if (my_strncmp(line_content[1], "if", 2) != 0) {
92 new_line_content = my_malloc_strdup_word_array(line_content);
93 FREE_WORD_ARRAY(line_content);
94 return exec_true_condition(mysh, new_line_content, 1);
95 }
96 mysh->exit_status = exec_else_if(mysh, line_content);
97 FREE_WORD_ARRAY(line_content);
98 return mysh->exit_status;
99 }
100
101 /**
102 * @brief The else if builtin
103 * @param mysh The shell structure
104 * @param line_content The command arguments
105 * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise
106 */
107 int exec_else_if(mysh_t *mysh, char **line_content)
108 {
109 int condition = 0;
110
111 if (line_content[2] == NULL) {
112 my_putstr_error("if: Too few arguments.\n");
113 return 1;
114 }
115 condition = get_condition(line_content[2]);
116 if (condition == -1)
117 return 1;
118 if (line_content[3] == NULL) {
119 my_putstr_error("if: Empty if.\n");
120 return 1;
121 }
122 if (condition && my_strcmp(line_content[3], "then") == 0)
123 return 0;
124 if (condition)
125 return exec_true_condition(mysh, line_content, 3);
126 return exec_false_condition(mysh);
127 }
128
129 /**
130 * @brief The if builtin
131 * @param mysh The shell structure
132 * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise
133 */
134 16 int exec_if(mysh_t *mysh)
135 {
136 16 int condition = 0;
137
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (mysh->args[1] == NULL) {
139 my_putstr_error("if: Too few arguments.\n");
140 return 1;
141 }
142 16 condition = get_condition(mysh->args[1]);
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (condition == -1)
144 return 1;
145
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 11 times.
16 if (mysh->args[2] == NULL) {
146 5 my_putstr_error("if: Empty if.\n");
147 5 return 1;
148 }
149
6/6
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2 times.
11 if (condition && my_strcmp(mysh->args[2], "then") == 0 ||
150
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 condition == 0 && my_strcmp(mysh->args[2], "then") != 0)
151 8 return 0;
152
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (condition)
153 2 return exec_true_condition(mysh, mysh->args, 2);
154 1 return exec_false_condition(mysh);
155 }
156