GCC Code Coverage Report


Directory: ./
File: src/builtins/where.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 37 42 88.1%
Functions: 4 4 100.0%
Branches: 15 20 75.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 4 static int is_builtin(char *command)
21 {
22
2/2
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 4 times.
92 for (int index = 0; get_builtin_command(index)->name != NULL; index++) {
23
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 88 times.
88 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 4 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 4 static int is_aliased(mysh_t *mysh, char *command)
38 {
39
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (node_t *tmp = mysh->alias_list; tmp != NULL; tmp = tmp->next) {
40
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (my_strcmp(((alias_t *)tmp->data)->name, command) == 0) {
41 2 my_printf("%s is aliased to %s\n", command,
42 2 ((alias_t *) tmp->data)->value);
43 2 return 1;
44 }
45 }
46 2 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 4 static int check_command(mysh_t *mysh, int i)
57 {
58 4 char *path = NULL;
59 4 int find = 0;
60
61
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (int j = 0; mysh->path_list[j] != NULL; j++) {
62 8 path = CALLOC(my_strlen(mysh->path_list[j]) +
63 my_strlen(mysh->args[i]) + 4, sizeof(char));
64 8 my_strcat(path, mysh->path_list[j]);
65 8 my_strcat(path, "/");
66 8 my_strcat(path, mysh->args[i]);
67
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 if (access(path, F_OK | X_OK) == 0) {
68 8 my_printf("%s\n", path);
69 8 find = 1;
70 }
71 }
72 4 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 8 int exec_where(mysh_t *mysh)
81 {
82 8 int find = 0;
83
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (mysh->args[1] == NULL) {
85 my_putstr_error("where: Too few arguments.\n");
86 return 1;
87 }
88 8 mysh->exit_status = 0;
89
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 for (int index = 1; mysh->args[index] != NULL; index++) {
90
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
8 if (my_str_contains(mysh->args[index], "/")) {
91 4 my_printf("where: / in command makes no sense\n",
92 4 mysh->args[index]);
93 4 mysh->exit_status = 1;
94 4 continue;
95 }
96 4 find += is_aliased(mysh, mysh->args[index]) +
97 4 is_builtin(mysh->args[index]) + check_command(mysh, index);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (find == 0)
99 mysh->exit_status = 1;
100 }
101 8 return mysh->exit_status;
102 }
103