Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing edge cases #9

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/builtin/builtin_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: iverniho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/16 10:43:33 by Jskehan #+# #+# */
/* Updated: 2024/08/04 16:29:27 by iverniho ### ########.fr */
/* Updated: 2024/08/05 14:06:29 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -50,11 +50,11 @@ void show_last_command_status(t_mini *mini, char **str)
void execute_builtin(t_mini *mini, t_cmd *cmd)
{
if (ft_strcmp(cmd->full_command[0], "echo") == 0)
mini_echo(cmd);
return (mini_echo(cmd));
else if (ft_strcmp(cmd->full_command[0], "pwd") == 0)
mini_pwd();
else if (ft_strcmp(cmd->full_command[0], "cd") == 0)
mini->exit_status = mini_cd(cmd->full_command, mini);
g_mini->exit_status = mini_cd(cmd->full_command, mini);
else if (ft_strcmp(cmd->full_command[0], "exit") == 0)
mini_exit(cmd->full_command, mini);
else if (ft_strcmp(cmd->full_command[0], "$?") == 0)
Expand Down
4 changes: 2 additions & 2 deletions src/env/path_resolution.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: iverniho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/16 15:23:12 by Jskehan #+# #+# */
/* Updated: 2024/08/04 16:04:59 by iverniho ### ########.fr */
/* Updated: 2024/08/05 13:26:42 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -57,7 +57,7 @@ char *resolve_command_path(char *command, t_mini **mini)
i++;
}
ft_free_2d_array(&paths);
g_mini->exit_status = 127;
// g_mini->exit_status = 127;
// printf("Command path not found for: %s\n", command); // Debug print
return (NULL);
}
24 changes: 18 additions & 6 deletions src/exec/execution.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: iverniho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/16 10:52:13 by Jskehan #+# #+# */
/* Updated: 2024/08/05 11:49:49 by iverniho ### ########.fr */
/* Updated: 2024/08/05 16:12:13 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,6 +26,12 @@ static void child_redir(t_cmd *cmd)
perror("dup2");
close(cmd->fd_out);
}
if (cmd->fd_out == -1)
{
// printf("fdout = -1\n");
if (close(STDOUT_FILENO) == -1)
perror("close");
}
}

static void execute_command(t_mini *mini, t_cmd *cmd)
Expand All @@ -49,7 +55,8 @@ static void execute_command(t_mini *mini, t_cmd *cmd)
}
else
{
mini_perror(CMD_NOT_FOUND, cmd->full_command[0], 127);
// mini_perror(CMD_NOT_FOUND, cmd->full_command[0], 127);
ft_error1(4, cmd->full_command[0], 127, cmd->full_command[0]);
exit(127);
}
}
Expand Down Expand Up @@ -118,16 +125,21 @@ static void wait_for_children(int num_cmds, t_mini *mini)
{
int status;

(void)mini;
for (int k = 0; k < num_cmds; k++)
{
wait(&status);
if (WIFEXITED(status))
{
mini->exit_status = WEXITSTATUS(status);
// mini->exit_status = WEXITSTATUS(status);
g_mini->exit_status = WEXITSTATUS(status);
}
else if (WIFSIGNALED(status))
{
mini->exit_status = 128 + WTERMSIG(status);
// mini->exit_status = 128 + WTERMSIG(status);
// g_mini->exit_status = 128 + WTERMSIG(status);
g_mini->exit_status = 1;

}
}
}
Expand Down Expand Up @@ -180,11 +192,11 @@ void *check_to_fork(t_mini *mini, t_list *commands)
if (cmd->full_command && (dir = opendir(cmd->full_command[0])) != NULL)
{
closedir(dir);
mini->exit_status = 126; // Command is a directory
g_mini->exit_status = 126; // Command is a directory
return (NULL);
}
if (!cmd->full_command && cmd->is_heredoc == 1)
return (mini->exit_status = 127, NULL);// Command not found
return (g_mini->exit_status = 127, NULL);// Command not found
cmd->command_path = resolve_command_path(cmd->full_command[0], &mini);
if (cmd->command_path && access(cmd->command_path, X_OK) == 0)
exec_pipes(mini, commands);
Expand Down
5 changes: 1 addition & 4 deletions src/parser/parser_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: iverniho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 17:38:35 by iverniho #+# #+# */
/* Updated: 2024/08/05 12:53:42 by iverniho ### ########.fr */
/* Updated: 2024/08/05 16:32:33 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -185,10 +185,7 @@ void ft_error1(int error, char *arg, int exit_code, char *message)
else if (error == 9)
{
ft_putstr_fd("minishell> export: ", STDERR_FILENO);

ft_putendl_fd(ft_strjoin(add_quotes(arg), message), 2);
// ft_putstr_fd("minishell> ", STDERR_FILENO);
// ft_putendl_fd(message, 2);
g_mini->exit_status = exit_code;
}
}
7 changes: 4 additions & 3 deletions src/redirect/here_doc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* here_doc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Jskehan <jskehan@student.42Berlin.de> +#+ +:+ +#+ */
/* By: iverniho <iverniho@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/10 14:19:34 by Jskehan #+# #+# */
/* Updated: 2024/07/16 14:32:56 by Jskehan ### ########.fr */
/* Updated: 2024/08/05 14:38:25 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -49,7 +49,8 @@ int get_here_doc(t_mini *mini, const char *limit, const char *warn)
mini->exit_status = 0;
if (pipe(fd) == -1)
{
mini_perror("PIPERR", NULL, 1);
// mini_perror("PIPERR", NULL, 1);
ft_error1(4, NULL, 1, "pipe");
return (-1);
}
str = read_string_until_limit(mini, limit, warn);
Expand Down
15 changes: 7 additions & 8 deletions src/redirect/redirection.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* redirection.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Jskehan <jskehan@student.42berlin.de> +#+ +:+ +#+ */
/* By: iverniho <iverniho@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/10 14:08:23 by Jskehan #+# #+# */
/* Updated: 2024/08/02 11:55:26 by Jskehan ### ########.fr */
/* Updated: 2024/08/05 16:25:51 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,12 +21,12 @@ int get_fd(int oldfd, t_cmd *cmd, char *path)
if (!path)
return (-1);
if (access(path, F_OK) == -1 && !cmd->is_outfile)
mini_perror("NDIR", path, 127);
ft_error1(127, path, 1, "NDIR");
else if (!cmd->is_outfile && access(path, R_OK) == -1)
mini_perror("NPERM", path, 126);
ft_error1(126, path, 1, "NPERM");
else if (cmd->is_outfile && access(path, W_OK) == -1 && access(path,
F_OK) == 0)
mini_perror("NPERM", path, 126);
ft_error1(126, path, 1, "NPERM");
if (cmd->is_outfile && cmd->is_append)
fd = open(path, O_CREAT | O_WRONLY | O_APPEND, 0666);
else if (cmd->is_outfile && !cmd->is_append)
Expand Down Expand Up @@ -58,11 +58,10 @@ t_mini *get_file(t_mini *mini, t_list *command, char **args, int *i)
*i = -1;
if ((cmd->is_outfile ? cmd->fd_out : cmd->fd_in) != -1)
{
ft_putendl_fd(nl, 2);
mini->exit_status = 2;
g_mini->exit_status = 2;
}
else
mini->exit_status = 1;
g_mini->exit_status = 1;
}
return (mini);
}
Expand Down
23 changes: 18 additions & 5 deletions src/redirect/set_redir_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: iverniho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/16 14:23:13 by Jskehan #+# #+# */
/* Updated: 2024/08/05 11:55:43 by iverniho ### ########.fr */
/* Updated: 2024/08/05 16:24:52 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,17 +23,18 @@ static int check_access(char *path, int mode)
if (mode == 1) // Read mode
{
if (access(path, F_OK) == -1)
return (ft_error1(3, NULL, 127, NULL), 0);
{
ft_error1(3, NULL, 0, NULL);
return (0);
}
else if (access(path, R_OK) == -1)
return (ft_error1(5, NULL, 1, NULL), 0);
// return (ft_error(5, NULL), 0);
return (1);
}
else if (mode == 2) // Write mode
{
if (access(path, W_OK) == -1 && access(path, F_OK) != -1)
return (ft_error1(5, NULL, 1, NULL), 0);
// return (ft_error(5, NULL), 0);
return (1);
}
return (1);
Expand Down Expand Up @@ -64,8 +65,14 @@ static t_cmd *handle_redirection(t_cmd *node, char **full_command, int **i,
if (node->fd_out > 2)
close(node->fd_out);
node->fd_out = get_fd(node->fd_out, mode, full_command[++(**i)]);
if (node->fd_out == -1)
if (!full_command[*(*i)] || node->fd_out == -1)
{
**i = -2;
if (node->fd_out != -1)
g_mini->exit_status = 2;
else
g_mini->exit_status = 1;
}
}
else if (mode == 1)
{
Expand Down Expand Up @@ -114,5 +121,11 @@ t_cmd *set_redir(t_cmd *node, char *input, char **full_command, int *i)
else if (input[0] != '|')
node->full_command = ft_add_row_2d_array(node->full_command, input);
}
if (node->fd_in == -1 || node->fd_out == -1)
{
ft_free_2d_array(&node->full_command);
free(node);
return (NULL);
}
return (node);
}
11 changes: 8 additions & 3 deletions src/utils/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
/* By: iverniho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 21:09:11 by Jskehan #+# #+# */
/* Updated: 2024/08/02 17:10:53 by iverniho ### ########.fr */
/* Updated: 2024/08/05 16:32:15 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

void handle_command_node(char **input, t_list **commands,
int handle_command_node(char **input, t_list **commands,
t_list **cur_command, int *i, t_mini *mini)
{
*cur_command = ft_lstlast(*commands);
Expand All @@ -23,6 +23,9 @@ void handle_command_node(char **input, t_list **commands,
}
(*cur_command)->content = set_redir((*cur_command)->content, input[*i],
input, i);
if (!(*cur_command)->content)
return (ft_lstclear(commands, free), -1);
return (1);
}

t_list *create_nodes(char **input, t_mini *mini)
Expand All @@ -37,14 +40,16 @@ t_list *create_nodes(char **input, t_mini *mini)
(void)mini;
while (input && input[++i])
{
handle_command_node(input, &commands, &cur_command, &i, mini);
if (handle_command_node(input, &commands, &cur_command, &i, mini) == -1)
return (g_mini->exit_status = 1, NULL);
if (i == -2)
{
ft_lstclear(&commands, free);
ft_free_2d_array(&input);
return (NULL);
}
}
// print_nodes(commands);
return (commands);
}

Expand Down
Loading