fixed a segfault

This commit is contained in:
mdev9
2024-03-02 17:00:58 +01:00
parent 25d81d04e2
commit 44f8024fc0
3 changed files with 16 additions and 12 deletions

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */ /* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
/* Updated: 2024/02/28 18:53:41 by marde-vr ### ########.fr */ /* Updated: 2024/03/02 16:48:46 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -155,6 +155,7 @@ void redirect_input(t_msh *msh, int i)
} }
else if (i > 0) else if (i > 0)
{ {
ft_printf_fd(2, "input_fd: %d\n", msh->fds[i - 1][0]);
if (dup2(msh->fds[i - 1][0], 0) < 0) if (dup2(msh->fds[i - 1][0], 0) < 0)
ft_exit(msh, 1); ft_exit(msh, 1);
} }
@ -178,12 +179,12 @@ void pipe_child(t_msh *msh, char **cmd_args, int i)
{ {
if (msh->in_type != ARG) if (msh->in_type != ARG)
{ {
//ft_printf_fd(2, "redirecting input of %s of type %d\n", msh->cmds->token, msh->in_type); ft_printf_fd(2, "redirecting input of %s of type %d with fd %d\n", msh->cmds->token, msh->in_type, msh->in_fd);
redirect_input(msh, i); redirect_input(msh, i);
} }
if (msh->out_type != ARG) if (msh->out_type != ARG)
{ {
//ft_printf_fd(2, "redirecting output of %s of type %d\n", msh->cmds->token, msh->out_type); ft_printf_fd(2, "redirecting output of %s of type %d with fd %d\n", msh->cmds->token, msh->out_type, msh->out_fd);
redirect_output(msh, i); redirect_output(msh, i);
} }
if (i != 0) if (i != 0)
@ -358,8 +359,8 @@ void get_in_type(t_msh *msh)
handle_here_doc(msh, cur_cmd->token); handle_here_doc(msh, cur_cmd->token);
if (msh->in_type == RED_I) if (msh->in_type == RED_I)
{ {
//ft_printf_fd(2, "opening %s\n", cur_cmd->token);
msh->in_fd = open(cur_cmd->token, O_RDONLY); msh->in_fd = open(cur_cmd->token, O_RDONLY);
ft_printf_fd(2, "opened %s: %d\n", cur_cmd->token, msh->in_fd);
if (msh->in_fd == -1) if (msh->in_fd == -1)
{ {
ft_printf_fd(2, "minishell: %s: ", cur_cmd->token); ft_printf_fd(2, "minishell: %s: ", cur_cmd->token);
@ -367,8 +368,8 @@ void get_in_type(t_msh *msh)
// todo: cancel execution of all commands // todo: cancel execution of all commands
} }
} }
cur_cmd = cur_cmd->next; //cur_cmd = cur_cmd->next;
msh->cmds = cur_cmd; //msh->cmds = cur_cmd;
//ft_printf_fd(2, "cmd: %s\n", msh->cmds->token); //ft_printf_fd(2, "cmd: %s\n", msh->cmds->token);
} }
} }
@ -380,6 +381,7 @@ void get_out_type(t_msh *msh)
t_cmd *cur_cmd; t_cmd *cur_cmd;
msh->out_type = ARG; msh->out_type = ARG;
msh->out_fd = 0;
cur_cmd = msh->cmds; cur_cmd = msh->cmds;
while (cur_cmd && cur_cmd->next && cur_cmd->type == ARG) while (cur_cmd && cur_cmd->next && cur_cmd->type == ARG)
cur_cmd = cur_cmd->next; cur_cmd = cur_cmd->next;
@ -394,6 +396,8 @@ void get_out_type(t_msh *msh)
if (msh->out_type == RED_O_APP) if (msh->out_type == RED_O_APP)
msh->out_fd = open(cur_cmd->next->token, msh->out_fd = open(cur_cmd->next->token,
O_CREAT | O_RDWR | O_APPEND, 0644); O_CREAT | O_RDWR | O_APPEND, 0644);
if (msh->out_fd) // debug
ft_printf_fd(2, "opened %s: %d\n", cur_cmd->next->token, msh->out_fd);
if (msh->out_fd == -1) if (msh->out_fd == -1)
{ {
perror("open"); perror("open");
@ -424,6 +428,7 @@ void exec_command(t_msh *msh)
ft_exit(msh, 1); ft_exit(msh, 1);
get_in_type(msh); get_in_type(msh);
get_out_type(msh); get_out_type(msh);
ft_printf_fd(2, "%d\n", msh->out_fd);
//ft_printf_fd(2, "cmd: %s\n", msh->cmds->token); //ft_printf_fd(2, "cmd: %s\n", msh->cmds->token);
if (!cmd_is_builtin(msh, msh->cmds->token)) if (!cmd_is_builtin(msh, msh->cmds->token))
get_cmd_path(msh); get_cmd_path(msh);

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/02/26 15:28:23 by marde-vr ### ########.fr */ /* Updated: 2024/03/02 16:13:58 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -88,7 +88,7 @@ int main(int argc, char **argv, char **envp)
command = (char *)1; command = (char *)1;
init_minishell(&msh, argc, argv, envp); init_minishell(&msh, argc, argv, envp);
//handle_minishellrc(msh); handle_minishellrc(msh);
while (msh->env && command) while (msh->env && command)
{ {
prompt = get_prompt(msh->env); prompt = get_prompt(msh->env);
@ -99,9 +99,8 @@ int main(int argc, char **argv, char **envp)
add_history(command); add_history(command);
msh->cmds = parse_command(command, msh->env); msh->cmds = parse_command(command, msh->env);
free(command); free(command);
//print_parsed_cmd(parsed_cmd);//debug
msh->cmds = handle_alias(msh); msh->cmds = handle_alias(msh);
//print_parsed_cmd(parsed_cmd);//debug //print_parsed_cmd(msh->cmds);
exec_command(msh); exec_command(msh);
free_cmd(msh->cmds); free_cmd(msh->cmds);
} }

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/16 17:40:16 by marde-vr #+# #+# */ /* Created: 2024/02/16 17:40:16 by marde-vr #+# #+# */
/* Updated: 2024/02/26 15:24:28 by marde-vr ### ########.fr */ /* Updated: 2024/02/29 13:13:15 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,7 +24,7 @@ void exec_rc_file(t_msh *msh, int fd)
msh->cmds = parse_command(line, msh->env); msh->cmds = parse_command(line, msh->env);
exec_command(msh); exec_command(msh);
free_cmd(msh->cmds); free_cmd(msh->cmds);
free(line); //free(line);
} }
free(line); free(line);
line = get_next_line(fd); line = get_next_line(fd);