| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the history_file functions | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file history_file.c | ||
| 9 | * @brief The file containing the history_file functions | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Set the command in the history | ||
| 16 | * @param mysh The shell structure | ||
| 17 | * @param line The line to set in the history | ||
| 18 | * @return <b>void</b> | ||
| 19 | */ | ||
| 20 | 1018 | void set_command_in_history(mysh_t *mysh, char *line) | |
| 21 | { | ||
| 22 | 1018 | char *buffer = NULL; | |
| 23 | 1018 | size_t len = 0; | |
| 24 | |||
| 25 | 2/2✓ Branch 0 taken 7 times. ✓ Branch 1 taken 1011 times. | 1018 | if (line == NULL) | 
| 26 | 7 | return; | |
| 27 | 1/2✗ Branch 1 not taken. ✓ Branch 2 taken 1011 times. | 1011 | while (getline(&buffer, &len, mysh->fd_history) != EOF) { | 
| 28 | ✗ | if (buffer[my_strlen(buffer) - 1] == '\n') | |
| 29 | ✗ | buffer[my_strlen(buffer) - 1] = '\0'; | |
| 30 | ✗ | if (buffer[0] != '\0') | |
| 31 | ✗ | my_push_back_circled(&mysh->history, my_strdup(buffer), STRING); | |
| 32 | } | ||
| 33 | 1011 | FREE(buffer); | |
| 34 | 1011 | fwrite(line, my_strlen(line), 1, mysh->fd_history); | |
| 35 | 2/4✓ Branch 0 taken 1011 times. ✗ Branch 1 not taken. ✓ Branch 2 taken 1011 times. ✗ Branch 3 not taken. | 1011 | if (line != NULL && line[0] != '\0') | 
| 36 | 1011 | my_push_back_circled(&mysh->history, my_strdup(line), STRING); | |
| 37 | } | ||
| 38 | |||
| 39 | /** | ||
| 40 | * @brief Create the history file | ||
| 41 | * @param mysh The shell structure | ||
| 42 | * @return <b>void</b> | ||
| 43 | */ | ||
| 44 | 371 | void create_history(mysh_t *mysh) | |
| 45 | { | ||
| 46 | 371 | char *home = get_env_var(mysh->env, "HOME"); | |
| 47 | 371 | size_t len = 0; | |
| 48 | 371 | char *buffer = NULL; | |
| 49 | |||
| 50 | 1/2✓ Branch 0 taken 371 times. ✗ Branch 1 not taken. | 371 | if (home == NULL) | 
| 51 | 371 | mysh->history_path = my_malloc_strdup("./.42sh_history"); | |
| 52 | else { | ||
| 53 | ✗ | mysh->history_path = CALLOC(my_strlen(home) + 16, sizeof(char)); | |
| 54 | ✗ | my_strcat(mysh->history_path, home); | |
| 55 | ✗ | my_strcat(mysh->history_path, "/.42sh_history\0"); | |
| 56 | } | ||
| 57 | 371 | mysh->fd_history = fopen(mysh->history_path, "a+"); | |
| 58 | 3/4✓ Branch 0 taken 217391 times. ✗ Branch 1 not taken. ✓ Branch 2 taken 217020 times. ✓ Branch 3 taken 371 times. | 434782 | while (mysh->fd_history != NULL && | 
| 59 | 217391 | getline(&buffer, &len, mysh->fd_history) != EOF) { | |
| 60 | 1/2✓ Branch 1 taken 217020 times. ✗ Branch 2 not taken. | 217020 | if (buffer[my_strlen(buffer) - 1] == '\n') | 
| 61 | 217020 | buffer[my_strlen(buffer) - 1] = '\0'; | |
| 62 | 2/2✓ Branch 0 taken 215954 times. ✓ Branch 1 taken 1066 times. | 217020 | if (buffer[0] != '\0') | 
| 63 | 215954 | my_push_back_circled(&mysh->history, my_strdup(buffer), STRING); | |
| 64 | } | ||
| 65 | 371 | FREE(buffer); | |
| 66 | 371 | } | |
| 67 |