| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the history builtins | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file setenv.c | ||
| 9 | * @brief The file containing the history builtins | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Display the history | ||
| 16 | * @param mysh The shell structure | ||
| 17 | * @return <b>void</b> | ||
| 18 | */ | ||
| 19 | ✗ | static void display_history(mysh_t *mysh) | |
| 20 | { | ||
| 21 | ✗ | int index = 0; | |
| 22 | |||
| 23 | ✗ | for (node_t *tmp = mysh->history; tmp != NULL; tmp = tmp->next) { | |
| 24 | ✗ | my_printf("%d %s\n", index + 1, (char *)tmp->data); | |
| 25 | ✗ | index++; | |
| 26 | ✗ | if (tmp->next == mysh->history) | |
| 27 | ✗ | break; | |
| 28 | } | ||
| 29 | ✗ | } | |
| 30 | |||
| 31 | /** | ||
| 32 | * @brief Print the n last command of the history | ||
| 33 | * @param mysh The shell structure | ||
| 34 | * @return <b>void</b> | ||
| 35 | */ | ||
| 36 | ✗ | static void print_number_history(mysh_t *mysh) | |
| 37 | { | ||
| 38 | ✗ | int size = my_list_size_circled(mysh->history); | |
| 39 | ✗ | int index = 1; | |
| 40 | ✗ | int nb = my_getnbr(mysh->args[1]); | |
| 41 | |||
| 42 | ✗ | for (node_t *tmp = mysh->history; tmp != NULL; tmp = tmp->next) { | |
| 43 | ✗ | if (index > size - nb) | |
| 44 | ✗ | my_printf("%d %s\n", index, (char *)tmp->data); | |
| 45 | ✗ | index++; | |
| 46 | ✗ | if (tmp->next == mysh->history && nb == 0) { | |
| 47 | ✗ | my_printf("%d %s\n", index - 1, (char *)tmp->data); | |
| 48 | ✗ | break; | |
| 49 | } | ||
| 50 | ✗ | if (tmp->next == mysh->history) | |
| 51 | ✗ | break; | |
| 52 | } | ||
| 53 | ✗ | } | |
| 54 | |||
| 55 | /** | ||
| 56 | * @brief The history builtin | ||
| 57 | * @param mysh The shell structure | ||
| 58 | * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise | ||
| 59 | */ | ||
| 60 | ✗ | int exec_history(mysh_t *mysh) | |
| 61 | { | ||
| 62 | ✗ | if (my_array_len((void **)mysh->args) >= 3) { | |
| 63 | ✗ | my_fprintf(2, "history: Too many arguments.\n"); | |
| 64 | ✗ | return 1; | |
| 65 | } | ||
| 66 | ✗ | if (mysh->args[1] == NULL) { | |
| 67 | ✗ | display_history(mysh); | |
| 68 | ✗ | return 0; | |
| 69 | } | ||
| 70 | ✗ | if (!my_str_isnum(mysh->args[1])) { | |
| 71 | ✗ | my_fprintf(2, "history: Badly formed number.\n"); | |
| 72 | ✗ | return 1; | |
| 73 | } | ||
| 74 | ✗ | print_number_history(mysh); | |
| 75 | ✗ | return 0; | |
| 76 | } | ||
| 77 |