From f9062d438529988126c17ce03786b8f61c05ec13 Mon Sep 17 00:00:00 2001 From: whaffman Date: Sat, 1 Mar 2025 14:11:46 +0100 Subject: [PATCH] error_MSG in builtin_cd/exit --- src/builtin/builtin_cd.c | 9 ++--- src/builtin/builtin_exit.c | 3 +- src/utils/error_msg.c | 67 +++++++++++++++++++------------------- 3 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/builtin/builtin_cd.c b/src/builtin/builtin_cd.c index f2a5006..edcc7ac 100644 --- a/src/builtin/builtin_cd.c +++ b/src/builtin/builtin_cd.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/20 11:33:07 by whaffman #+# #+# */ -/* Updated: 2025/02/26 15:45:55 by whaffman ######## odam.nl */ +/* Updated: 2025/03/01 14:00:52 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ int builtin_cd(t_minishell *msh, t_command *cmd) env = environment_get(msh, "HOME"); if (env == NULL || env->value == NULL) { - ft_putendl_fd("minishell: cd: HOME not set", STDERR_FILENO); + error_msg(NULL, NULL); return (FAILURE); } path = env->value; @@ -31,10 +31,7 @@ int builtin_cd(t_minishell *msh, t_command *cmd) path = cmd->args[1]; if (chdir(path) == -1) { - ft_putstr_fd("minishell: cd: ", STDERR_FILENO); - ft_putstr_fd(path, STDERR_FILENO); - ft_putstr_fd(": ", STDERR_FILENO); - ft_putendl_fd(strerror(errno), STDERR_FILENO); + error_msg("cd", path); return (FAILURE); } return (SUCCESS); diff --git a/src/builtin/builtin_exit.c b/src/builtin/builtin_exit.c index adf39cf..49086a7 100644 --- a/src/builtin/builtin_exit.c +++ b/src/builtin/builtin_exit.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/02/20 11:32:59 by whaffman #+# #+# */ -/* Updated: 2025/02/26 15:47:14 by whaffman ######## odam.nl */ +/* Updated: 2025/03/01 12:54:28 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -37,6 +37,7 @@ int builtin_exit(t_minishell *msh, t_command *cmd) exit_status = ft_atoi(cmd->args[1]); } free_minishell(&msh); + ft_putendl_fd("exit", STDERR_FILENO); //TODO stderr? rl_clear_history(); exit(exit_status); return (FAILURE); diff --git a/src/utils/error_msg.c b/src/utils/error_msg.c index 2ce1ed1..8fffef7 100644 --- a/src/utils/error_msg.c +++ b/src/utils/error_msg.c @@ -1,33 +1,34 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* error_msg.c :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/02/20 17:03:13 by whaffman #+# #+# */ -/* Updated: 2025/02/20 17:59:36 by whaffman ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -void error_msg(char *func, char *msg) -{ - if (errno) - perror(RED BOLD "minishell" RESET); - else - { - ft_putstr_fd(RED BOLD "minishell" RESET ": ", 2); - if (func != NULL) - { - ft_putstr_fd(func, 2); - ft_putstr_fd(": ", 2); - } - if (msg != NULL) - ft_putstr_fd(msg, 2); - else - ft_putstr_fd("general error", 2); - ft_putstr_fd("\n", 2); - } -} +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* error_msg.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/20 17:03:13 by whaffman #+# #+# */ +/* Updated: 2025/03/01 14:11:16 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" +#define SHELL_NAME "minishell" + +void error_msg(char *func, char *msg) +{ + if (errno) + perror(RED BOLD SHELL_NAME RESET); + else + { + ft_putstr_fd(RED BOLD SHELL_NAME RESET ": ", 2); + if (func != NULL) + { + ft_putstr_fd(func, 2); + ft_putstr_fd(": ", 2); + } + if (msg != NULL) + ft_putstr_fd(msg, 2); + else + ft_putstr_fd("general error", 2); + ft_putstr_fd("\n", 2); + } +}