Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2024 | ||
3 | ** 42sh | ||
4 | ** File description: | ||
5 | ** The file containing the environnement functions | ||
6 | */ | ||
7 | /** | ||
8 | * @file environnement.c | ||
9 | * @brief The file containing the environnement functions | ||
10 | */ | ||
11 | |||
12 | #include "../include/myshell.h" | ||
13 | |||
14 | /** | ||
15 | * @brief Set a new environment variable | ||
16 | * @param mysh The shell structure | ||
17 | * @param name The name of the variable | ||
18 | * @param value The value of the variable | ||
19 | * @return <b>void</b> | ||
20 | */ | ||
21 | 1444 | static char *var_to_str(char *var, int start) | |
22 | { | ||
23 |
1/2✓ Branch 0 taken 1444 times.
✗ Branch 1 not taken.
|
1444 | for (int index = start; var[index] != '\0'; index++) { |
24 |
1/2✓ Branch 0 taken 1444 times.
✗ Branch 1 not taken.
|
1444 | if (var[index] == '=') |
25 | 1444 | return &var[index + 1]; | |
26 | } | ||
27 | ✗ | return NULL; | |
28 | } | ||
29 | |||
30 | /** | ||
31 | * @brief Get an environment variable | ||
32 | * @param env The environment | ||
33 | * @param var The variable to get | ||
34 | * @return <b>char *</b> The value of the variable | ||
35 | */ | ||
36 | 7124 | char *get_env_var(char **env, char *var) | |
37 | { | ||
38 |
2/2✓ Branch 0 taken 10612 times.
✓ Branch 1 taken 5680 times.
|
16292 | for (int index = 0; env[index] != NULL; index++) { |
39 |
2/2✓ Branch 2 taken 1444 times.
✓ Branch 3 taken 9168 times.
|
10612 | if (my_strncmp(env[index], var, my_strlen(var)) == 0) |
40 | 1444 | return var_to_str(env[index], my_strlen(var)); | |
41 | } | ||
42 | 5680 | return NULL; | |
43 | } | ||
44 | |||
45 | /** | ||
46 | * @brief Replace an environment variable | ||
47 | * @param env The environment | ||
48 | * @param name The name of the variable | ||
49 | * @param value The new value | ||
50 | * @return <b>void</b> | ||
51 | */ | ||
52 | 3 | void replace_env_var(char **env, char *name, char *value) | |
53 | { | ||
54 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
|
8 | for (int index = 0; env[index] != NULL; index++) { |
55 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
5 | if (my_strncmp(env[index], name, my_strlen(name)) == 0 |
56 | ✗ | && env[index][my_strlen(name)] == '=') { | |
57 | ✗ | env[index] = value; | |
58 | ✗ | return; | |
59 | } | ||
60 | } | ||
61 | } | ||
62 | |||
63 | /** | ||
64 | * @brief Check if the PATH is set in the environment and set it if not | ||
65 | * @param mysh The shell structure | ||
66 | * @return <b>void</b> | ||
67 | */ | ||
68 | 371 | void check_path(mysh_t *mysh) | |
69 | { | ||
70 |
2/2✓ Branch 1 taken 117 times.
✓ Branch 2 taken 254 times.
|
371 | if (get_env_var(mysh->env, "PATH") == NULL) |
71 | 117 | set_new_env_var(mysh, "PATH", "/usr/bin:/bin"); | |
72 | 371 | } | |
73 | |||
74 | /** | ||
75 | * @brief Update the path list | ||
76 | * @param mysh The shell structure | ||
77 | * @return <b>void</b> | ||
78 | */ | ||
79 | 492 | void update_path_list(mysh_t *mysh) | |
80 | { | ||
81 |
2/2✓ Branch 1 taken 117 times.
✓ Branch 2 taken 375 times.
|
492 | if (get_env_var(mysh->env, "PATH") == NULL) |
82 | 117 | return; | |
83 | 375 | FREE_WORD_ARRAY(mysh->path_list); | |
84 | 375 | mysh->path_list = my_str_to_word_array_select( | |
85 | 375 | get_env_var(mysh->env, "PATH"), ":"); | |
86 | } | ||
87 |