From bd8f652de51395fb26659f7a634e55bd46917b2e Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 17 Jul 2020 14:09:10 +0200 Subject: Fixing exit error message and overflow detection --- src/builtin/exit.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src/builtin/exit.c') diff --git a/src/builtin/exit.c b/src/builtin/exit.c index 15271f6..860325e 100644 --- a/src/builtin/exit.c +++ b/src/builtin/exit.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:16 by charles #+# #+# */ -/* Updated: 2020/07/13 15:35:44 by charles ### ########.fr */ +/* Updated: 2020/07/17 16:36:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,27 +19,32 @@ int builtin_exit(char **argv, t_env env) { - int status; + long status; + char *after; (void)env; if (argv[1] == NULL) status = g_last_status_code; - else if (argv[2] != NULL) - { - // replace with minishell error system - ft_putendl_fd("minishell: exit: too many arguments", STDERR_FILENO); - return 1; - } else { errno = 0; - status = ft_atoi_strict(argv[1]); - if (errno != 0) + status = ft_strtol(argv[1], &after, 10); + while (ft_isblank(*after)) + after++; + if (*after != '\0' || errno == ERANGE) { // replace with minishell error system - ft_putendl_fd("minishell: exit: ---argv[1]---: numeric argument required", STDERR_FILENO); + ft_putstr_fd("minishell: exit: ", STDERR_FILENO); + ft_putstr_fd(argv[1], STDERR_FILENO); + ft_putstr_fd(": numeric argument required\n", STDERR_FILENO); return 2; } + if (argv[2] != NULL) + { + // replace with minishell error system + ft_putendl_fd("minishell: exit: too many arguments", STDERR_FILENO); + return 1; + } } exit(status % 256); return (0); -- cgit