fix leak parent and add cmd to launch with good settings

This commit is contained in:
kinou-p 2022-04-16 16:18:20 +02:00
parent af92795001
commit d413fed41c
9 changed files with 79 additions and 46 deletions

View File

@ -6,7 +6,7 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ # # By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/03/06 12:50:24 by apommier #+# #+# # # Created: 2022/03/06 12:50:24 by apommier #+# #+# #
# Updated: 2022/04/16 02:25:09 by apommier ### ########.fr # # Updated: 2022/04/16 09:58:10 by apommier ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -40,6 +40,8 @@ LIB = -lreadline
RM = rm -rf RM = rm -rf
LIBFT = ./libft LIBFT = ./libft
#-include ./valgrind.mk
${NAME}: ${OBJS} ${NAME}: ${OBJS}
@make bonus -C ${LIBFT} @make bonus -C ${LIBFT}
@${CC} ${LIB} ${OBJS} ${LIBFT}/libft.a -o ${NAME} @${CC} ${LIB} ${OBJS} ${LIBFT}/libft.a -o ${NAME}
@ -58,4 +60,4 @@ re: fclean all
.PHONY: all clean fclean re .PHONY: all clean fclean re
-include ./valgrind.mk

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */ /* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
/* Updated: 2022/04/16 02:39:21 by apommier ### ########.fr */ /* Updated: 2022/04/16 16:10:54 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -50,6 +50,8 @@ typedef struct s_simple {
// Describes a complete command with the multiple pipes if any // Describes a complete command with the multiple pipes if any
// and input/output redirection if any. // and input/output redirection if any.
typedef struct s_command { typedef struct s_command {
int tmpin;
int tmpout;
char **env; char **env;
int nb_s_cmd; int nb_s_cmd;
struct s_simple **s_cmds; struct s_simple **s_cmds;

1
launch_minishell Executable file
View File

@ -0,0 +1 @@
valgrind --leak-check=full --show-leak-kinds=all --suppressions=ignoreliberror --track-fds=yes ./minishell

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */ /* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
/* Updated: 2022/04/16 04:08:28 by apommier ### ########.fr */ /* Updated: 2022/04/16 13:54:16 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -73,12 +73,13 @@ void print_prompt(char **path)
{ {
cmd = set_cmd(input, path, err_var); cmd = set_cmd(input, path, err_var);
//path = ft_dup_double(path); //path = ft_dup_double(path);
free_double(path);
if (cmd) if (cmd)
{ {
//cmd->err_var = 0; //cmd->err_var = 0;
execute(cmd, path); execute(cmd, cmd->env);
err_var = cmd->err_var; err_var = cmd->err_var;
free_double(path);
path = ft_dup_double(cmd->env); path = ft_dup_double(cmd->env);
//cmd = 0; //cmd = 0;
free_cmd(cmd); free_cmd(cmd);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/02 18:51:31 by apommier #+# #+# */ /* Created: 2022/04/02 18:51:31 by apommier #+# #+# */
/* Updated: 2022/04/16 04:34:00 by apommier ### ########.fr */ /* Updated: 2022/04/16 16:14:43 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -101,6 +101,13 @@ void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
cmd->err_var = 127; cmd->err_var = 127;
else else
cmd->err_var = 126; cmd->err_var = 126;
/*if (cmd->current_s_cmd->child)
{
if (cmd->current_s_cmd->infile)
close(cmd->current_s_cmd->infile);
if (cmd->current_s_cmd->outfile)
close(cmd->current_s_cmd->outfile);
}*/
} }
void execute(t_cmd *cmd, char **env) void execute(t_cmd *cmd, char **env)
@ -108,15 +115,19 @@ void execute(t_cmd *cmd, char **env)
int fdpipe[2]; int fdpipe[2];
int fdout; int fdout;
int fdin; int fdin;
int tmpin; int tmpin = -1;
int tmpout; int tmpout = -1;
int i; int i;
//printf("exec\n");
i = 0; i = 0;
fdpipe[1] = -1; fdpipe[1] = -1;
tmpin = dup(0); tmpin = dup(0);
tmpout = dup(1); tmpout = dup(1);
cmd->tmpin = tmpin;
cmd->tmpout = tmpout;
//printf("infile= %s\n", cmd->current_s_cmd->infile);
if (cmd->current_s_cmd->infile) if (cmd->current_s_cmd->infile)
{ {
fdin = open(cmd->current_s_cmd->infile, O_RDWR); fdin = open(cmd->current_s_cmd->infile, O_RDWR);
@ -124,13 +135,16 @@ void execute(t_cmd *cmd, char **env)
printf("Minishell: open : bad file descriptor\n"); printf("Minishell: open : bad file descriptor\n");
} }
else else
fdin=dup(tmpin); fdin = dup(tmpin);
while(cmd->current_s_cmd) while(cmd->current_s_cmd)
{ {
if (i > 0)
close(fdout);
cmd->current_s_cmd->child = 1; cmd->current_s_cmd->child = 1;
fdout = 0; fdout = -1;
if (i > 0 && cmd->current_s_cmd->infile) if (i > 0 && cmd->current_s_cmd->infile)
{ {
close(fdin);
fdin = open(cmd->current_s_cmd->infile, O_RDWR); fdin = open(cmd->current_s_cmd->infile, O_RDWR);
if (fdin < 0) if (fdin < 0)
printf("Minishell: open : bad file descriptor\n"); printf("Minishell: open : bad file descriptor\n");
@ -141,7 +155,7 @@ void execute(t_cmd *cmd, char **env)
if (cmd->current_s_cmd->outfile) if (cmd->current_s_cmd->outfile)
fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_APPEND, 0666); fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_APPEND, 0666);
else else
fdout=dup(tmpout); fdout = dup(tmpout);
cmd->current_s_cmd->fd[0] = fdin; cmd->current_s_cmd->fd[0] = fdin;
cmd->current_s_cmd->fd[1] = fdout; cmd->current_s_cmd->fd[1] = fdout;
if (i == 0 && is_builtin(cmd->current_s_cmd->cmd)) if (i == 0 && is_builtin(cmd->current_s_cmd->cmd))
@ -164,6 +178,7 @@ void execute(t_cmd *cmd, char **env)
fdin=fdpipe[0]; fdin=fdpipe[0];
exec_cmd(cmd, env, fdpipe); exec_cmd(cmd, env, fdpipe);
close(cmd->current_s_cmd->fd[0]); close(cmd->current_s_cmd->fd[0]);
close(fdin);
} }
if (fdpipe[1] > 0) if (fdpipe[1] > 0)
close(fdpipe[1]); close(fdpipe[1]);
@ -172,9 +187,13 @@ void execute(t_cmd *cmd, char **env)
//printf("cmd->err_var= %d\n", cmd->err_var); //printf("cmd->err_var= %d\n", cmd->err_var);
} }
close_pipe(cmd); close_pipe(cmd);
wait_exit(cmd);
dup2(tmpin, 0); dup2(tmpin, 0);
dup2(tmpout, 1);
close(tmpin); close(tmpin);
dup2(tmpout, 1);
close(tmpout); close(tmpout);
wait_exit(cmd);
//close(tmpin);
//tmpin = -1;
//close(tmpout);
//tmpout = -1;
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */ /* Created: 2022/03/09 12:33:30 by apommier #+# #+# */
/* Updated: 2022/04/16 04:43:03 by apommier ### ########.fr */ /* Updated: 2022/04/16 16:13:46 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,8 +17,19 @@ void exit_shell(t_cmd *cmd, int ret)
int i; int i;
i = 0; i = 0;
if (cmd) //if (cmd)
free_double(cmd->env); // free_double(cmd->env);
if (cmd->tmpin != -1)
{
dup2(cmd->tmpin, 0);
close(cmd->tmpin);
}
if (cmd->tmpout != -1)
{
dup2(cmd->tmpout, 1);
close(cmd->tmpout);
}
free_cmd(cmd);
clear_history(); clear_history();
//print_double_fd(cmd->s_cmds[0]->env, 1); //print_double_fd(cmd->s_cmds[0]->env, 1);
ft_putstr_fd("exit\n", 1); ft_putstr_fd("exit\n", 1);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */ /* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
/* Updated: 2022/04/16 04:33:32 by apommier ### ########.fr */ /* Updated: 2022/04/16 16:11:37 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -112,10 +112,12 @@ t_cmd *set_cmd(char *input, char **env, int nb)
cmd = malloc(sizeof(t_cmd)); cmd = malloc(sizeof(t_cmd));
if (!cmd) if (!cmd)
return (0); return (0);
cmd->s_cmds = calloc(sizeof(t_s_cmd), double_size(cmds) + 1); cmd->s_cmds = ft_calloc(sizeof(t_s_cmd), double_size(cmds) + 1);
if (!cmd->s_cmds) if (!cmd->s_cmds)
//free(cmd); //free(cmd);
return (0); return (0);
cmd->tmpin = -1;
cmd->tmpout = -1;
cmd->err_var = nb; cmd->err_var = nb;
cmd->path = get_path(env); cmd->path = get_path(env);
cmd->env = ft_dup_double(env); cmd->env = ft_dup_double(env);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/09 23:58:21 by apommier #+# #+# */ /* Created: 2022/04/09 23:58:21 by apommier #+# #+# */
/* Updated: 2022/04/16 04:43:06 by apommier ### ########.fr */ /* Updated: 2022/04/16 13:47:49 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -221,7 +221,7 @@ int parse_quote(t_cmd *cmd)
} }
//printf("parse quote -%s-\n", cmd->s_cmds[i]->args[0]); //printf("parse quote -%s-\n", cmd->s_cmds[i]->args[0]);
if (!is_builtin(cmd->s_cmds[i]->args[0])) if (!is_builtin(cmd->s_cmds[i]->args[0]))
cmd->s_cmds[i]->cmd = ft_strdup(get_command(cmd->s_cmds[i]->args, cmd->path)); cmd->s_cmds[i]->cmd = get_command(cmd->s_cmds[i]->args, cmd->path);
else else
cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->args[0]); cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->args[0]);
//free(cmd->s_cmds[i]->cmd); //free(cmd->s_cmds[i]->cmd);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */ /* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
/* Updated: 2022/04/16 04:33:24 by apommier ### ########.fr */ /* Updated: 2022/04/16 11:03:41 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,7 +25,9 @@ char *cut_str(char *str, int start, int end)
char *swap; char *swap;
char *del; char *del;
swap = 0;
del = str; del = str;
if (str[end])
swap = ft_strjoin(&str[end], 0); swap = ft_strjoin(&str[end], 0);
str[start] = 0; str[start] = 0;
str = ft_strjoin(str, swap); str = ft_strjoin(str, swap);
@ -53,12 +55,6 @@ char *get_word(char *str, int start)
return (new); return (new);
} }
void error_redirect(char *str)
{
printf("error : %s\n", str);
exit(1);
}
char *set_input(char *line, t_s_cmd *cmd, int index) char *set_input(char *line, t_s_cmd *cmd, int index)
{ {
int i; int i;
@ -74,24 +70,18 @@ char *set_input(char *line, t_s_cmd *cmd, int index)
i++; i++;
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i]) while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
i++; i++;
cmd->infile = get_word(line, word_index); cmd->infile = get_word(line, index);
cmd->infile = set_var(cmd->big_cmd, cmd->infile); cmd->infile = set_var(cmd->big_cmd, cmd->infile);
i = open(cmd->infile, O_RDONLY);
if (i == -1)
{
ft_putstr_fd("Minishell: ", 2);
ft_putstr_fd(cmd->infile, 2);
ft_putstr_fd(": no such file\n", 2);
return (0);
}
if (access(cmd->infile, R_OK)) if (access(cmd->infile, R_OK))
{ {
ft_putstr_fd("Minishell: ", 2); ft_putstr_fd("Minishell: ", 2);
ft_putstr_fd(cmd->infile, 2); ft_putstr_fd(cmd->infile, 2);
if (access(cmd->infile, F_OK))
ft_putstr_fd(": no such file\n", 2);
else
ft_putstr_fd(": Permission denied\n", 2); ft_putstr_fd(": Permission denied\n", 2);
return (0); return (0);
} }
close(i);
line = cut_str(line, index, i); line = cut_str(line, index, i);
return (line); return (line);
} }
@ -134,9 +124,11 @@ char *ft_input(char *line, t_s_cmd *cmd, int index)
cmd->in_type = 0; cmd->in_type = 0;
if (next == '<' || next == '>' || !next) if (next == '<' || next == '>' || !next)
return (0); return (0);
//printf("line bf set_input -%s-", line);
line = set_input(line, cmd, i); line = set_input(line, cmd, i);
if (!line) if (!line)
return (0); return (0);
//printf("line -%s-\n", line);
return (line); return (line);
} }
@ -157,6 +149,7 @@ char *ft_output(char *line, t_s_cmd *cmd, int index)
if (next == '<' || next == '>' || !next) if (next == '<' || next == '>' || !next)
return (0); return (0);
line = set_output(line, cmd, i); line = set_output(line, cmd, i);
//printf("line -%s-\n", line);
return (line); return (line);
} }
@ -242,6 +235,7 @@ void sig_heredoc(int num)
base.sa_handler = &crtl_c; base.sa_handler = &crtl_c;
base.sa_flags = 0; base.sa_flags = 0;
close(1);
//printf("sig_heredoc\n"); //printf("sig_heredoc\n");
if (sigaction(SIGINT, &base, 0) == -1) if (sigaction(SIGINT, &base, 0) == -1)
{ {
@ -306,11 +300,12 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index)
{ {
int i; int i;
i = 0; i= 0;
// printf("enter redirection\n"); // printf("enter redirection\n");
while (line[i]) while (line[i])
{ {
//printf("line[i] i= %d\n", i); //printf("line[i] i= %d\n", i);
//printf("-%s-\n", line);
if(line[i] == '<') if(line[i] == '<')
{ {
if (!is_in_quote(line, i)) if (!is_in_quote(line, i))
@ -337,9 +332,9 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index)
i = 0; i = 0;
} }
} }
if ((line[i] == '<' || line[i] == '>') && is_in_quote(line, i)) if (line[i] && (line[i] == '<' || line[i] == '>') && is_in_quote(line, i))
i++; i++;
else if (line[i] != '<' && line[i] != '>') else if (line[i] && line[i] != '<' && line[i] != '>')
i++; i++;
} }
return(line); return(line);