fix leak parent and add cmd to launch with good settings
This commit is contained in:
parent
af92795001
commit
d413fed41c
6
Makefile
6
Makefile
@ -6,7 +6,7 @@
|
||||
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# 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
|
||||
LIBFT = ./libft
|
||||
|
||||
#-include ./valgrind.mk
|
||||
|
||||
${NAME}: ${OBJS}
|
||||
@make bonus -C ${LIBFT}
|
||||
@${CC} ${LIB} ${OBJS} ${LIBFT}/libft.a -o ${NAME}
|
||||
@ -58,4 +60,4 @@ re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
|
||||
-include ./valgrind.mk
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
// and input/output redirection if any.
|
||||
typedef struct s_command {
|
||||
int tmpin;
|
||||
int tmpout;
|
||||
char **env;
|
||||
int nb_s_cmd;
|
||||
struct s_simple **s_cmds;
|
||||
|
||||
1
launch_minishell
Executable file
1
launch_minishell
Executable file
@ -0,0 +1 @@
|
||||
valgrind --leak-check=full --show-leak-kinds=all --suppressions=ignoreliberror --track-fds=yes ./minishell
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
//path = ft_dup_double(path);
|
||||
free_double(path);
|
||||
if (cmd)
|
||||
{
|
||||
//cmd->err_var = 0;
|
||||
execute(cmd, path);
|
||||
execute(cmd, cmd->env);
|
||||
err_var = cmd->err_var;
|
||||
free_double(path);
|
||||
|
||||
path = ft_dup_double(cmd->env);
|
||||
//cmd = 0;
|
||||
free_cmd(cmd);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
else
|
||||
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)
|
||||
@ -108,15 +115,19 @@ void execute(t_cmd *cmd, char **env)
|
||||
int fdpipe[2];
|
||||
int fdout;
|
||||
int fdin;
|
||||
int tmpin;
|
||||
int tmpout;
|
||||
int tmpin = -1;
|
||||
int tmpout = -1;
|
||||
int i;
|
||||
|
||||
//printf("exec\n");
|
||||
|
||||
|
||||
i = 0;
|
||||
fdpipe[1] = -1;
|
||||
tmpin = dup(0);
|
||||
tmpout = dup(1);
|
||||
cmd->tmpin = tmpin;
|
||||
cmd->tmpout = tmpout;
|
||||
//printf("infile= %s\n", cmd->current_s_cmd->infile);
|
||||
if (cmd->current_s_cmd->infile)
|
||||
{
|
||||
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");
|
||||
}
|
||||
else
|
||||
fdin=dup(tmpin);
|
||||
fdin = dup(tmpin);
|
||||
while(cmd->current_s_cmd)
|
||||
{
|
||||
if (i > 0)
|
||||
close(fdout);
|
||||
cmd->current_s_cmd->child = 1;
|
||||
fdout = 0;
|
||||
fdout = -1;
|
||||
if (i > 0 && cmd->current_s_cmd->infile)
|
||||
{
|
||||
close(fdin);
|
||||
fdin = open(cmd->current_s_cmd->infile, O_RDWR);
|
||||
if (fdin < 0)
|
||||
printf("Minishell: open : bad file descriptor\n");
|
||||
@ -141,7 +155,7 @@ void execute(t_cmd *cmd, char **env)
|
||||
if (cmd->current_s_cmd->outfile)
|
||||
fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_APPEND, 0666);
|
||||
else
|
||||
fdout=dup(tmpout);
|
||||
fdout = dup(tmpout);
|
||||
cmd->current_s_cmd->fd[0] = fdin;
|
||||
cmd->current_s_cmd->fd[1] = fdout;
|
||||
if (i == 0 && is_builtin(cmd->current_s_cmd->cmd))
|
||||
@ -164,6 +178,7 @@ void execute(t_cmd *cmd, char **env)
|
||||
fdin=fdpipe[0];
|
||||
exec_cmd(cmd, env, fdpipe);
|
||||
close(cmd->current_s_cmd->fd[0]);
|
||||
close(fdin);
|
||||
}
|
||||
if (fdpipe[1] > 0)
|
||||
close(fdpipe[1]);
|
||||
@ -172,9 +187,13 @@ void execute(t_cmd *cmd, char **env)
|
||||
//printf("cmd->err_var= %d\n", cmd->err_var);
|
||||
}
|
||||
close_pipe(cmd);
|
||||
wait_exit(cmd);
|
||||
dup2(tmpin, 0);
|
||||
dup2(tmpout, 1);
|
||||
close(tmpin);
|
||||
dup2(tmpout, 1);
|
||||
close(tmpout);
|
||||
wait_exit(cmd);
|
||||
//close(tmpin);
|
||||
//tmpin = -1;
|
||||
//close(tmpout);
|
||||
//tmpout = -1;
|
||||
}
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
||||
i = 0;
|
||||
if (cmd)
|
||||
free_double(cmd->env);
|
||||
//if (cmd)
|
||||
// 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();
|
||||
//print_double_fd(cmd->s_cmds[0]->env, 1);
|
||||
ft_putstr_fd("exit\n", 1);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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));
|
||||
if (!cmd)
|
||||
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)
|
||||
//free(cmd);
|
||||
return (0);
|
||||
cmd->tmpin = -1;
|
||||
cmd->tmpout = -1;
|
||||
cmd->err_var = nb;
|
||||
cmd->path = get_path(env);
|
||||
cmd->env = ft_dup_double(env);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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]);
|
||||
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
|
||||
cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->args[0]);
|
||||
//free(cmd->s_cmds[i]->cmd);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 *del;
|
||||
|
||||
swap = 0;
|
||||
del = str;
|
||||
if (str[end])
|
||||
swap = ft_strjoin(&str[end], 0);
|
||||
str[start] = 0;
|
||||
str = ft_strjoin(str, swap);
|
||||
@ -53,12 +55,6 @@ char *get_word(char *str, int start)
|
||||
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)
|
||||
{
|
||||
int i;
|
||||
@ -74,24 +70,18 @@ char *set_input(char *line, t_s_cmd *cmd, int index)
|
||||
i++;
|
||||
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||
i++;
|
||||
cmd->infile = get_word(line, word_index);
|
||||
cmd->infile = get_word(line, index);
|
||||
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))
|
||||
{
|
||||
ft_putstr_fd("Minishell: ", 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);
|
||||
return (0);
|
||||
}
|
||||
close(i);
|
||||
line = cut_str(line, index, i);
|
||||
return (line);
|
||||
}
|
||||
@ -134,9 +124,11 @@ char *ft_input(char *line, t_s_cmd *cmd, int index)
|
||||
cmd->in_type = 0;
|
||||
if (next == '<' || next == '>' || !next)
|
||||
return (0);
|
||||
//printf("line bf set_input -%s-", line);
|
||||
line = set_input(line, cmd, i);
|
||||
if (!line)
|
||||
return (0);
|
||||
//printf("line -%s-\n", line);
|
||||
return (line);
|
||||
}
|
||||
|
||||
@ -157,6 +149,7 @@ char *ft_output(char *line, t_s_cmd *cmd, int index)
|
||||
if (next == '<' || next == '>' || !next)
|
||||
return (0);
|
||||
line = set_output(line, cmd, i);
|
||||
//printf("line -%s-\n", line);
|
||||
return (line);
|
||||
}
|
||||
|
||||
@ -242,6 +235,7 @@ void sig_heredoc(int num)
|
||||
|
||||
base.sa_handler = &crtl_c;
|
||||
base.sa_flags = 0;
|
||||
close(1);
|
||||
//printf("sig_heredoc\n");
|
||||
if (sigaction(SIGINT, &base, 0) == -1)
|
||||
{
|
||||
@ -306,11 +300,12 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
i= 0;
|
||||
// printf("enter redirection\n");
|
||||
while (line[i])
|
||||
{
|
||||
//printf("line[i] i= %d\n", i);
|
||||
//printf("-%s-\n", line);
|
||||
if(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;
|
||||
}
|
||||
}
|
||||
if ((line[i] == '<' || line[i] == '>') && is_in_quote(line, i))
|
||||
if (line[i] && (line[i] == '<' || line[i] == '>') && is_in_quote(line, i))
|
||||
i++;
|
||||
else if (line[i] != '<' && line[i] != '>')
|
||||
else if (line[i] && line[i] != '<' && line[i] != '>')
|
||||
i++;
|
||||
}
|
||||
return(line);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user