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 | 23 | void *free_str_and_tab(char *str, char **tab) | |
21 | { | ||
22 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 16 times.
|
23 | if (str != NULL) |
23 | 7 | FREE(str); | |
24 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 16 times.
|
23 | if (tab != NULL) |
25 | 7 | FREE_WORD_ARRAY(tab); | |
26 | 23 | return NULL; | |
27 | } | ||
28 | |||
29 | /** | ||
30 | * @brief Free the input list | ||
31 | * @param mysh The shell structure | ||
32 | * @return <b>void</b> | ||
33 | */ | ||
34 | 2084 | void free_input_list(mysh_t *mysh) | |
35 | { | ||
36 |
2/2✓ Branch 0 taken 778 times.
✓ Branch 1 taken 1306 times.
|
2084 | if (mysh->input_list == NULL) |
37 | 778 | return; | |
38 |
2/2✓ Branch 0 taken 1564 times.
✓ Branch 1 taken 1306 times.
|
2870 | for (int i = 0; mysh->input_list[i] != NULL; i++) |
39 | 1564 | my_free_array((void **)mysh->input_list[i]->args); | |
40 | 1306 | my_free_array((void **)mysh->input_list); | |
41 | 1306 | 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 | 10 | static int error_handling(mysh_t *mysh) | |
51 | { | ||
52 |
3/4✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
|
18 | if (my_char_is(mysh->args[1][0], "-0123456789") == 0 || |
53 | 8 | my_str_contains(mysh->args[1], "0123456789") == 0) { | |
54 | 2 | my_putstr_error("exit: Expression Syntax.\n"); | |
55 | 2 | mysh->exit_status = 1; | |
56 | 2 | return 1; | |
57 | } | ||
58 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (my_errno != 0) { |
59 | ✗ | my_putstr_error("exit: Badly formed number.\n"); | |
60 | ✗ | mysh->exit_status = 1; | |
61 | ✗ | return 1; | |
62 | } | ||
63 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
|
8 | if (mysh->args[2] != NULL) { |
64 | 1 | my_putstr_error("exit: Expression Syntax.\n"); | |
65 | 1 | mysh->exit_status = 1; | |
66 | 1 | return 1; | |
67 | } | ||
68 | 7 | 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 | 919 | void my_exit(mysh_t *mysh, unsigned char status, char const *message) | |
79 | { | ||
80 |
3/4✓ Branch 0 taken 737 times.
✓ Branch 1 taken 182 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 737 times.
|
919 | if (message != NULL && isatty(0) == 1) |
81 | ✗ | my_putstr_error(message); | |
82 |
1/2✓ Branch 0 taken 919 times.
✗ Branch 1 not taken.
|
919 | if (mysh != NULL) { |
83 | 919 | close(mysh->config_file); | |
84 | 919 | FREE_WORD_ARRAY(mysh->path_list); | |
85 | 919 | FREE(mysh->old_pwd); | |
86 | 919 | FREE(mysh->line); | |
87 | 919 | FREE_WORD_ARRAY(mysh->multi_cmds); | |
88 | 919 | my_delete_list(&mysh->operators_cmds); | |
89 | 919 | my_delete_list(&mysh->operators_list); | |
90 | 919 | FREE_WORD_ARRAY(mysh->pipe_cmds); | |
91 | 919 | free_input_list(mysh); | |
92 |
2/2✓ Branch 0 taken 545 times.
✓ Branch 1 taken 374 times.
|
919 | if (mysh->fd_history != NULL) |
93 | 545 | fclose(mysh->fd_history); | |
94 | 919 | my_delete_circle_list(&mysh->history); | |
95 | 919 | my_delete_list(&mysh->alias_list); | |
96 | 919 | my_delete_list(&mysh->variable_list); | |
97 | } | ||
98 | 919 | my_free(); | |
99 | 919 | 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 | 360 | int exec_exit(mysh_t *mysh) | |
109 | { | ||
110 | 360 | int value = 0; | |
111 | |||
112 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 350 times.
|
360 | if (mysh->args[1] != NULL) { |
113 | 10 | value = my_super_number(mysh->args[1], | |
114 | 10 | (number_settings_t){0, 0, 0, 1}); | |
115 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 7 times.
|
10 | if (error_handling(mysh)) |
116 | 3 | return 1; | |
117 | 7 | free_input_list(mysh); | |
118 | 7 | my_exit(mysh, value, "exit\n"); | |
119 | ✗ | return value; | |
120 | } | ||
121 | 350 | free_input_list(mysh); | |
122 | 350 | my_exit(mysh, value, "exit\n"); | |
123 | ✗ | return 0; | |
124 | } | ||
125 |