| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2023 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the unset builtin | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file unset.c | ||
| 9 | * @brief The file containing the unset builtin | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Remove a variable from the variable list | ||
| 16 | * @param mysh The shell structure | ||
| 17 | * @param index The index of the variable to remove | ||
| 18 | * @return <b>void</b> | ||
| 19 | */ | ||
| 20 | 8 | static void unset_variable(mysh_t *mysh, int index) | |
| 21 | { | ||
| 22 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 6 times.
|
28 | for (node_t *tmp = mysh->variable_list; tmp != NULL; tmp = tmp->next) { |
| 23 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
|
22 | if (my_strcmp(((variable_t *)tmp->data)->name, |
| 24 | 22 | mysh->args[index]) == 0) { | |
| 25 | 2 | my_free_ptr(my_previous_to_next(&mysh->variable_list, tmp)); | |
| 26 | 2 | return; | |
| 27 | } | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | /** | ||
| 32 | * @brief The unset builtin | ||
| 33 | * @param mysh The shell structure | ||
| 34 | * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise | ||
| 35 | */ | ||
| 36 | 4 | int exec_unset(mysh_t *mysh) | |
| 37 | { | ||
| 38 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (mysh->args[1] == NULL) { |
| 39 | ✗ | my_putstr_error("unset: Too few arguments.\n"); | |
| 40 | ✗ | return 1; | |
| 41 | } | ||
| 42 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
|
12 | for (int index = 0; mysh->args[index] != NULL; index++) |
| 43 | 8 | unset_variable(mysh, index); | |
| 44 | 4 | return 0; | |
| 45 | } | ||
| 46 |