GCC Code Coverage Report


Directory: ./
File: src/builtins/exec.c
Date: 2024-05-07 14:54:03
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 453 static void do_child(char **args, char *path, char **env)
34 {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 453 times.
453 if (args == NULL)
36 exit(1);
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 453 times.
453 if (execve(path, args, env) == -1) {
38 my_fprintf(2, "%s: %s.", path, strerror(errno));
39 check_errno();
40 exit(errno);
41 }
42 453 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 445 static int execute(char *path, char **args, char **env)
53 {
54 445 int status = 0;
55 445 pid_t pid = fork();
56
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 898 times.
898 if (pid == -1) {
58 my_fprintf(2, "fork: %s.\n", strerror(errno));
59 return 1;
60 }
61
2/2
✓ Branch 0 taken 453 times.
✓ Branch 1 taken 445 times.
898 if (pid == 0)
62 453 do_child(args, path, env);
63 445 waitpid(pid, &status, 0);
64 445 status_handler(status);
65 445 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 536 int exec_command(mysh_t *mysh)
74 {
75 536 char *path = NULL;
76 536 char **tmp = NULL;
77
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 536 times.
536 if (mysh->args[0] == NULL)
79 return 1;
80 536 tmp = globbing(mysh->args);
81
2/4
✓ Branch 0 taken 536 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 536 times.
536 if (tmp == NULL || tmp[0] == NULL)
82 return 1;
83 536 mysh->args = my_malloc_strdup_word_array(tmp);
84 536 FREE(tmp);
85 536 path = check_command_exist(mysh, mysh->args[0]);
86
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 445 times.
536 if (path == NULL)
87 91 return 1;
88 445 return execute(path, mysh->args, mysh->env);
89 }
90