Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2024 | ||
3 | ** 42sh | ||
4 | ** File description: | ||
5 | ** The file containing the tilde functions | ||
6 | */ | ||
7 | /** | ||
8 | * @file tilde.c | ||
9 | * @brief The file containing the tilde functions | ||
10 | */ | ||
11 | |||
12 | #include "../../include/myshell.h" | ||
13 | |||
14 | /** | ||
15 | * @brief Replace the tilde by the home path | ||
16 | * @param mysh The shell structure | ||
17 | * @param home The home path | ||
18 | * @brief index The index of the tilde | ||
19 | * @return <b>int</b> <u>0</u> if the replacement is successful, | ||
20 | * <u>1</u> otherwise | ||
21 | */ | ||
22 | ✗ | static int replace_tilde(mysh_t *mysh, char *home, int index) | |
23 | { | ||
24 | ✗ | char *new_str = NULL; | |
25 | |||
26 | ✗ | if (home == NULL) { | |
27 | ✗ | my_putstr_error("No $home variable set.\n"); | |
28 | ✗ | mysh->exit_status = 1; | |
29 | ✗ | return 1; | |
30 | } | ||
31 | ✗ | new_str = calloc((my_strlen(mysh->line) | |
32 | ✗ | + my_strlen(home) + 1), sizeof(char)); | |
33 | ✗ | my_strncpy(new_str, mysh->line, index); | |
34 | ✗ | my_strcat(new_str, home); | |
35 | ✗ | my_strcat(new_str, &mysh->line[index + 1]); | |
36 | ✗ | FREE(mysh->line); | |
37 | ✗ | mysh->line = new_str; | |
38 | ✗ | return 0; | |
39 | } | ||
40 | |||
41 | /** | ||
42 | * @brief Transform the tilde in the command line by the home path | ||
43 | * @param mysh The shell structure | ||
44 | * @return <b>int</b> <u>0</u> if the replacement is successful, | ||
45 | * <u>1</u> otherwise | ||
46 | */ | ||
47 | 4774 | int check_tilde(mysh_t *mysh) | |
48 | { | ||
49 | 4774 | char *home = get_env_var(mysh->env, "HOME"); | |
50 | 4774 | int result = 0; | |
51 | |||
52 |
3/4✓ Branch 0 taken 43263 times.
✓ Branch 1 taken 4774 times.
✓ Branch 2 taken 43263 times.
✗ Branch 3 not taken.
|
48037 | for (int index = 0; mysh->line[index] != '\0' && result == 0; index++) { |
53 |
4/4✓ Branch 0 taken 42219 times.
✓ Branch 1 taken 1044 times.
✓ Branch 3 taken 2645 times.
✓ Branch 4 taken 39574 times.
|
43263 | if ((index == 0 || my_char_is(mysh->line[index - 1], "( \t\n") == 1) && |
54 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 3689 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
3689 | mysh->line[index] == '~' && char_is_inhibited(mysh->line, index) == 0) |
55 | ✗ | result = replace_tilde(mysh, home, index); | |
56 | } | ||
57 | 4774 | return result; | |
58 | } | ||
59 |