| 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 | 7 | static int is_builtin(char *command) | |
| 21 | { | ||
| 22 | 2/2✓ Branch 1 taken 40 times. ✓ Branch 2 taken 1 times. | 41 | for (int index = 0; get_builtin_command(index)->name != NULL; index++) { | 
| 23 | 2/2✓ Branch 2 taken 6 times. ✓ Branch 3 taken 34 times. | 40 | if (my_strcmp(command, get_builtin_command(index)->name) == 0) { | 
| 24 | 6 | my_printf("%s: shell built-in command.\n", command); | |
| 25 | 6 | return 1; | |
| 26 | } | ||
| 27 | } | ||
| 28 | 1 | 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 | 7 | static int is_aliased(mysh_t *mysh, char *command) | |
| 38 | { | ||
| 39 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 7 times. | 7 | 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 | 7 | 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 | 1 | static int check_command(mysh_t *mysh, int i) | |
| 57 | { | ||
| 58 | 1 | char *path = NULL; | |
| 59 | |||
| 60 | 1/2✓ Branch 0 taken 1 times. ✗ Branch 1 not taken. | 1 | for (int j = 0; mysh->path_list[j] != NULL; j++) { | 
| 61 | 1 | path = CALLOC(my_strlen(mysh->path_list[j]) + | |
| 62 | my_strlen(mysh->args[i]) + 4, sizeof(char)); | ||
| 63 | 1 | my_strcat(path, mysh->path_list[j]); | |
| 64 | 1 | my_strcat(path, "/"); | |
| 65 | 1 | my_strcat(path, mysh->args[i]); | |
| 66 | 1/2✗ Branch 1 not taken. ✓ Branch 2 taken 1 times. | 1 | if (access(path, X_OK) == 0) { | 
| 67 | ✗ | my_printf("%s\n", path); | |
| 68 | ✗ | return 1; | |
| 69 | } | ||
| 70 | 2/4✓ Branch 1 taken 1 times. ✗ Branch 2 not taken. ✓ Branch 3 taken 1 times. ✗ Branch 4 not taken. | 2 | if (my_str_contains(mysh->args[i], "/") && | 
| 71 | 1 | access(mysh->args[i], X_OK) == 0) { | |
| 72 | 1 | my_printf("%s\n", mysh->args[i]); | |
| 73 | 1 | 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 | 12 | int exec_which(mysh_t *mysh) | |
| 85 | { | ||
| 86 | 2/2✓ Branch 0 taken 5 times. ✓ Branch 1 taken 7 times. | 12 | if (mysh->args[1] == NULL) { | 
| 87 | 5 | my_putstr_error("which: Too few arguments.\n"); | |
| 88 | 5 | return 1; | |
| 89 | } | ||
| 90 | 7 | mysh->exit_status = 0; | |
| 91 | 2/2✓ Branch 0 taken 7 times. ✓ Branch 1 taken 7 times. | 14 | for (int index = 1; mysh->args[index] != NULL; index++) { | 
| 92 | 1/2✓ Branch 1 taken 7 times. ✗ Branch 2 not taken. | 7 | if (is_aliased(mysh, mysh->args[index]) | 
| 93 | 2/2✓ Branch 1 taken 6 times. ✓ Branch 2 taken 1 times. | 7 | || is_builtin(mysh->args[index])) | 
| 94 | 6 | continue; | |
| 95 | 2/4✓ Branch 0 taken 1 times. ✗ Branch 1 not taken. ✗ Branch 2 not taken. ✓ Branch 3 taken 1 times. | 1 | if (mysh->path_list == NULL || mysh->path_list[0] == NULL) { | 
| 96 | ✗ | my_printf("%s: Command not found.\n", mysh->args[index]); | |
| 97 | ✗ | mysh->exit_status = 1; | |
| 98 | ✗ | continue; | |
| 99 | } | ||
| 100 | 1/2✓ Branch 1 taken 1 times. ✗ Branch 2 not taken. | 1 | if (check_command(mysh, index)) | 
| 101 | 1 | continue; | |
| 102 | ✗ | my_printf("%s: Command not found.\n", mysh->args[index]); | |
| 103 | ✗ | mysh->exit_status = 1; | |
| 104 | } | ||
| 105 | 7 | return mysh->exit_status; | |
| 106 | } | ||
| 107 |