GCC Code Coverage Report


Directory: ./
File: src/builtins/exec.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 25 38 65.8%
Functions: 3 4 75.0%
Branches: 10 18 55.6%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the exec functions
6 */
7 /**
8 * @file exec.c
9 * @brief The file containing the exec functions
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief Check if errno is a ENOEXEC error
16 * @return <b>void</b>
17 */
18 static void check_errno(void)
19 {
20 if (errno == ENOEXEC) {
21 my_putstr_error(" Wrong Architecture.\n");
22 } else {
23 my_putstr_error("\n");
24 }
25 }
26
27 /**
28 * @brief Execute the command in the child process
29 * @param args The arguments of the command
30 * @param path The path of the command
31 * @param env The environment
32 */
33 388 static void do_child(char **args, char *path, char **env)
34 {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 388 times.
388 if (args == NULL)
36 exit(1);
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 388 times.
388 if (execve(path, args, env) == -1) {
38 my_fprintf(2, "%s: %s.", path, strerror(errno));
39 check_errno();
40 exit(errno);
41 }
42 388 exit(0);
43 }
44
45 /**
46 * @brief Execute the command
47 * @param path The path of the command
48 * @param args The arguments of the command
49 * @param env The env
50 * @return <b>int</b> The status of the command
51 */
52 376 static int execute(char *path, char **args, char **env)
53 {
54 376 int status = 0;
55 376 pid_t pid = fork();
56
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 764 times.
764 if (pid == -1) {
58 my_fprintf(2, "fork: %s.\n", strerror(errno));
59 return 1;
60 }
61
2/2
✓ Branch 0 taken 388 times.
✓ Branch 1 taken 376 times.
764 if (pid == 0)
62 388 do_child(args, path, env);
63 376 waitpid(pid, &status, 0);
64 376 status_handler(status);
65 376 return WEXITSTATUS(status);
66 }
67
68 /**
69 * @brief Execute the command
70 * @param mysh The shell structure
71 * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise
72 */
73 460 int exec_command(mysh_t *mysh)
74 {
75 460 char *path = NULL;
76 460 char **tmp = NULL;
77
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 460 times.
460 if (mysh->args[0] == NULL)
79 return 1;
80 460 tmp = globbing(mysh->args);
81
2/4
✓ Branch 0 taken 460 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 460 times.
460 if (tmp == NULL || tmp[0] == NULL)
82 return 1;
83 460 mysh->args = my_malloc_strdup_word_array(tmp);
84 460 FREE(tmp);
85 460 path = check_command_exist(mysh, mysh->args[0]);
86
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 376 times.
460 if (path == NULL)
87 84 return 1;
88 376 return execute(path, mysh->args, mysh->env);
89 }
90