Skip to content

Commit

Permalink
Merge pull request #42 from Ofacy/exec-errors
Browse files Browse the repository at this point in the history
Fix all tested exit code issues
  • Loading branch information
UnRenardQuiDab authored Apr 9, 2024
2 parents 1b310f0 + b994c87 commit 6609a01
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 26 deletions.
4 changes: 3 additions & 1 deletion includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/13 17:55:03 by lcottet #+# #+# */
/* Updated: 2024/04/08 11:37:09 by lcottet ### ########.fr */
/* Updated: 2024/04/08 19:54:38 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -136,4 +136,6 @@ void signal_stop(int sig);
void signal_heredoc(int sig);
void reset_signal(void);

int ft_atoll(const char *nptr);

#endif
8 changes: 2 additions & 6 deletions srcs/builtins/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/14 13:57:43 by bwisniew #+# #+# */
/* Updated: 2024/04/09 15:37:39 by bwisniew ### ########.fr */
/* Updated: 2024/04/09 16:37:12 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -80,11 +80,7 @@ int cd_home(t_mshell *sh)
custom_error("cd", "HOME not set");
return (1);
}
if (chdir(home->value) == -1)
{
error("cd");
return (1);
}
chdir(home->value);
return (cd_change_env(sh, NULL));
}

Expand Down
16 changes: 11 additions & 5 deletions srcs/exec/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/10 17:18:34 by lcottet #+# #+# */
/* Updated: 2024/04/08 14:29:39 by bwisniew ### ########.fr */
/* Updated: 2024/04/09 16:28:56 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -44,9 +44,15 @@ int exec_set_cmd(t_execute *exec, t_mshell *sh)
if (!exec->cmd)
{
if (errno == ENOENT)
{
custom_error(((char **)exec->args.tab)[0], "command not found");
set_env_return(sh, 127);
}
else
{
error(((char **)exec->args.tab)[0]);
set_env_return(sh, 126);
}
return (1);
}
return (0);
Expand Down Expand Up @@ -87,7 +93,7 @@ pid_t exec_txt(t_execute *exec, t_mshell *sh)
if (vector_add(&exec->args, &null) != 0)
return (-1);
if (exec_set_cmd(exec, sh))
return (-1);
return (-4);
if (exec->builtin && !exec->has_pipe)
{
if (exec_builtins(exec, sh) != 0)
Expand All @@ -98,7 +104,7 @@ pid_t exec_txt(t_execute *exec, t_mshell *sh)
if (filetype == -1)
return (free(exec->cmd), -1);
else if (!filetype)
return (free(exec->cmd), -4);
return (free(exec->cmd), set_env_return(sh, 126), -4);
pid = exec_fork(exec, sh);
free(exec->cmd);
return (pid);
Expand All @@ -119,11 +125,11 @@ pid_t exec(t_mshell *sh)
{
if (exec_prepare(sh, &exec, &i) != 0)
return (close_exec(&exec), vector_free(&exec.args), -1);
pid = -3;
pid = -2;
if (exec.in > 0 && exec.out > 0)
{
pid = exec_txt(&exec, sh);
if (pid < 0)
if (pid < 0 && pid == -1)
return (close_exec(&exec), vector_free(&exec.args), pid);
}
exec_init(&exec, sh, true);
Expand Down
3 changes: 2 additions & 1 deletion srcs/exec/filetype.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
/* By: lcottet <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 23:12:15 by lcottet #+# #+# */
/* Updated: 2024/04/03 23:39:57 by lcottet ### ########.fr */
/* Updated: 2024/04/09 16:28:15 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

#include "minishell.h"

Expand Down
4 changes: 2 additions & 2 deletions srcs/exec/here_doc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/10 20:41:37 by lcottet #+# #+# */
/* Updated: 2024/04/08 11:46:31 by lcottet ### ########.fr */
/* Updated: 2024/04/08 18:58:18 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -50,7 +50,7 @@ char *here_doc_getline(t_mshell *sh, size_t i, t_fd fd, int *err)
char *tmp;

line.txt = hd_get_user_input();
if (g_signal != -1)
if (g_signal != 0)
return (here_doc_signal(&line, sh, err));
else if (!line.txt)
{
Expand Down
4 changes: 3 additions & 1 deletion srcs/exec/here_doc_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
/* By: lcottet <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/08 11:22:29 by lcottet #+# #+# */
/* Updated: 2024/04/08 11:43:58 by lcottet ### ########.fr */
/* Updated: 2024/04/09 16:33:58 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

#include "get_next_line.h"
#include "minishell.h"
#include <readline/readline.h>
#include <stdbool.h>
#include <errno.h>

char *hd_get_user_input(void)
{
if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO))
return (readline(PROMPT_HEREDOC));
errno = 0;
return (get_next_line(STDIN_FILENO));
}

Expand Down
8 changes: 5 additions & 3 deletions srcs/exec/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/10 17:23:03 by lcottet #+# #+# */
/* Updated: 2024/04/08 13:03:54 by lcottet ### ########.fr */
/* Updated: 2024/04/09 16:32:14 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -43,6 +43,8 @@ char *path_valid(char *file, char *curr_path, int access_mode)
char *tmp1;
char *tmp;

if (file[0] == '\0')
return (NULL);
tmp1 = ft_strjoin(curr_path, "/");
if (!tmp1)
return (NULL);
Expand All @@ -62,8 +64,6 @@ char *get_openable_path(char *file, int access_mode, t_mshell *sh)
int i;
char **path;

if (!file)
return (NULL);
if (ft_strchr(file, '/') && access(file, access_mode) != -1)
return (ft_strdup(file));
if (errno == EACCES)
Expand All @@ -81,6 +81,8 @@ char *get_openable_path(char *file, int access_mode, t_mshell *sh)
return (ft_freesplit(path), NULL);
i++;
}
if (errno == 0)
errno = ENOENT;
ft_freesplit(path);
return (NULL);
}
4 changes: 2 additions & 2 deletions srcs/exec/wait.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/11 17:58:52 by bwisniew #+# #+# */
/* Updated: 2024/04/08 14:04:46 by bwisniew ### ########.fr */
/* Updated: 2024/04/08 19:16:31 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,7 +34,7 @@ int wait_for_child(pid_t last_pid)
return (1);
if (errno != ECHILD)
return (error("wait"), 127);
if (last_pid == -1 && g_signal == 0)
if (last_pid == -5 || (last_pid == -1 && g_signal == 0))
return (127);
else if (last_pid == -1)
return (128 + g_signal);
Expand Down
11 changes: 7 additions & 4 deletions srcs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/14 13:11:23 by lcottet #+# #+# */
/* Updated: 2024/04/08 14:10:03 by bwisniew ### ########.fr */
/* Updated: 2024/04/09 16:35:22 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,6 +19,7 @@
#include <readline/history.h>
#include <readline/readline.h>
#include <signal.h>
#include <errno.h>

int minishell(t_mshell *sh, char *input)
{
Expand All @@ -29,6 +30,7 @@ int minishell(t_mshell *sh, char *input)
return (-1);
if (sh->tokens.len != 0 && isatty(STDERR_FILENO))
add_history(input);
errno = 0;
syntax = check_syntax(&sh->tokens);
if (syntax == 0)
{
Expand All @@ -53,10 +55,12 @@ void finish_mshell(t_mshell *mshell)
close_fd(&mshell->stdin);
vector_foreach(&mshell->env, (void (*)(void *))env_free);
vector_free(&mshell->env);
free(mshell->last_return.value);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
if (mshell->exit == -1)
mshell->exit = (unsigned int)ft_atoll(mshell->last_return.value);
free(mshell->last_return.value);
}

int prepare_mshell(t_mshell *mshell, char **env)
Expand Down Expand Up @@ -96,8 +100,7 @@ int main(int argc, char **argv, char **env)
}
if (isatty(STDIN_FILENO))
printf("exit\n");
errno = 0;
finish_mshell(&mshell);
if (mshell.exit == -1)
return (0);
return ((char)mshell.exit);
}
4 changes: 3 additions & 1 deletion srcs/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/14 13:12:55 by lcottet #+# #+# */
/* Updated: 2024/04/08 14:05:40 by bwisniew ### ########.fr */
/* Updated: 2024/04/09 16:33:49 by lcottet ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,6 +17,7 @@
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <errno.h>

t_token prompt(t_mshell *sh)
{
Expand Down Expand Up @@ -52,6 +53,7 @@ char *get_user_input(t_mshell *sh)
input = readline(c_prompt.txt);
else
input = get_next_line(STDIN_FILENO);
errno = 0;
if (g_signal != 0)
{
set_env_return(sh, 128 + g_signal);
Expand Down

0 comments on commit 6609a01

Please sign in to comment.