-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from Ofacy/exec-errors
Fix all tested exit code issues
- Loading branch information
Showing
10 changed files
with
40 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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)); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
|
@@ -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) | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
|
@@ -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) | ||
|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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) | ||
{ | ||
|
@@ -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) | ||
{ | ||
|
@@ -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) | ||
|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -17,6 +17,7 @@ | |
#include <stdio.h> | ||
#include <readline/readline.h> | ||
#include <readline/history.h> | ||
#include <errno.h> | ||
|
||
t_token prompt(t_mshell *sh) | ||
{ | ||
|
@@ -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); | ||
|