」 feat: command return code msg + env norm

This commit is contained in:
y-syo
2024-06-24 12:57:03 +02:00
parent 667beff259
commit 831d046fcb
3 changed files with 52 additions and 35 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 20:25:06 by adjoly #+# #+# */ /* Created: 2024/05/20 20:25:06 by adjoly #+# #+# */
/* Updated: 2024/05/30 12:53:09 by adjoly ### ########.fr */ /* Updated: 2024/06/24 10:54:36 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,4 +21,6 @@
# define ERROR_NO_REDIR ": need redirection file" # define ERROR_NO_REDIR ": need redirection file"
# define ERROR_NO_EOF ": need delimiter to heredoc" # define ERROR_NO_EOF ": need delimiter to heredoc"
# define ERROR_COREDUMP "(core dumped)"
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/22 14:42:52 by mmoussou #+# #+# */ /* Created: 2024/05/22 14:42:52 by mmoussou #+# #+# */
/* Updated: 2024/05/27 18:05:18 by mmoussou ### ########.fr */ /* Updated: 2024/06/24 12:53:38 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -64,12 +64,9 @@ int env_init(char **env_d, t_env *env)
while (env_d[++i]) while (env_d[++i])
{ {
if (bool_first_el) if (bool_first_el)
{ if (env_create_first_el(env_d[i], env))
i = env_create_first_el(env_d[i], env);
if (i < 0)
return (1); return (1);
} if (!bool_first_el)
else
{ {
new = env_create_el(env_d[i]); new = env_create_el(env_d[i]);
if (!new) if (!new)

View File

@ -6,11 +6,12 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */ /* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */
/* Updated: 2024/06/23 03:47:39 by mmoussou ### ########.fr */ /* Updated: 2024/06/24 12:55:13 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#include "error_msg.h"
char *get_cmd_local_path(char *cmd, t_env *env) char *get_cmd_local_path(char *cmd, t_env *env)
{ {
@ -53,21 +54,45 @@ int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2])
status = dup2(cmd->infile, STDIN_FILENO); status = dup2(cmd->infile, STDIN_FILENO);
if (cmd->infile != STDIN_FILENO) if (cmd->infile != STDIN_FILENO)
close(cmd->infile); close(cmd->infile);
if (status == -1) status += dup2(cmd->outfile, STDOUT_FILENO);
exit(-1);
status = dup2(cmd->outfile, STDOUT_FILENO);
if (cmd->outfile != STDOUT_FILENO) if (cmd->outfile != STDOUT_FILENO)
close(cmd->outfile); close(cmd->outfile);
if (status == -1)
exit(-1);
close(pipe_fd[0]); close(pipe_fd[0]);
close(pipe_fd[1]); close(pipe_fd[1]);
if (status)
exit(-1);
execve(cmd->cmd, cmd->argv, env); execve(cmd->cmd, cmd->argv, env);
exit(-1); exit(-1);
} }
return (0); return (0);
} }
void print_return_value(int return_code)
{
int code;
static const char *sigmsg[] = {0, "Hangup", 0, "Quit", "Illegal \
instruction", "Trace/breakpoint trap", "Aborted", "Bus error",
"Floating point exception", "Killed", "User defined signal 1",
"Segmentation fault", "User defined signal 2", 0,
"Alarm clock", "Terminated", "Stack fault", 0, 0, "Stopped", "Stopped",
"Stopped", "Stopped", 0, "CPU time limit exceeded",
"File size limit exceeded", "Virtual time expired",
"Profiling timer expired", "I/O possible", "Power failure",
"Bad system call"};
if (!WIFEXITED(return_code))
{
if (WIFSIGNALED(return_code))
{
code = WTERMSIG(return_code);
if (WCOREDUMP(return_code))
printf("minishell : %s %s\n", sigmsg[code], ERROR_COREDUMP);
else
printf("minishell : %s\n", sigmsg[code]);
}
}
}
int exec_split_cmd(t_list *list_cmd, t_env *env) int exec_split_cmd(t_list *list_cmd, t_env *env)
{ {
char **env_array; char **env_array;
@ -81,7 +106,7 @@ int exec_split_cmd(t_list *list_cmd, t_env *env)
pipe_fd[1] = -1; pipe_fd[1] = -1;
if (!env_array) if (!env_array)
return (-1); return (-1);
i = ft_lstsize(list_cmd); i = 1;
while (list_cmd->next) while (list_cmd->next)
{ {
status = pipe(pipe_fd); status = pipe(pipe_fd);
@ -96,11 +121,10 @@ int exec_split_cmd(t_list *list_cmd, t_env *env)
close(((t_cmd *)(list_cmd->content))->outfile); close(((t_cmd *)(list_cmd->content))->outfile);
if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO) if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO)
close(((t_cmd *)(list_cmd->content))->infile); close(((t_cmd *)(list_cmd->content))->infile);
if (status == -1) if (status)
{ printf("minishell : command not found: %s\n", ((t_cmd *)(list_cmd->content))->cmd);
ft_free("a", &env_array); else
return (-1); i++;
}
list_cmd = list_cmd->next; list_cmd = list_cmd->next;
} }
status = exec_single_cmd(list_cmd->content, env_array, env, pipe_fd); status = exec_single_cmd(list_cmd->content, env_array, env, pipe_fd);
@ -110,23 +134,17 @@ int exec_split_cmd(t_list *list_cmd, t_env *env)
close(((t_cmd *)(list_cmd->content))->infile); close(((t_cmd *)(list_cmd->content))->infile);
ft_free("a", &env_array); ft_free("a", &env_array);
if (status == -1) if (status == -1)
return (-1); {
printf("minishell : command not found: %s\n", ((t_cmd *)(list_cmd->content))->cmd);
i--;
}
if (i < 1)
return (0);
while (i) while (i)
{ {
waitpid(-1, &return_code, 0); waitpid(-1, &return_code, 0);
i--; i--;
} }
if (WIFEXITED(return_code)) print_return_value(return_code);
printf("minishell: %d\n", WEXITSTATUS(return_code));
else
{
if (WIFSIGNALED(return_code))
{
if (WCOREDUMP(return_code))
printf("minishell: %d\n", WTERMSIG(return_code));
else
printf("minishell: %d\n", WTERMSIG(return_code));
}
}
return (0); return (0);
} }