GCC Code Coverage Report


Directory: ./
File: src/globbing/globbing.c
Date: 2024-05-07 14:54:03
Exec Total Coverage
Lines: 21 25 84.0%
Functions: 2 2 100.0%
Branches: 9 12 75.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the globbing functions
6 */
7 /**
8 * @file globbing.c
9 * @brief The file containing the globbing functions
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief Get the file list from a globbing argument
16 * @param arg The argument
17 * @param args_list The list of arguments for the command
18 * @return <b>int</b> <u>0</u> if success, <u>-1</u> if error
19 */
20 19 int get_args_list(char *arg, node_t **args_list)
21 {
22 glob_t glob_r;
23 int result;
24
25 19 result = glob(arg, GLOB_TILDE, NULL, &glob_r);
26
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (result == 0) {
27
2/2
✓ Branch 0 taken 3427 times.
✓ Branch 1 taken 19 times.
3446 for (size_t j = 0; j < glob_r.gl_pathc; j++)
28 3427 my_push_back(args_list,
29 3427 my_malloc_strdup(glob_r.gl_pathv[j]), UNKNOWN);
30 19 globfree(&glob_r);
31 } else
32 return -1;
33 19 return 0;
34 }
35
36 /**
37 * @brief Analyse and change the arguments if there are globbing
38 * @param args The command arguments
39 * @return <b>char **</b> The new command arguments
40 * @note Pushing each argument in a list and then converting it to an array
41 */
42 564 char **globbing(char **args)
43 {
44 564 node_t *args_list = NULL;
45 564 int result = 0;
46 564 char **new_args = NULL;
47
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 564 times.
564 if (args == NULL)
49 return NULL;
50
2/2
✓ Branch 0 taken 1920 times.
✓ Branch 1 taken 564 times.
2484 for (int i = 0; args[i]; i++) {
51
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 1901 times.
1920 if (my_str_contains(args[i], "*?[]"))
52 19 result = get_args_list(args[i], &args_list);
53 else
54 1901 my_push_back(&args_list, args[i], UNKNOWN);
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1920 times.
1920 if (result == -1) {
56 my_fprintf(2, "%s: No match.\n", args[0]);
57 return NULL;
58 }
59 }
60 564 new_args = (char **)my_list_to_array(args_list);
61 564 my_delete_list(&args_list);
62 564 return new_args;
63 }
64