add: exitcode

This commit is contained in:
Camille Chauvet 2023-02-21 22:12:23 +01:00
parent 03a2cc055c
commit 3fb6eb65a8
7 changed files with 29 additions and 31 deletions

BIN
.execution.c.swp Normal file

Binary file not shown.

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 13:09:08 by erey-bet #+# #+# */ /* Created: 2023/02/17 13:09:08 by erey-bet #+# #+# */
/* Updated: 2023/02/21 16:02:09 by erey-bet ### ########.fr */ /* Updated: 2023/02/21 21:45:38 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -88,9 +88,3 @@ int echo(int fd, char **strs)
write(fd, "\n", 1); write(fd, "\n", 1);
return (0); return (0);
} }
int main(int argc, char *argv[])
{
echo(1, argv);
return (0);
}

6
cmd.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */ /* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
/* Updated: 2023/02/20 14:47:29 by starnakin ### ########.fr */ /* Updated: 2023/02/21 21:54:08 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,6 +22,10 @@ void ft_cmddel(void *ptr)
ft_freer_tab_ultimate(1, content->args); ft_freer_tab_ultimate(1, content->args);
if (content->executable != NULL) if (content->executable != NULL)
free(content->executable); free(content->executable);
if (content->fd_in > 2)
close(content->fd_in);
if (content->fd_out > 2)
close(content->fd_in);
free(content); free(content);
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */ /* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
/* Updated: 2023/02/21 15:45:24 by cchauvet ### ########.fr */ /* Updated: 2023/02/21 22:11:34 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -119,7 +119,7 @@ static int ft_own_cmd(t_list **env, t_cmd *cmd)
return (return_code); return (return_code);
} }
int ft_cmds_executor(t_list **cmds, t_list **env) int ft_cmds_executor(t_data *data, t_list **cmds)
{ {
t_cmd *content; t_cmd *content;
t_list *current; t_list *current;
@ -141,25 +141,24 @@ int ft_cmds_executor(t_list **cmds, t_list **env)
content->fd_out = fds[1]; content->fd_out = fds[1];
((t_cmd *) current->next->content)->fd_in = fds[0]; ((t_cmd *) current->next->content)->fd_in = fds[0];
} }
cmd_return = ft_own_cmd(env, content); cmd_return = ft_own_cmd(data->env, content);
if (cmd_return == -1) if (cmd_return == -1)
{ {
content->executable = ft_get_executable_path( content->executable = ft_get_executable_path(
content->executable, env); content->executable, data->env);
if (content->executable != NULL) if (content->executable != NULL)
cmd_return = ft_excutor(content, env); cmd_return = ft_excutor(content, data->env);
} }
if (content->fd_in != 0) else if (cmd_return == -2)
close(content->fd_in); return (-1);
if (content->fd_out != 1 && content->fd_out != 2) data->exit_code = cmd_return;
close(content->fd_out);
return_value = ft_itoa(cmd_return); return_value = ft_itoa(cmd_return);
if (return_value == NULL) if (return_value == NULL)
{ {
ft_eprintf("minishell: malloc failed\n"); ft_eprintf("minishell: malloc failed\n");
return (1); return (1);
} }
set_value_by_key("?", return_value, env); set_value_by_key("?", return_value, data->env);
current = current->next; current = current->next;
} }
return (0); return (0);

11
main.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */ /* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
/* Updated: 2023/02/21 14:48:07 by cchauvet ### ########.fr */ /* Updated: 2023/02/21 22:05:04 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,7 +35,7 @@ static char *ft_get_user_input(t_list **env)
return (line); return (line);
} }
static int ft_minishell(t_list **env, char *line) static int ft_minishell(t_data *data, char *line)
{ {
t_list **cmds; t_list **cmds;
int code; int code;
@ -64,7 +64,7 @@ static int ft_minishell(t_list **env, char *line)
free(line_clean); free(line_clean);
return (1); return (1);
} }
cmds = ft_parse_cmds(line_clean, env, infile, outfile); cmds = ft_parse_cmds(line_clean, data->env, infile, outfile);
if (cmds == NULL) if (cmds == NULL)
{ {
close(outfile); close(outfile);
@ -74,7 +74,7 @@ static int ft_minishell(t_list **env, char *line)
free(line_clean); free(line_clean);
return (1); return (1);
} }
code = ft_cmds_executor(cmds, env); code = ft_cmds_executor(data, cmds);
ft_lstclear(cmds, ft_cmddel); ft_lstclear(cmds, ft_cmddel);
free(cmds); free(cmds);
free(line_clean); free(line_clean);
@ -129,7 +129,7 @@ int main(int ac, char **av, char **env)
} }
while (line != NULL) while (line != NULL)
{ {
if (ft_minishell(data.env, line) == 2) if (ft_minishell(&data, line) == -1)
break ; break ;
free(line); free(line);
line = ft_get_user_input(data.env); line = ft_get_user_input(data.env);
@ -142,6 +142,7 @@ int main(int ac, char **av, char **env)
} }
ft_lstclear(data.env, env_del); ft_lstclear(data.env, env_del);
free(data.env); free(data.env);
return (data.exit_code);
} }
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */ /* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
/* Updated: 2023/02/21 15:43:50 by erey-bet ### ########.fr */ /* Updated: 2023/02/21 22:03:41 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,6 +26,12 @@
# include <readline/history.h> # include <readline/history.h>
# define DEBUG 0 # define DEBUG 0
typedef struct s_data
{
t_list **env;
int exit_code;
} t_data;
int ft_syntatic_verif(const char *str); int ft_syntatic_verif(const char *str);
int ft_file_is_readable(const char *path); int ft_file_is_readable(const char *path);
int ft_file_is_writable(const char *path); int ft_file_is_writable(const char *path);
@ -35,7 +41,7 @@ int ft_infile(char *line);
int ft_outfile(char *line); int ft_outfile(char *line);
int ft_heredoc(char *stop); int ft_heredoc(char *stop);
size_t ft_seglen_quoted(const char *str, char c); size_t ft_seglen_quoted(const char *str, char c);
int ft_cmds_executor(t_list **cmds, t_list **env); int ft_cmds_executor(t_data *data, t_list **cmds);
char **ft_split_quoted(const char *s, char c); char **ft_split_quoted(const char *s, char c);
void ft_cmddel(void *content); void ft_cmddel(void *content);
void env_del(void *content); void env_del(void *content);
@ -82,12 +88,6 @@ typedef struct s_cmd
char **args; char **args;
} t_cmd; } t_cmd;
typedef struct s_data
{
t_list **env;
int heredoc;
} t_data;
typedef struct s_env typedef struct s_env
{ {
void *key; void *key;