| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the exit builtin | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file exit.c | ||
| 9 | * @brief The file containing the exit builtin | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Free an string and his array | ||
| 16 | * @param str The string to free | ||
| 17 | * @param tab The array of the string to free | ||
| 18 | * @return <b>void *</b> Always <u>NULL</u> | ||
| 19 | */ | ||
| 20 | 62 | void *free_str_and_tab(char *str, char **tab) | |
| 21 | { | ||
| 22 | 2/2✓ Branch 0 taken 20 times. ✓ Branch 1 taken 42 times. | 62 | if (str != NULL) | 
| 23 | 20 | FREE(str); | |
| 24 | 2/2✓ Branch 0 taken 18 times. ✓ Branch 1 taken 44 times. | 62 | if (tab != NULL) | 
| 25 | 18 | FREE_WORD_ARRAY(tab); | |
| 26 | 62 | return NULL; | |
| 27 | } | ||
| 28 | |||
| 29 | /** | ||
| 30 | * @brief Free the input list | ||
| 31 | * @param mysh The shell structure | ||
| 32 | * @return <b>void</b> | ||
| 33 | */ | ||
| 34 | 1947 | void free_input_list(mysh_t *mysh) | |
| 35 | { | ||
| 36 | 2/2✓ Branch 0 taken 772 times. ✓ Branch 1 taken 1175 times. | 1947 | if (mysh->input_list == NULL) | 
| 37 | 772 | return; | |
| 38 | 2/2✓ Branch 0 taken 1389 times. ✓ Branch 1 taken 1175 times. | 2564 | for (int i = 0; mysh->input_list[i] != NULL; i++) | 
| 39 | 1389 | my_free_array((void **)mysh->input_list[i]->args); | |
| 40 | 1175 | my_free_array((void **)mysh->input_list); | |
| 41 | 1175 | mysh->input_list = NULL; | |
| 42 | } | ||
| 43 | |||
| 44 | /** | ||
| 45 | * @brief Check if the exit command has a syntax error | ||
| 46 | * @param mysh The shell structure | ||
| 47 | * @return <b>int</b> <u>1</u> if the command has a syntax error, | ||
| 48 | * <u>0</u> otherwise | ||
| 49 | */ | ||
| 50 | 14 | static int error_handling(mysh_t *mysh) | |
| 51 | { | ||
| 52 | 2/4✓ Branch 1 taken 14 times. ✗ Branch 2 not taken. ✗ Branch 3 not taken. ✓ Branch 4 taken 14 times. | 28 | if (my_char_is(mysh->args[1][0], "-0123456789") == 0 || | 
| 53 | 14 | my_str_contains(mysh->args[1], "0123456789") == 0) { | |
| 54 | ✗ | my_putstr_error("exit: Expression Syntax.\n"); | |
| 55 | ✗ | mysh->exit_status = 1; | |
| 56 | ✗ | return 1; | |
| 57 | } | ||
| 58 | 2/2✓ Branch 0 taken 3 times. ✓ Branch 1 taken 11 times. | 14 | if (my_errno != 0) { | 
| 59 | 3 | my_putstr_error("exit: Badly formed number.\n"); | |
| 60 | 3 | mysh->exit_status = 1; | |
| 61 | 3 | return 1; | |
| 62 | } | ||
| 63 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 11 times. | 11 | if (mysh->args[2] != NULL) { | 
| 64 | ✗ | my_putstr_error("exit: Expression Syntax.\n"); | |
| 65 | ✗ | mysh->exit_status = 1; | |
| 66 | ✗ | return 1; | |
| 67 | } | ||
| 68 | 11 | return 0; | |
| 69 | } | ||
| 70 | |||
| 71 | /** | ||
| 72 | * @brief Free the shell structure and exit the shell | ||
| 73 | * @param mysh The shell structure | ||
| 74 | * @param status The exit status | ||
| 75 | * @param message The message to display | ||
| 76 | * @return <b>void</b> | ||
| 77 | */ | ||
| 78 | 900 | void my_exit(mysh_t *mysh, unsigned char status, char const *message) | |
| 79 | { | ||
| 80 | 3/4✓ Branch 0 taken 748 times. ✓ Branch 1 taken 152 times. ✗ Branch 3 not taken. ✓ Branch 4 taken 748 times. | 900 | if (message != NULL && isatty(0) == 1) | 
| 81 | ✗ | my_putstr_error(message); | |
| 82 | 1/2✓ Branch 0 taken 900 times. ✗ Branch 1 not taken. | 900 | if (mysh != NULL) { | 
| 83 | 900 | close(mysh->config_file); | |
| 84 | 900 | FREE_WORD_ARRAY(mysh->path_list); | |
| 85 | 900 | FREE(mysh->old_pwd); | |
| 86 | 900 | FREE(mysh->line); | |
| 87 | 900 | FREE_WORD_ARRAY(mysh->multi_cmds); | |
| 88 | 900 | my_delete_list(&mysh->operators_cmds); | |
| 89 | 900 | my_delete_list(&mysh->operators_list); | |
| 90 | 900 | FREE_WORD_ARRAY(mysh->pipe_cmds); | |
| 91 | 900 | free_input_list(mysh); | |
| 92 | 2/2✓ Branch 0 taken 526 times. ✓ Branch 1 taken 374 times. | 900 | if (mysh->fd_history != NULL) | 
| 93 | 526 | fclose(mysh->fd_history); | |
| 94 | 900 | my_delete_circle_list(&mysh->history); | |
| 95 | 900 | my_delete_list(&mysh->alias_list); | |
| 96 | 900 | my_delete_list(&mysh->variable_list); | |
| 97 | } | ||
| 98 | 900 | my_free(); | |
| 99 | 900 | exit(status); | |
| 100 | } | ||
| 101 | |||
| 102 | /** | ||
| 103 | * @brief The exit builtin | ||
| 104 | * @param mysh The shell structure | ||
| 105 | * @return <b>int</b> <u>0</u> or the exit value if the command succeed, | ||
| 106 | * <u>1</u> otherwise | ||
| 107 | */ | ||
| 108 | 370 | int exec_exit(mysh_t *mysh) | |
| 109 | { | ||
| 110 | 370 | int value = 0; | |
| 111 | |||
| 112 | 2/2✓ Branch 0 taken 14 times. ✓ Branch 1 taken 356 times. | 370 | if (mysh->args[1] != NULL) { | 
| 113 | 14 | value = my_super_number(mysh->args[1], | |
| 114 | 14 | (number_settings_t){0, 0, 0, 1}); | |
| 115 | 2/2✓ Branch 1 taken 3 times. ✓ Branch 2 taken 11 times. | 14 | if (error_handling(mysh)) | 
| 116 | 3 | return 1; | |
| 117 | 11 | free_input_list(mysh); | |
| 118 | 11 | my_exit(mysh, value, "exit\n"); | |
| 119 | ✗ | return value; | |
| 120 | } | ||
| 121 | 356 | free_input_list(mysh); | |
| 122 | 356 | my_exit(mysh, value, "exit\n"); | |
| 123 | ✗ | return 0; | |
| 124 | } | ||
| 125 |