GCC Code Coverage Report


Directory: ./
File: src/environnement.c
Date: 2024-05-07 14:54:03
Exec Total Coverage
Lines: 22 26 84.6%
Functions: 5 5 100.0%
Branches: 13 18 72.2%

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 1517 static char *var_to_str(char *var, int start)
22 {
23
1/2
✓ Branch 0 taken 1517 times.
✗ Branch 1 not taken.
1517 for (int index = start; var[index] != '\0'; index++) {
24
1/2
✓ Branch 0 taken 1517 times.
✗ Branch 1 not taken.
1517 if (var[index] == '=')
25 1517 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 7259 char *get_env_var(char **env, char *var)
37 {
38
2/2
✓ Branch 0 taken 10957 times.
✓ Branch 1 taken 5742 times.
16699 for (int index = 0; env[index] != NULL; index++) {
39
2/2
✓ Branch 2 taken 1517 times.
✓ Branch 3 taken 9440 times.
10957 if (my_strncmp(env[index], var, my_strlen(var)) == 0)
40 1517 return var_to_str(env[index], my_strlen(var));
41 }
42 5742 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 6 void replace_env_var(char **env, char *name, char *value)
53 {
54
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 6 times.
16 for (int index = 0; env[index] != NULL; index++) {
55
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
10 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 366 void check_path(mysh_t *mysh)
69 {
70
2/2
✓ Branch 1 taken 118 times.
✓ Branch 2 taken 248 times.
366 if (get_env_var(mysh->env, "PATH") == NULL)
71 118 set_new_env_var(mysh, "PATH",
72 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
73 366 }
74
75 /**
76 * @brief Update the path list
77 * @param mysh The shell structure
78 * @return <b>void</b>
79 */
80 493 void update_path_list(mysh_t *mysh)
81 {
82
2/2
✓ Branch 1 taken 118 times.
✓ Branch 2 taken 375 times.
493 if (get_env_var(mysh->env, "PATH") == NULL)
83 118 return;
84 375 FREE_WORD_ARRAY(mysh->path_list);
85 375 mysh->path_list = my_str_to_word_array_select(
86 375 get_env_var(mysh->env, "PATH"), ":");
87 }
88