fixed leaks and some redirection errors

This commit is contained in:
mdev9
2024-04-22 19:19:55 +02:00
parent 3cfeeb348a
commit 642a9ab232
6 changed files with 31 additions and 32 deletions

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */ /* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
/* Updated: 2024/04/22 16:43:18 by tomoron ### ########.fr */ /* Updated: 2024/04/22 19:18:52 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,12 +26,12 @@ void get_redirections(t_msh *msh, t_cmd *cmds)
if (!get_out_type(msh, cmds)) if (!get_out_type(msh, cmds))
get_in_type(msh, cmds); get_in_type(msh, cmds);
} }
/*
printf("in_type:"); printf("in_type:");
print_cmd_type(msh->in_type, 0); print_cmd_type(msh->in_type, 0);
printf("\nout_type:"); printf("\nout_type:");
print_cmd_type(msh->out_type, 0); print_cmd_type(msh->out_type, 0);
printf("\n"); printf("\n");*/
} }
t_cmd *get_next_command(t_cmd *cmd) t_cmd *get_next_command(t_cmd *cmd)
@ -93,6 +93,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
if (pipe(msh->fds[i]) == -1) if (pipe(msh->fds[i]) == -1)
{ {
perror("minishell: pipe"); perror("minishell: pipe");
free(cmd_args);
ft_exit(msh, 1); ft_exit(msh, 1);
} }
//fprintf(stderr, "pipe: msh->fds[%d][0]: %d, msh->fds[%d][1]: %d\n", i, msh->fds[i][0], i, msh->fds[i][1]); //fprintf(stderr, "pipe: msh->fds[%d][0]: %d, msh->fds[%d][1]: %d\n", i, msh->fds[i][0], i, msh->fds[i][1]);
@ -101,6 +102,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
if (pid == -1) if (pid == -1)
{ {
perror("minishell: fork"); perror("minishell: fork");
free(cmd_args);
ft_exit(msh, 1); ft_exit(msh, 1);
} }
if (pid == 0) if (pid == 0)
@ -132,10 +134,10 @@ int get_cmd_count(t_cmd *cmds)
int nb; int nb;
nb = 0; nb = 0;
while(cmds && (is_output_type(cmds) || is_input_type(cmds)))
cmds=cmds->next;
while (cmds && !is_operand_type(cmds)) while (cmds && !is_operand_type(cmds))
{ {
while(cmds && (is_output_type(cmds) || is_input_type(cmds)))
cmds=cmds->next;
if (is_cmd_type(cmds)) if (is_cmd_type(cmds))
nb++; nb++;
while(cmds && (is_output_type(cmds) || is_input_type(cmds) || is_cmd_type(cmds))) while(cmds && (is_output_type(cmds) || is_input_type(cmds) || is_cmd_type(cmds)))

View File

@ -6,18 +6,21 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */ /* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */
/* Updated: 2024/04/22 17:02:13 by tomoron ### ########.fr */ /* Updated: 2024/04/22 19:12:50 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void redirect_input(t_msh *msh, int i) void redirect_input(t_msh *msh, int i, char **cmd_args)
{ {
if (msh->in_type != PIPE) if (msh->in_type != PIPE)
{ {
if (dup2(msh->in_fd, 0) < 0) if (dup2(msh->in_fd, 0) < 0)
{
free(cmd_args);
ft_exit(msh, 1); ft_exit(msh, 1);
}
close(msh->in_fd); close(msh->in_fd);
} }
else else
@ -25,6 +28,7 @@ void redirect_input(t_msh *msh, int i)
if (dup2(msh->fds[i - 1][0], 0) < 0) if (dup2(msh->fds[i - 1][0], 0) < 0)
{ {
perror("dup2"); //debug perror("dup2"); //debug
free(cmd_args);
ft_exit(msh, 1); ft_exit(msh, 1);
} }
} }

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */ /* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
/* Updated: 2024/04/22 18:39:39 by tomoron ### ########.fr */ /* Updated: 2024/04/22 18:41:29 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,8 +20,6 @@ char *get_prompt(t_env *env)
char cwd_buffer[100]; char cwd_buffer[100];
char *cwd; char *cwd;
if(!isatty(0))
return(ft_strdup(""));
res = ft_strjoin_free("\001", ft_get_color(10, 255, 80), 2); res = ft_strjoin_free("\001", ft_get_color(10, 255, 80), 2);
res = ft_strjoin_free(res, "\033[1m\002", 1); res = ft_strjoin_free(res, "\033[1m\002", 1);
if (getenv("USER")) if (getenv("USER"))

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
/* Updated: 2024/04/22 15:25:22 by marde-vr ### ########.fr */ /* Updated: 2024/04/22 19:13:12 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -107,14 +107,14 @@ int file_access(t_msh *msh, int *found);
void remove_command_from_msh(t_msh *msh); void remove_command_from_msh(t_msh *msh);
void ft_exit(t_msh *msh, int error_code); void ft_exit(t_msh *msh, int error_code);
void sort_wildcards_token(t_token *list); void sort_wildcards_token(t_token *list);
void redirect_input(t_msh *msh, int i, char **cmd_args);
void redirect_output(t_msh *msh, int i, char **cmd_args);
void print_cmd_type(t_cmd_type type, char *value); void print_cmd_type(t_cmd_type type, char *value);
int cmd_is_forkable_builtin(char *cmd_token); int cmd_is_forkable_builtin(char *cmd_token);
void signal_handler_command(int signum); void signal_handler_command(int signum);
void ft_exit(t_msh *msh, int exit_code); void ft_exit(t_msh *msh, int exit_code);
void redirect_output(t_msh *msh, int i);
char **split_paths_from_env(t_env *env); char **split_paths_from_env(t_env *env);
int add_return_code_to_str(char *res); int add_return_code_to_str(char *res);
void redirect_input(t_msh *msh, int i);
void parse_var(t_msh *msh, char *line); void parse_var(t_msh *msh, char *line);
void print_parsed_token(t_token *cmd);//debug void print_parsed_token(t_token *cmd);//debug
int get_var_name_len(char *command); int get_var_name_len(char *command);

View File

@ -6,30 +6,30 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/19 14:09:44 by tomoron #+# #+# */ /* Created: 2024/04/19 14:09:44 by tomoron #+# #+# */
/* Updated: 2024/04/19 14:10:30 by tomoron ### ########.fr */ /* Updated: 2024/04/22 19:12:32 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#include <stdio.h>
void redirect_output(t_msh *msh, int i) void redirect_output(t_msh *msh, int i, char **cmd_args)
{ {
if (msh->out_type != PIPE) if (msh->out_type != PIPE)
{ {
//fprintf(stderr, "redirecting output\n");
if (dup2(msh->out_fd, 1) < 0) if (dup2(msh->out_fd, 1) < 0)
{
free(cmd_args);
ft_exit(msh, 1); ft_exit(msh, 1);
}
} }
else else
{ {
//fprintf(stderr, "redirecting pipe output\n");
//sleep(1);
if (dup2(msh->fds[i][1], 1) < 0) if (dup2(msh->fds[i][1], 1) < 0)
{ {
perror("dup2"); perror("dup2");
//fprintf(stderr, "exiting\n"); free(cmd_args);
//ft_exit(msh, 1); ft_exit(msh, 1);
} }
} }
} }
@ -51,7 +51,7 @@ int open_out_file(t_msh *msh, t_cmd **cur_cmd, char *filename)
while ((*cur_cmd)->next && is_cmd_type((*cur_cmd)->next)) while ((*cur_cmd)->next && is_cmd_type((*cur_cmd)->next))
*cur_cmd = (*cur_cmd)->next; *cur_cmd = (*cur_cmd)->next;
if ((*cur_cmd)->next && is_output_type((*cur_cmd)->next)) if ((*cur_cmd)->next && is_output_type((*cur_cmd)->next))
get_out_type(msh, *cur_cmd); get_out_type(msh, (*cur_cmd)->next);
} }
return (0); return (0);
} }
@ -66,17 +66,12 @@ int get_out_type(t_msh *msh, t_cmd *cmds)
msh->out_fd = 0; msh->out_fd = 0;
ret = 0; ret = 0;
cur_cmd = cmds; cur_cmd = cmds;
while (cur_cmd && !is_cmd_type(cur_cmd)) while (cur_cmd && cur_cmd->next && (!is_cmd_type(cur_cmd) && !is_output_type(cur_cmd)))
cur_cmd = cur_cmd->next; cur_cmd = cur_cmd->next;
while (cur_cmd && cur_cmd->next && !is_output_type(cur_cmd) while (cur_cmd && cur_cmd->next && !is_output_type(cur_cmd)
&& !is_operand_type(cur_cmd) && cur_cmd->cmd_type != PIPE) && !is_operand_type(cur_cmd) && cur_cmd->cmd_type != PIPE)
{
//fprintf(stderr, "%s: %d\n", cur_cmd->value, cur_cmd->cmd_type);
cur_cmd = cur_cmd->next; cur_cmd = cur_cmd->next;
} if (cur_cmd && (cur_cmd->cmd_type == CMD || cur_cmd->cmd_type == PAREN))
//if (cur_cmd)
//fprintf(stderr, "%s: %d\n", cur_cmd->value, cur_cmd->cmd_type);
if (cur_cmd->cmd_type == CMD || cur_cmd->cmd_type == PAREN)
msh->out_type = 0; msh->out_type = 0;
else if (cur_cmd && is_output_type(cur_cmd) && !is_operand_type(cur_cmd) else if (cur_cmd && is_output_type(cur_cmd) && !is_operand_type(cur_cmd)
&& cur_cmd->cmd_type != PIPE) && cur_cmd->cmd_type != PIPE)

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */ /* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */
/* Updated: 2024/04/22 13:20:25 by marde-vr ### ########.fr */ /* Updated: 2024/04/22 19:18:39 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -55,10 +55,10 @@ void child(t_msh *msh, char **cmd_args, int i)
if ((msh->in_type != CMD && msh->in_type != PAREN && msh->in_type != AND if ((msh->in_type != CMD && msh->in_type != PAREN && msh->in_type != AND
&& msh->in_type != OR && msh->in_type != PIPE) && msh->in_type != OR && msh->in_type != PIPE)
|| (msh->in_type == PIPE && i > 0)) || (msh->in_type == PIPE && i > 0))
redirect_input(msh, i); redirect_input(msh, i, cmd_args);
if (msh->out_type == PIPE || msh->out_type == RED_O if (msh->out_type == PIPE || msh->out_type == RED_O
|| msh->out_type == RED_O_APP) || msh->out_type == RED_O_APP)
redirect_output(msh, i); redirect_output(msh, i, cmd_args);
close_pipe_fds(msh, i); close_pipe_fds(msh, i);
execute_command(msh, cmd_args); execute_command(msh, cmd_args);
close(0); close(0);