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 | 18 | int get_args_list(char *arg, node_t **args_list) | |
21 | { | ||
22 | glob_t glob_r; | ||
23 | int result; | ||
24 | |||
25 | 18 | result = glob(arg, GLOB_TILDE, NULL, &glob_r); | |
26 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | if (result == 0) { |
27 |
2/2✓ Branch 0 taken 3308 times.
✓ Branch 1 taken 18 times.
|
3326 | for (size_t j = 0; j < glob_r.gl_pathc; j++) |
28 | 3308 | my_push_back(args_list, | |
29 | 3308 | my_malloc_strdup(glob_r.gl_pathv[j]), UNKNOWN); | |
30 | 18 | globfree(&glob_r); | |
31 | } else | ||
32 | ✗ | return -1; | |
33 | 18 | 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 | 483 | char **globbing(char **args) | |
43 | { | ||
44 | 483 | node_t *args_list = NULL; | |
45 | 483 | int result = 0; | |
46 | 483 | char **new_args = NULL; | |
47 | |||
48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 483 times.
|
483 | if (args == NULL) |
49 | ✗ | return NULL; | |
50 |
2/2✓ Branch 0 taken 6417 times.
✓ Branch 1 taken 483 times.
|
6900 | for (int i = 0; args[i]; i++) { |
51 |
2/2✓ Branch 1 taken 18 times.
✓ Branch 2 taken 6399 times.
|
6417 | if (my_str_contains(args[i], "*?[]")) |
52 | 18 | result = get_args_list(args[i], &args_list); | |
53 | else | ||
54 | 6399 | my_push_back(&args_list, args[i], UNKNOWN); | |
55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6417 times.
|
6417 | if (result == -1) { |
56 | ✗ | my_fprintf(2, "%s: No match.\n", args[0]); | |
57 | ✗ | return NULL; | |
58 | } | ||
59 | } | ||
60 | 483 | new_args = (char **)my_list_to_array(args_list); | |
61 | 483 | my_delete_list(&args_list); | |
62 | 483 | return new_args; | |
63 | } | ||
64 |