fix 0 var and 1 leak

This commit is contained in:
kinou-p 2022-04-22 11:43:57 +02:00
parent 211c35dacf
commit cbd9442469
12 changed files with 36 additions and 31 deletions

View File

@ -3,10 +3,10 @@
# ::: :::::::: # # ::: :::::::: #
# Makefile :+: :+: :+: # # Makefile :+: :+: :+: #
# +:+ +:+ +:+ # # +:+ +:+ +:+ #
# By: sadjigui <sadjigui@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/20 17:39:51 by sadjigui ### ########.fr # # Updated: 2022/04/22 11:25:14 by apommier ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -42,13 +42,11 @@ SRCS = srcs/main.c\
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
CC = clang CC = clang
CFLAGS = -Wall -Wextra -g CFLAGS = -Wall -Wextra -Werror -g
LIB = -lreadline 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}

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/20 16:58:49 by apommier ### ########.fr */ /* Updated: 2022/04/22 11:43:15 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,6 +28,8 @@
# include <signal.h> # include <signal.h>
# include <dirent.h> # include <dirent.h>
int g_var;
// Command Data Structure // Command Data Structure
// Describes a simple command and arguments // Describes a simple command and arguments
@ -68,7 +70,7 @@ char **ft_dup_double(char **env);
void execute(t_cmd *cmd); void execute(t_cmd *cmd);
//set_cmd.c //set_cmd.c
char *error_parsing(void); char *error_parsing(char *to_free);
t_cmd *set_cmd(char *input, char **path, int nb); t_cmd *set_cmd(char *input, char **path, int nb);
//exec_utils.c //exec_utils.c

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/20 04:53:03 by apommier ### ########.fr */ /* Updated: 2022/04/22 11:43:15 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,6 +35,7 @@ char **ft_dup_double(char **env)
char **read_line(char **path, char *input, t_cmd *cmd, int *err_var) char **read_line(char **path, char *input, t_cmd *cmd, int *err_var)
{ {
(void)*err_var;
input = readline("\033[1;31m~$ \033[0m"); input = readline("\033[1;31m~$ \033[0m");
if (!input) if (!input)
{ {
@ -44,12 +45,12 @@ char **read_line(char **path, char *input, t_cmd *cmd, int *err_var)
add_history(input); add_history(input);
if (ft_strlen(input) && next_space(input, 0) && input && path) if (ft_strlen(input) && next_space(input, 0) && input && path)
{ {
cmd = set_cmd(input, path, *err_var); cmd = set_cmd(input, path, g_var);
if (cmd) if (cmd)
{ {
free_double(path); free_double(path);
execute(cmd); execute(cmd);
*err_var = cmd->err_var; g_var = cmd->err_var;
path = ft_dup_double(cmd->env); path = ft_dup_double(cmd->env);
free_cmd(cmd); free_cmd(cmd);
cmd = 0; cmd = 0;

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/19 15:01:32 by apommier ### ########.fr */ /* Updated: 2022/04/22 11:24:40 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,23 +18,20 @@ void exit_shell(t_cmd *cmd, int ret)
i = 0; i = 0;
ft_putstr_fd("exit\n", 1); ft_putstr_fd("exit\n", 1);
close (0);
close (1);
if (!cmd) if (!cmd)
exit(ret); exit(ret);
if (cmd->tmpin != -1) if (cmd->tmpin != -1)
{
dup2(cmd->tmpin, 0);
close(cmd->tmpin); close(cmd->tmpin);
}
if (cmd->tmpout != -1) if (cmd->tmpout != -1)
{
dup2(cmd->tmpout, 1);
close(cmd->tmpout); close(cmd->tmpout);
}
if (cmd->current_s_cmd->fd[0] != -1) if (cmd->current_s_cmd->fd[0] != -1)
close(cmd->current_s_cmd->fd[0]); close(cmd->current_s_cmd->fd[0]);
if (cmd->current_s_cmd->fd[1] != -1) if (cmd->current_s_cmd->fd[1] != -1)
close(cmd->current_s_cmd->fd[1]); close(cmd->current_s_cmd->fd[1]);
free_cmd(cmd); free_cmd(cmd);
rl_clear_history();
clear_history(); clear_history();
cmd = 0; cmd = 0;
exit(ret); exit(ret);

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/20 15:49:33 by apommier ### ########.fr */ /* Updated: 2022/04/22 11:41:45 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -111,7 +111,7 @@ t_cmd *set_cmd(char *input, char **env, int nb)
if (!is_quote_good(input) || !is_pipe_good(input)) if (!is_quote_good(input) || !is_pipe_good(input))
{ {
ft_putstr_fd("Minishell: error while parsing command\n", 2); error_parsing(0);
return (0); return (0);
} }
cmds = ft_split_with_quote(input, '|'); cmds = ft_split_with_quote(input, '|');

View File

@ -6,14 +6,17 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 16:53:13 by apommier #+# #+# */ /* Created: 2022/04/19 16:53:13 by apommier #+# #+# */
/* Updated: 2022/04/20 17:03:57 by apommier ### ########.fr */ /* Updated: 2022/04/22 11:43:15 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/minishell.h" #include "../../includes/minishell.h"
char *error_parsing(void) char *error_parsing(char *to_free)
{ {
g_var = 2;
if (to_free)
free(to_free);
ft_putstr_fd("Minishell: error while parsing command\n", 2); ft_putstr_fd("Minishell: error while parsing command\n", 2);
return (0); return (0);
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 15:02:01 by apommier #+# #+# */ /* Created: 2022/04/19 15:02:01 by apommier #+# #+# */
/* Updated: 2022/04/20 17:04:43 by apommier ### ########.fr */ /* Updated: 2022/04/22 10:41:04 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -69,6 +69,7 @@ char *change_var(t_cmd *big_cmd, char *cmd, int *index)
cmd[*index] = 0; cmd[*index] = 0;
ret = ft_strjoin(cmd, var); ret = ft_strjoin(cmd, var);
free(cmd); free(cmd);
if (*index > 0)
*index += ft_strlen(var) - 1; *index += ft_strlen(var) - 1;
free(var); free(var);
var = ret; var = ret;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 19:25:16 by apommier #+# #+# */ /* Created: 2022/04/19 19:25:16 by apommier #+# #+# */
/* Updated: 2022/04/20 04:55:14 by apommier ### ########.fr */ /* Updated: 2022/04/22 11:24:29 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 14:46:01 by apommier #+# #+# */ /* Created: 2022/04/19 14:46:01 by apommier #+# #+# */
/* Updated: 2022/04/19 19:36:08 by apommier ### ########.fr */ /* Updated: 2022/04/22 11:24:15 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 14:44:22 by apommier #+# #+# */ /* Created: 2022/04/19 14:44:22 by apommier #+# #+# */
/* Updated: 2022/04/20 16:50:37 by apommier ### ########.fr */ /* Updated: 2022/04/22 11:43:15 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,7 @@ int check_access_input(t_s_cmd *cmd)
{ {
if (access(cmd->infile, R_OK)) if (access(cmd->infile, R_OK))
{ {
g_var = 1;
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)) if (access(cmd->infile, F_OK))
@ -70,7 +71,7 @@ char *ft_input(char *line, t_s_cmd *cmd, int index)
else else
cmd->in_type = 0; cmd->in_type = 0;
if (next == '<' || next == '>' || !next) if (next == '<' || next == '>' || !next)
return (error_parsing()); return (error_parsing(line));
line = set_input(line, cmd, i); line = set_input(line, cmd, i);
if (!line) if (!line)
return (0); return (0);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 14:45:10 by apommier #+# #+# */ /* Created: 2022/04/19 14:45:10 by apommier #+# #+# */
/* Updated: 2022/04/20 17:05:24 by apommier ### ########.fr */ /* Updated: 2022/04/22 11:43:15 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,7 @@ int check_access_output(t_s_cmd *cmd)
{ {
if (access(cmd->outfile, W_OK)) if (access(cmd->outfile, W_OK))
{ {
g_var = 1;
ft_putstr_fd("Minishell: ", 2); ft_putstr_fd("Minishell: ", 2);
ft_putstr_fd(cmd->outfile, 2); ft_putstr_fd(cmd->outfile, 2);
if (access(cmd->outfile, F_OK)) if (access(cmd->outfile, F_OK))
@ -71,7 +72,7 @@ char *ft_output(char *line, t_s_cmd *cmd, int index)
else else
cmd->in_type = 0; cmd->in_type = 0;
if (next == '<' || next == '>' || !next) if (next == '<' || next == '>' || !next)
return (0); return (error_parsing(line));
line = set_output(line, cmd, i); line = set_output(line, cmd, i);
return (line); return (line);
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 15:30:30 by apommier #+# #+# */ /* Created: 2022/04/19 15:30:30 by apommier #+# #+# */
/* Updated: 2022/04/19 16:58:02 by apommier ### ########.fr */ /* Updated: 2022/04/22 11:43:15 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,7 @@
void crtl_c(int num) void crtl_c(int num)
{ {
num = 0; num = 0;
g_var = 130;
printf("\n"); printf("\n");
rl_replace_line("", 0); rl_replace_line("", 0);
rl_on_new_line(); rl_on_new_line();