| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the which builtin | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file which.c | ||
| 9 | * @brief The file containing the which builtin | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Check if the command is a builtin and print it | ||
| 16 | * @param command The command to check | ||
| 17 | * @return <b>int</b> <u>1</u> if the command is a builtin, | ||
| 18 | * <u>0</u> otherwise | ||
| 19 | */ | ||
| 20 | 14 | static int is_builtin(char *command) | |
| 21 | { | ||
| 22 |
2/2✓ Branch 1 taken 308 times.
✓ Branch 2 taken 14 times.
|
322 | for (int index = 0; get_builtin_command(index)->name != NULL; index++) { |
| 23 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 308 times.
|
308 | if (my_strcmp(command, get_builtin_command(index)->name) == 0) { |
| 24 | ✗ | my_printf("%s: shell built-in command.\n", command); | |
| 25 | ✗ | return 1; | |
| 26 | } | ||
| 27 | } | ||
| 28 | 14 | return 0; | |
| 29 | } | ||
| 30 | |||
| 31 | /** | ||
| 32 | * @brief Check if the command is aliased and print it | ||
| 33 | * @param mysh The shell structure | ||
| 34 | * @param command The command to check | ||
| 35 | * @return <b>int</b> <u>1</u> if the command is aliased, <u>0</u> otherwise | ||
| 36 | */ | ||
| 37 | 14 | static int is_aliased(mysh_t *mysh, char *command) | |
| 38 | { | ||
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | for (node_t *tmp = mysh->alias_list; tmp != NULL; tmp = tmp->next) { |
| 40 | ✗ | if (my_strcmp(((alias_t *)tmp->data)->name, command) == 0) { | |
| 41 | ✗ | my_printf("%s: \t aliased to %s\n", command, | |
| 42 | ✗ | ((alias_t *) tmp->data)->value); | |
| 43 | ✗ | return 1; | |
| 44 | } | ||
| 45 | } | ||
| 46 | 14 | return 0; | |
| 47 | } | ||
| 48 | |||
| 49 | /** | ||
| 50 | * @biref Check if the command exist and print it | ||
| 51 | * @param mysh The shell structure | ||
| 52 | * @param i The index of the command | ||
| 53 | * @return <b>int</b> <u>1</u> if the command is exist, | ||
| 54 | * <u>0</u> otherwise | ||
| 55 | */ | ||
| 56 | 13 | static int check_command(mysh_t *mysh, int i) | |
| 57 | { | ||
| 58 | 13 | char *path = NULL; | |
| 59 | |||
| 60 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | for (int j = 0; mysh->path_list[j] != NULL; j++) { |
| 61 | 13 | path = CALLOC(my_strlen(mysh->path_list[j]) + | |
| 62 | my_strlen(mysh->args[i]) + 4, sizeof(char)); | ||
| 63 | 13 | my_strcat(path, mysh->path_list[j]); | |
| 64 | 13 | my_strcat(path, "/"); | |
| 65 | 13 | my_strcat(path, mysh->args[i]); | |
| 66 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 2 taken 2 times.
|
13 | if (access(path, X_OK) == 0) { |
| 67 | 11 | my_printf("%s\n", path); | |
| 68 | 11 | return 1; | |
| 69 | } | ||
| 70 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
4 | if (my_str_contains(mysh->args[i], "/") && |
| 71 | 2 | access(mysh->args[i], X_OK) == 0) { | |
| 72 | 2 | my_printf("%s\n", mysh->args[i]); | |
| 73 | 2 | return 1; | |
| 74 | } | ||
| 75 | } | ||
| 76 | ✗ | return 0; | |
| 77 | } | ||
| 78 | |||
| 79 | /** | ||
| 80 | * @brief The which builtin | ||
| 81 | * @param mysh The shell structure | ||
| 82 | * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise | ||
| 83 | */ | ||
| 84 | 10 | int exec_which(mysh_t *mysh) | |
| 85 | { | ||
| 86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (mysh->args[1] == NULL) { |
| 87 | ✗ | my_putstr_error("which: Too few arguments.\n"); | |
| 88 | ✗ | return 1; | |
| 89 | } | ||
| 90 | 10 | mysh->exit_status = 0; | |
| 91 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 10 times.
|
24 | for (int index = 1; mysh->args[index] != NULL; index++) { |
| 92 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | if (is_aliased(mysh, mysh->args[index]) |
| 93 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
|
14 | || is_builtin(mysh->args[index])) |
| 94 | ✗ | continue; | |
| 95 |
3/4✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 13 times.
|
14 | if (mysh->path_list == NULL || mysh->path_list[0] == NULL) { |
| 96 | 1 | my_printf("%s: Command not found.\n", mysh->args[index]); | |
| 97 | 1 | mysh->exit_status = 1; | |
| 98 | 1 | continue; | |
| 99 | } | ||
| 100 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | if (check_command(mysh, index)) |
| 101 | 13 | continue; | |
| 102 | ✗ | my_printf("%s: Command not found.\n", mysh->args[index]); | |
| 103 | ✗ | mysh->exit_status = 1; | |
| 104 | } | ||
| 105 | 10 | return mysh->exit_status; | |
| 106 | } | ||
| 107 |