GCC Code Coverage Report


Directory: ./
File: src/builtins/setenv.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 46 49 93.9%
Functions: 4 4 100.0%
Branches: 31 40 77.5%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the setenv builtin
6 */
7 /**
8 * @file setenv.c
9 * @brief The file containing the setenv builtin
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief Check if a string is valid to be an environment variable
16 * @param str The string to check
17 * @return <b>int</b> <u>1</u> if the string is valid, <u>0</u> otherwise
18 */
19 120 int my_str_is_valid(char const *str)
20 {
21
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 120 times.
120 if (my_strlen(str) == 0)
22 return 1;
23
2/2
✓ Branch 1 taken 479 times.
✓ Branch 2 taken 119 times.
598 for (int i = 0; i < my_strlen(str); i++) {
24
1/2
✓ Branch 0 taken 479 times.
✗ Branch 1 not taken.
479 if (str[i] != '_' &&
25
4/6
✓ Branch 0 taken 479 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 479 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 478 times.
✓ Branch 5 taken 1 times.
479 (str[i] < '0' || (str[i] > '9' && str[i] < 'A') ||
26
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 476 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 478 times.
478 (str[i] > 'Z' && str[i] < 'a') || str[i] > 'z'))
27 1 return 0;
28 }
29 119 return 1;
30 }
31
32 /**
33 * @brief Display an error message if the variable name is invalid
34 * @param var The variable name
35 * @return <b>int</b> <u>1</u> if the variable name is invalid,
36 * <u>0</u> otherwise
37 */
38 125 static int error_handling(char *var)
39 {
40
3/4
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 120 times.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
125 if (my_char_is_alpha(var[0]) == 0 && var[0] != '_') {
41 5 my_fprintf(2, "setenv: Variable name must begin with a letter.\n");
42 5 return 1;
43 }
44
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 119 times.
120 if (my_str_is_valid(var) == 0) {
45 1 my_fprintf(2, "setenv: "
46 "Variable name must contain alphanumeric characters.\n");
47 1 return 1;
48 }
49 119 return 0;
50 }
51
52 /**
53 * @brief Set a new environment variable
54 * @param mysh The shell structure
55 * @param var The variable name
56 * @param value The variable value
57 * @return <b>int</b> 0 if the command succeed, 1 otherwise
58 */
59 125 int set_new_env_var(mysh_t *mysh, char *var, char *value)
60 {
61 125 char **new_env = MALLOC(sizeof(char *) *
62 (my_array_len((void **)mysh->env) + 2));
63 125 int index = 0;
64
65
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 119 times.
125 if (error_handling(var))
66 6 return 1;
67
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 119 times.
123 for (; mysh->env[index] != NULL; index++)
68 4 new_env[index] = mysh->env[index];
69 119 new_env[index] = CALLOC((my_strlen(var) +
70 my_strlen(value) + 4), sizeof(char));
71 119 new_env[index] = my_strcat(new_env[index], var);
72 119 new_env[index] = my_strcat(new_env[index], "=");
73
1/2
✓ Branch 0 taken 119 times.
✗ Branch 1 not taken.
119 if (value != NULL)
74 119 new_env[index] = my_strcat(new_env[index], value);
75 119 new_env[index + 1] = NULL;
76 119 mysh->env = new_env;
77 119 update_path_list(mysh);
78 119 return 0;
79 }
80
81 /**
82 * @brief The setenv builtin
83 * @param mysh The shell structure
84 * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise
85 */
86 13 int exec_setenv(mysh_t *mysh)
87 {
88
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
13 if (mysh->args[1] == NULL)
89 4 return exec_env(mysh);
90
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
9 if (mysh->args[2] != NULL && mysh->args[3] != NULL) {
91 my_fprintf(2, "setenv: Too many arguments.\n");
92 return 1;
93 }
94
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 7 times.
31 for (int index = 0; mysh->env[index] != NULL; index++) {
95
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 22 times.
24 if (my_strncmp(mysh->env[index], mysh->args[1],
96 24 my_strlen(mysh->args[1])) == 0) {
97 2 mysh->env[index] = CALLOC((my_strlen(mysh->args[1]) +
98 my_strlen(mysh->args[2]) + 4), sizeof(char));
99 2 my_strcat(mysh->env[index], mysh->args[1]);
100 2 my_strcat(mysh->env[index], "=\0");
101 2 my_strcat(mysh->env[index], mysh->args[2]);
102 2 update_path_list(mysh);
103 2 return 0;
104 }
105 }
106 7 return set_new_env_var(mysh, mysh->args[1], mysh->args[2]);
107 }
108