error_MSG in builtin_cd/exit

This commit is contained in:
whaffman 2025-03-01 14:11:46 +01:00
parent ededbc98f4
commit f9062d4385
3 changed files with 39 additions and 40 deletions

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:33:07 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"); env = environment_get(msh, "HOME");
if (env == NULL || env->value == NULL) if (env == NULL || env->value == NULL)
{ {
ft_putendl_fd("minishell: cd: HOME not set", STDERR_FILENO); error_msg(NULL, NULL);
return (FAILURE); return (FAILURE);
} }
path = env->value; path = env->value;
@ -31,10 +31,7 @@ int builtin_cd(t_minishell *msh, t_command *cmd)
path = cmd->args[1]; path = cmd->args[1];
if (chdir(path) == -1) if (chdir(path) == -1)
{ {
ft_putstr_fd("minishell: cd: ", STDERR_FILENO); error_msg("cd", path);
ft_putstr_fd(path, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
ft_putendl_fd(strerror(errno), STDERR_FILENO);
return (FAILURE); return (FAILURE);
} }
return (SUCCESS); return (SUCCESS);

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:32:59 by whaffman #+# #+# */ /* 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]); exit_status = ft_atoi(cmd->args[1]);
} }
free_minishell(&msh); free_minishell(&msh);
ft_putendl_fd("exit", STDERR_FILENO); //TODO stderr?
rl_clear_history(); rl_clear_history();
exit(exit_status); exit(exit_status);
return (FAILURE); return (FAILURE);

View File

@ -1,33 +1,34 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* :::::::: */
/* error_msg.c :+: :+: */ /* error_msg.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 17:03:13 by whaffman #+# #+# */ /* Created: 2025/02/20 17:03:13 by whaffman #+# #+# */
/* Updated: 2025/02/20 17:59:36 by whaffman ######## odam.nl */ /* Updated: 2025/03/01 14:11:16 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#define SHELL_NAME "minishell"
void error_msg(char *func, char *msg)
{ void error_msg(char *func, char *msg)
if (errno) {
perror(RED BOLD "minishell" RESET); if (errno)
else perror(RED BOLD SHELL_NAME RESET);
{ else
ft_putstr_fd(RED BOLD "minishell" RESET ": ", 2); {
if (func != NULL) ft_putstr_fd(RED BOLD SHELL_NAME RESET ": ", 2);
{ if (func != NULL)
ft_putstr_fd(func, 2); {
ft_putstr_fd(": ", 2); ft_putstr_fd(func, 2);
} ft_putstr_fd(": ", 2);
if (msg != NULL) }
ft_putstr_fd(msg, 2); if (msg != NULL)
else ft_putstr_fd(msg, 2);
ft_putstr_fd("general error", 2); else
ft_putstr_fd("\n", 2); ft_putstr_fd("general error", 2);
} ft_putstr_fd("\n", 2);
} }
}