GCC Code Coverage Report


Directory: ./
File: src/builtins/set.c
Date: 2024-05-07 14:54:03
Exec Total Coverage
Lines: 76 84 90.5%
Functions: 7 8 87.5%
Branches: 39 50 78.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2023
3 ** 42sh
4 ** File description:
5 ** The file containing the set builtin
6 */
7 /**
8 * @file set.c
9 * @brief The file containing the set builtin
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief Print the list of variable if only one argument
16 * @param mysh The shell structure
17 * @return <b>int</b> <u>1</u> if we print the variables, <u>0</u> otherwise
18 */
19 46 int display_variable(mysh_t *mysh)
20 {
21
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 28 times.
46 if (mysh->args[1] != NULL)
22 18 return 0;
23
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 28 times.
50 for (node_t *tmp = mysh->variable_list; tmp != NULL; tmp = tmp->next) {
24
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 if (my_strcmp(((variable_t *)tmp->data)->name, "?") == 0)
25 continue;
26 22 my_printf("%s\t", ((variable_t *)tmp->data)->name);
27
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (((variable_t *)tmp->data)->value != NULL)
28 22 my_putstr(((variable_t *)tmp->data)->value);
29 22 my_putchar('\n');
30 }
31 28 return 1;
32 }
33
34 /**
35 * @brief Separate the variable name and value when there are stuck together
36 * with an '='
37 * @param mysh The shell structure
38 * @param index The index of the args to separate
39 * @return <b>void</b>
40 */
41 14 static void change_args(mysh_t *mysh, int index)
42 {
43 14 char **new_args = MALLOC(sizeof(char *) *
44 (my_array_len((void *)mysh->args) + 3));
45
46
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
28 for (int i = 0; i < index; i++)
47 14 new_args[i] = mysh->args[i];
48 14 new_args[index] = my_malloc_strndup(mysh->args[index],
49 14 my_get_char_index(mysh->args[index], '=', 1));
50 14 new_args[index + 1] = my_malloc_strdup("=");
51 42 new_args[index + 2] = my_malloc_strdup(mysh->args[index] +
52 14 my_get_char_index(mysh->args[index], '=', 1) + 1);
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 for (int i = index + 1; mysh->args[i] != NULL; i++)
54 new_args[i + 2] = mysh->args[i];
55 14 new_args[my_array_len((void *)mysh->args) + 2] = NULL;
56 14 mysh->args = new_args;
57 14 }
58
59 /**
60 * @brief Check if the variable is in the right order
61 * @param list The list of variable
62 * @param cmp The function to compare
63 * @return <b>void</b>
64 */
65 60 static void check_swap(node_t **list)
66 {
67 60 void *tmp = NULL;
68
69
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if ((*list)->prev != NULL
70
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 56 times.
60 && my_strcmp(((variable_t *)(*list)->prev->data)->name,
71 60 ((variable_t *)(*list)->data)->name) > 0) {
72 4 tmp = (*list)->prev->data;
73 4 (*list)->prev->data = (*list)->data;
74 4 (*list)->data = tmp;
75 }
76 60 }
77
78 /**
79 * @brief Sort the variable list
80 * @param begin The list of variable
81 * @param cmp The function to compare
82 * @return <b>void</b>
83 */
84 25 static void my_sort_variable(node_t **begin)
85 {
86
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 25 times.
70 for (node_t *list1 = *begin; list1 != NULL; list1 = list1->next) {
87 45 for (node_t *list2 = (*begin)->next;
88
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 45 times.
105 list2 != NULL; list2 = list2->next)
89 60 check_swap(&list2);
90 }
91 25 }
92
93 /**
94 * @brief Add a variable to the list
95 * @param mysh The shell structure
96 * @param name The name of the variable
97 * @param value The value of the variable
98 * @return <b>void</b>
99 */
100 29 void add_variable(mysh_t *mysh, char *name, char *value)
101 {
102 29 variable_t *variable = NULL;
103
104
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 29 times.
61 for (node_t *tmp = mysh->variable_list; tmp != NULL; tmp = tmp->next) {
105
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 28 times.
32 if (my_strcmp(((variable_t *)tmp->data)->name, name) == 0)
106 4 variable = (variable_t *)tmp->data;
107 }
108
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 4 times.
29 if (variable == NULL) {
109 25 variable = MALLOC(sizeof(variable_t));
110 25 variable->name = name;
111 25 variable->value = value;
112 25 my_push_back(&mysh->variable_list, variable, UNKNOWN);
113 25 my_sort_variable(&mysh->variable_list);
114 } else {
115 4 variable->value = value;
116 }
117 29 }
118
119 /**
120 * @brief Add all the variables to the list
121 * @param mysh The shell structure
122 * @return <b>void</b>
123 */
124 18 static void add_all_variables(mysh_t *mysh)
125 {
126 18 char *value = NULL;
127
128
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
36 for (int index = 1; mysh->args[index] != NULL; index++) {
129
1/2
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
18 if (my_strcmp(mysh->args[index + 1], "=") == 0)
130 18 value = mysh->args[index + 2];
131 18 add_variable(mysh, mysh->args[index], value);
132
1/2
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
18 if (my_strcmp(mysh->args[index + 1], "=") == 0)
133 18 index = index + 2;
134 18 value = NULL;
135 }
136 18 }
137
138 /**
139 * @brief The set builtin
140 * @param mysh The shell structure
141 * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise
142 */
143 46 int exec_set(mysh_t *mysh)
144 {
145
2/2
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 18 times.
46 if (display_variable(mysh))
146 28 return 0;
147
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 18 times.
44 for (int index = 1; mysh->args[index] != NULL; index++) {
148
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 8 times.
26 if (my_str_contains(mysh->args[index], "=") == 1
149
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 4 times.
18 && my_strlen(mysh->args[index]) != 1) {
150 14 change_args(mysh, index);
151 14 index = index + 2;
152 }
153 }
154
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 18 times.
54 for (int index = 1; mysh->args[index] != NULL; index++) {
155
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 18 times.
36 if (my_strcmp(mysh->args[index], "=") == 0) {
156 18 index++;
157 18 continue;
158 }
159
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if (is_valid_variable(mysh->args[index], "set") == 0)
160 return 1;
161 }
162 18 add_all_variables(mysh);
163 18 return 0;
164 }
165
166 /**
167 * @brief Get the value of a variable
168 * @param mysh The shell structure
169 * @param name The name of the variable
170 * @return <b>char *</b> The value of the variable
171 */
172 char *get_variable_value(mysh_t *mysh, char *name)
173 {
174 for (node_t *tmp = mysh->variable_list; tmp != NULL; tmp = tmp->next) {
175 if (my_strcmp(((variable_t *)tmp->data)->name, name) == 0)
176 return ((variable_t *)tmp->data)->value;
177 }
178 return NULL;
179 }
180