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 | 1 | static int replace_tilde(mysh_t *mysh, char *home, int index) | |
23 | { | ||
24 | 1 | char *new_str = NULL; | |
25 | |||
26 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (home == NULL) { |
27 | 1 | my_putstr_error("No $home variable set.\n"); | |
28 | 1 | mysh->exit_status = 1; | |
29 | 1 | 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 | 4699 | int check_tilde(mysh_t *mysh) | |
48 | { | ||
49 | 4699 | char *home = get_env_var(mysh->env, "HOME"); | |
50 | 4699 | int result = 0; | |
51 | |||
52 |
4/4✓ Branch 0 taken 179011 times.
✓ Branch 1 taken 4698 times.
✓ Branch 2 taken 179010 times.
✓ Branch 3 taken 1 times.
|
183709 | for (int index = 0; mysh->line[index] != '\0' && result == 0; index++) { |
53 |
4/4✓ Branch 0 taken 178051 times.
✓ Branch 1 taken 959 times.
✓ Branch 3 taken 6862 times.
✓ Branch 4 taken 171189 times.
|
179010 | if ((index == 0 || my_char_is(mysh->line[index - 1], "( \t\n") == 1) && |
54 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7820 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
7821 | mysh->line[index] == '~' && char_is_inhibited(mysh->line, index) == 0) |
55 | 1 | result = replace_tilde(mysh, home, index); | |
56 | } | ||
57 | 4699 | return result; | |
58 | } | ||
59 |