「✨」 feat: command return code msg + env norm

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

No files matched your search

+6 -4
View File
@@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
@@ -17,8 +17,10 @@
* Here we define all the error message
*/
#define ERROR_SYNTAX ": syntax error"
#define ERROR_NO_REDIR ": need redirection file"
#define ERROR_NO_EOF ": need delimiter to heredoc"
# define ERROR_SYNTAX ": syntax error"
# define ERROR_NO_REDIR ": need redirection file"
# define ERROR_NO_EOF ": need delimiter to heredoc"
# define ERROR_COREDUMP "(core dumped)"
#endif
+3 -6
View File
@@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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])
{
if (bool_first_el)
{
i = env_create_first_el(env_d[i], env);
if (i < 0)
if (env_create_first_el(env_d[i], env))
return (1);
}
else
if (!bool_first_el)
{
new = env_create_el(env_d[i]);
if (!new)
+43 -25
View File
@@ -6,11 +6,12 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 "error_msg.h"
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);
if (cmd->infile != STDIN_FILENO)
close(cmd->infile);
if (status == -1)
exit(-1);
status = dup2(cmd->outfile, STDOUT_FILENO);
status += dup2(cmd->outfile, STDOUT_FILENO);
if (cmd->outfile != STDOUT_FILENO)
close(cmd->outfile);
if (status == -1)
exit(-1);
close(pipe_fd[0]);
close(pipe_fd[1]);
if (status)
exit(-1);
execve(cmd->cmd, cmd->argv, env);
exit(-1);
}
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)
{
char **env_array;
@@ -81,7 +106,7 @@ int exec_split_cmd(t_list *list_cmd, t_env *env)
pipe_fd[1] = -1;
if (!env_array)
return (-1);
i = ft_lstsize(list_cmd);
i = 1;
while (list_cmd->next)
{
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);
if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO)
close(((t_cmd *)(list_cmd->content))->infile);
if (status == -1)
{
ft_free("a", &env_array);
return (-1);
}
if (status)
printf("minishell : command not found: %s\n", ((t_cmd *)(list_cmd->content))->cmd);
else
i++;
list_cmd = list_cmd->next;
}
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);
ft_free("a", &env_array);
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)
{
waitpid(-1, &return_code, 0);
i--;
}
if (WIFEXITED(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));
}
}
print_return_value(return_code);
return (0);
}