GCC Code Coverage Report


Directory: ./
File: src/builtins/where.c
Date: 2024-05-07 14:54:03
Exec Total Coverage
Lines: 31 42 73.8%
Functions: 4 4 100.0%
Branches: 14 20 70.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the where builtin
6 */
7 /**
8 * @file where.c
9 * @brief The file containing the where 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 12 static int is_builtin(char *command)
21 {
22
2/2
✓ Branch 1 taken 264 times.
✓ Branch 2 taken 12 times.
276 for (int index = 0; get_builtin_command(index)->name != NULL; index++) {
23
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 264 times.
264 if (my_strcmp(command, get_builtin_command(index)->name) == 0) {
24 my_printf("%s is a shell built-in\n", command);
25 return 1;
26 }
27 }
28 12 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 12 static int is_aliased(mysh_t *mysh, char *command)
38 {
39
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 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 is aliased to %s\n", command,
42 ((alias_t *) tmp->data)->value);
43 return 1;
44 }
45 }
46 12 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 12 static int check_command(mysh_t *mysh, int i)
57 {
58 12 char *path = NULL;
59 12 int find = 0;
60
61
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 12 times.
60 for (int j = 0; mysh->path_list[j] != NULL; j++) {
62 48 path = CALLOC(my_strlen(mysh->path_list[j]) +
63 my_strlen(mysh->args[i]) + 4, sizeof(char));
64 48 my_strcat(path, mysh->path_list[j]);
65 48 my_strcat(path, "/");
66 48 my_strcat(path, mysh->args[i]);
67
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 24 times.
48 if (access(path, F_OK | X_OK) == 0) {
68 24 my_printf("%s\n", path);
69 24 find = 1;
70 }
71 }
72 12 return find;
73 }
74
75 /**
76 * @brief The where builtin
77 * @param mysh The shell structure
78 * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise
79 */
80 12 int exec_where(mysh_t *mysh)
81 {
82 12 int find = 0;
83
84
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (mysh->args[1] == NULL) {
85 2 my_putstr_error("where: Too few arguments.\n");
86 2 return 1;
87 }
88 10 mysh->exit_status = 0;
89
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 10 times.
22 for (int index = 1; mysh->args[index] != NULL; index++) {
90
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (my_str_contains(mysh->args[index], "/")) {
91 my_printf("where: / in command makes no sense\n",
92 mysh->args[index]);
93 mysh->exit_status = 1;
94 continue;
95 }
96 12 find += is_aliased(mysh, mysh->args[index]) +
97 12 is_builtin(mysh->args[index]) + check_command(mysh, index);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (find == 0)
99 mysh->exit_status = 1;
100 }
101 10 return mysh->exit_status;
102 }
103