From 66183ab4410fd61f2465f554af5cf48b27ccf908 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Sun, 5 Dec 2021 16:28:41 +0100 Subject: [PATCH] cd refactoring, error_wrappers --- headers/minishell_prototypes.h | 6 +++--- srcs/builtins/cd.c | 17 ++++------------- srcs/error_wrappers.c | 10 +++++++--- srcs/exec/find_access.c | 4 ++-- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index 1abbc1d..619e293 100644 --- a/headers/minishell_prototypes.h +++ b/headers/minishell_prototypes.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */ -/* Updated: 2021/12/04 19:09:42 by lperrey ### ########.fr */ +/* Updated: 2021/12/05 15:55:07 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,8 +71,8 @@ void close_pipeline_fd(t_cmd *pipeline[]); typedef void (*t_free_f)(void *); // generic // Error wrappers -void shell_error(char *s1, char *s2); -void shell_perror(char *s1, char *s2, char *s3); +int shell_error(char *s1, char *s2, char *s3, int ret_val); +int shell_perror(char *s1, char *s2, char *s3, int ret_val); // Generic char *ft_strjoinfree(char *s1, char *s2); diff --git a/srcs/builtins/cd.c b/srcs/builtins/cd.c index 48c5701..06abd87 100644 --- a/srcs/builtins/cd.c +++ b/srcs/builtins/cd.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/12/04 19:31:19 by lperrey #+# #+# */ -/* Updated: 2021/12/04 20:01:37 by lperrey ### ########.fr */ +/* Updated: 2021/12/05 16:21:41 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,21 +22,12 @@ int builtin_cd(int argc, char *argv[], t_all *c) { tmp = getenv("HOME"); if (!tmp) - { - shell_error("cd: ", "HOME not set"); - return (EXIT_FAILURE); - } + return (shell_error("cd: ", "HOME not set", "", EXIT_FAILURE)); } else if (argc > 2) - { - shell_error("cd: ", "too many arguments"); - return (EXIT_FAILURE); - } + return (shell_error("cd: ", "too many arguments", "", EXIT_FAILURE)); if (chdir(tmp) == -1) - { - shell_perror("cd: ", tmp, ": "); - return (EXIT_FAILURE); - } + return (shell_perror("cd: ", tmp, ": ", EXIT_FAILURE)); tmp = init_prompt(c->prompt_base); if (!tmp) return (ft_reti_perror(EXIT_FAILURE, "builtin_cd, init_prompt()")); diff --git a/srcs/error_wrappers.c b/srcs/error_wrappers.c index c88754c..aef5775 100644 --- a/srcs/error_wrappers.c +++ b/srcs/error_wrappers.c @@ -6,13 +6,13 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/12/01 17:16:30 by lperrey #+# #+# */ -/* Updated: 2021/12/04 19:09:34 by lperrey ### ########.fr */ +/* Updated: 2021/12/05 16:26:48 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void shell_error(char *s1, char *s2) +int shell_error(char *s1, char *s2, char *s3, int ret_val) { char *prefix; @@ -22,10 +22,13 @@ void shell_error(char *s1, char *s2) write(STDERR_FILENO, s1, ft_strlen(s1)); if (s2) write(STDERR_FILENO, s2, ft_strlen(s2)); + if (s3) + write(STDERR_FILENO, s3, ft_strlen(s3)); write(STDERR_FILENO, "\n", 1); + return (ret_val); } -void shell_perror(char *s1, char *s2, char *s3) +int shell_perror(char *s1, char *s2, char *s3, int ret_val) { char *prefix; @@ -38,4 +41,5 @@ void shell_perror(char *s1, char *s2, char *s3) if (s3) write(STDERR_FILENO, s3, ft_strlen(s3)); perror(NULL); + return (ret_val); } diff --git a/srcs/exec/find_access.c b/srcs/exec/find_access.c index fbe16e5..22d44a1 100644 --- a/srcs/exec/find_access.c +++ b/srcs/exec/find_access.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ -/* Updated: 2021/12/01 17:16:55 by lperrey ### ########.fr */ +/* Updated: 2021/12/05 16:20:55 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,7 +48,7 @@ int cmd_find_access(t_cmd *cmd, char *path[]) if (!cmd->path) { cmd->error = EXIT_CMD_NOT_FOUND; - shell_error(cmd->argv[0], ": command not found"); + shell_error(cmd->argv[0], ": ", "command not found", 0); } } return (1);