maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
execute_child.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* execute_child.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: rocky <marvin@42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/11/06 16:28:49 by rocky #+# #+# */
9/* Updated: 2024/11/06 16:28:51 by rocky ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "builtins.h"
14#include "libft.h"
15#include <stdio.h>
16#include <sys/wait.h>
17#include "shell.h"
18#include "execute.h"
19#include "tokens.h"
20#include "pipe.h"
21#include "redirection.h"
22#include "exit_status.h"
23#include "signals.h"
24#include "exit_status.h"
25
26int execute(t_ms_data *data);
27static int new_process(t_ms_data *data);
28static void signal_handler(void);
29static void handle_exec_errors(char *exec_path, t_ms_data *data);
30
41{
42 size_t i;
43 char *builtin_commands[7];
44 int (*builtin_functions[7])(t_ms_data *);
45
46 builtin_commands[0] = "cd";
47 builtin_commands[1] = "echo";
48 builtin_commands[2] = "env";
49 builtin_commands[3] = "exit";
50 builtin_commands[4] = "export";
51 builtin_commands[5] = "pwd";
52 builtin_commands[6] = "unset";
53 builtin_functions[0] = &builtin_cd;
54 builtin_functions[1] = &builtin_echo;
55 builtin_functions[2] = &builtin_env;
56 builtin_functions[3] = &builtin_exit;
57 builtin_functions[4] = &builtin_export;
58 builtin_functions[5] = &builtin_pwd;
59 builtin_functions[6] = &builtin_unset;
60 i = -1;
61 while (++i < sizeof(builtin_commands) / sizeof(char *))
62 if (ft_strcmp(data->args[0], builtin_commands[i]) == 0)
63 return ((*builtin_functions[i])(data));
64 return (new_process(data));
65}
66
76static void child_process(t_ms_data *data)
77{
78 char *exec_path;
79
80 handle_std_io(&data->std_in, STDIN_FILENO);
81 handle_std_io(&data->std_out, STDOUT_FILENO);
82 close_fds(data->std_in, data->std_out);
84 if (ft_strncmp(data->args[0], "/", 1) == 0)
85 exec_path = ft_strdup(data->args[0]);
86 else
87 exec_path = ft_find_path(data->args[0], data->envp);
88 handle_exec_errors(exec_path, data);
89}
90
91static int new_process(t_ms_data *data)
92{
93 pid_t pid;
94 int exit;
95
96 exit = 0;
97 handle_std_io(&data->std_in, STDIN_FILENO);
98 handle_std_io(&data->std_out, STDOUT_FILENO);
99 pid = fork();
100 if (pid == -1)
101 ft_perror("fork");
102 if (pid == 0)
103 child_process(data);
104 close_fds(data->std_in, data->std_out);
105 waitpid(pid, &exit, 0);
106 if (WIFSIGNALED(exit) && WTERMSIG(exit) == SIGQUIT)
107 ft_printf("\n");
108 return (WEXITSTATUS(exit));
109}
110
111static void handle_exec_errors(char *exec_path, t_ms_data *data)
112{
113 if (exec_path == NULL)
114 {
115 ft_putstr_fd("maxishell: command not found\n", STDERR_FILENO);
116 exit(127);
117 }
118 if (!data->args || !data->args[0] || !data->envp)
119 {
120 ft_putstr_fd("maxishell: invalid \
121 arguments or environment\n", STDERR_FILENO);
122 exit(EXIT_FAILURE);
123 }
124 if (execve(exec_path, data->args, env_to_array(data->envp)) == -1)
125 {
126 ft_putstr_fd("maxishell: command not found\n", STDERR_FILENO);
127 exit(IS_DIRECTORY);
128 }
129}
130
131static void signal_handler(void)
132{
133 struct sigaction a;
134
135 ft_memset(&a, 0, sizeof(struct sigaction));
136 sigemptyset(&a.sa_mask);
137 a.sa_handler = SIG_DFL;
138 a.sa_flags = 0;
139 sigaction(SIGQUIT, &a, NULL);
140}
int builtin_exit(t_ms_data *data)
Definition exit.c:61
int builtin_pwd(t_ms_data *data)
Definition pwd.c:24
int builtin_unset(t_ms_data *data)
Definition unset.c:29
int builtin_cd(t_ms_data *data)
Definition cd.c:28
int builtin_echo(t_ms_data *data)
Definition echo.c:23
int builtin_env(t_ms_data *data)
Definition env.c:35
int builtin_export(t_ms_data *data)
Definition export.c:30
char ** env_to_array(t_env *envp)
Definition utils.c:23
char * ft_find_path(char *cmd, t_env *envp)
function find the full path of the executed command
Definition utils_1.c:32
void handle_std_io(int *std_io, int std_fileno)
Definition utils_0.c:50
void close_fds(int in, int out)
close two file descriptors
Definition utils_0.c:28
int ft_perror(char *str)
static void signal_handler(void)
static void handle_exec_errors(char *exec_path, t_ms_data *data)
static int new_process(t_ms_data *data)
static void child_process(t_ms_data *data)
executes a new process
int execute(t_ms_data *data)
execute distribution function
#define EXIT_FAILURE
Definition exit_status.h:17
#define IS_DIRECTORY
Definition exit_status.h:20
int ft_printf(const char *format,...)
Definition ft_printf.c:37
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
void * ft_memset(void *s, int c, size_t n)
Definition ft_memset.c:28
int ft_strncmp(const char *s1, const char *s2, size_t n)
Definition ft_strncmp.c:24
void ft_putstr_fd(char *s, int fd)
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
t_env * envp
Definition shell.h:24
int std_out
Definition shell.h:27
char ** args
Definition shell.h:23
int std_in
Definition shell.h:26