maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
execute_child.c File Reference
#include "builtins.h"
#include "libft.h"
#include <stdio.h>
#include <sys/wait.h>
#include "shell.h"
#include "execute.h"
#include "tokens.h"
#include "pipe.h"
#include "redirection.h"
#include "exit_status.h"
#include "signals.h"
Include dependency graph for execute_child.c:

Go to the source code of this file.

Functions

int execute (t_ms_data *data)
 execute distribution function
 
static int new_process (t_ms_data *data)
 
static void signal_handler (void)
 
static void handle_exec_errors (char *exec_path, t_ms_data *data)
 
static void child_process (t_ms_data *data)
 executes a new process
 

Function Documentation

◆ child_process()

static void child_process ( t_ms_data data)
static

executes a new process

--

  • Parameters
    dataminishell data structure
  • Returns
    int return status:
  • - 0: SUCCESS
  • - 1: ERROR

Definition at line 76 of file execute_child.c.

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}
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
static void signal_handler(void)
static void handle_exec_errors(char *exec_path, t_ms_data *data)
int ft_strncmp(const char *s1, const char *s2, size_t n)
Definition ft_strncmp.c:24
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

References s_ms_data::args, close_fds(), s_ms_data::envp, ft_find_path(), ft_strdup(), ft_strncmp(), handle_exec_errors(), handle_std_io(), signal_handler(), s_ms_data::std_in, and s_ms_data::std_out.

Referenced by new_process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ execute()

int execute ( t_ms_data data)

execute distribution function

--

  • Parameters
    dataminishell data struct
  • Returns
    int return status:
  • - 0: success
  • - 1: error

Definition at line 40 of file execute_child.c.

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}
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
static int new_process(t_ms_data *data)
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24

References s_ms_data::args, builtin_cd(), builtin_echo(), builtin_env(), builtin_exit(), builtin_export(), builtin_pwd(), builtin_unset(), ft_strcmp(), and new_process().

Referenced by execute_ast().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_exec_errors()

static void handle_exec_errors ( char *  exec_path,
t_ms_data data 
)
static

Definition at line 111 of file execute_child.c.

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}
char ** env_to_array(t_env *envp)
Definition utils.c:23
#define EXIT_FAILURE
Definition exit_status.h:17
#define IS_DIRECTORY
Definition exit_status.h:20
void ft_putstr_fd(char *s, int fd)

References s_ms_data::args, env_to_array(), s_ms_data::envp, EXIT_FAILURE, ft_putstr_fd(), and IS_DIRECTORY.

Referenced by child_process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ new_process()

static int new_process ( t_ms_data data)
static

Definition at line 91 of file execute_child.c.

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}
int ft_perror(char *str)
static void child_process(t_ms_data *data)
executes a new process
int ft_printf(const char *format,...)
Definition ft_printf.c:37

References child_process(), close_fds(), ft_perror(), ft_printf(), handle_std_io(), s_ms_data::std_in, and s_ms_data::std_out.

Referenced by execute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ signal_handler()

static void signal_handler ( void  )
static

Definition at line 131 of file execute_child.c.

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}
void * ft_memset(void *s, int c, size_t n)
Definition ft_memset.c:28

References ft_memset().

Referenced by child_process().

Here is the call graph for this function:
Here is the caller graph for this function: