2023-02-24 06:46:02 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* exit.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/02/24 10:17:59 by erey-bet #+# #+# */
|
2023-04-05 06:17:16 -04:00
|
|
|
/* Updated: 2023/04/05 12:16:37 by erey-bet ### ########.fr */
|
2023-02-24 06:46:02 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
#include "./builtins_private.h"
|
2023-02-24 06:46:02 -05:00
|
|
|
|
2023-02-28 08:54:57 -05:00
|
|
|
static int error(int err, char *reason, char *problem)
|
2023-02-24 06:46:02 -05:00
|
|
|
{
|
2023-02-28 08:40:51 -05:00
|
|
|
ft_putstr_fd("minishell: exit ", 2);
|
2023-02-24 06:46:02 -05:00
|
|
|
if (problem != NULL)
|
|
|
|
{
|
2023-02-28 08:40:51 -05:00
|
|
|
ft_putstr_fd(problem, 2);
|
2023-02-28 08:53:56 -05:00
|
|
|
write(2, ": ", 3);
|
2023-02-24 06:46:02 -05:00
|
|
|
}
|
2023-02-28 08:40:51 -05:00
|
|
|
ft_putstr_fd(reason, 2);
|
2023-02-24 06:46:02 -05:00
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
2023-04-05 06:17:16 -04:00
|
|
|
int ft_exit(char **args, int err)
|
2023-02-24 06:46:02 -05:00
|
|
|
{
|
2023-02-28 08:25:20 -05:00
|
|
|
int err;
|
2023-02-24 06:46:02 -05:00
|
|
|
|
|
|
|
if (args[0] == NULL)
|
2023-03-10 08:53:22 -05:00
|
|
|
{
|
|
|
|
write(1, "exit\n", 6);
|
2023-04-05 06:17:16 -04:00
|
|
|
return (err);
|
2023-03-10 08:53:22 -05:00
|
|
|
}
|
2023-02-28 08:28:54 -05:00
|
|
|
err = ft_atoi_check(args[0]);
|
2023-03-09 14:13:38 -05:00
|
|
|
if (err == 1)
|
2023-03-30 09:16:21 -04:00
|
|
|
return (error(err, "exit: numeric argument required", args[0]));
|
2023-02-24 06:46:02 -05:00
|
|
|
if (args[1] != NULL)
|
2023-04-04 16:03:30 -04:00
|
|
|
return (error(-1, "exit: too many arguments", NULL));
|
2023-02-28 08:40:51 -05:00
|
|
|
if (err > 0)
|
2023-03-30 09:16:21 -04:00
|
|
|
return (error(err, "exit: numeric argument required", args[0]));
|
2023-03-10 08:53:22 -05:00
|
|
|
write(1, "exit\n", 6);
|
2023-02-28 08:51:39 -05:00
|
|
|
return ((ft_atoi(args[0]) % 256 + 256) % 256);
|
2023-02-24 06:46:02 -05:00
|
|
|
}
|