Restored exit but adding isatty
This commit is contained in:
@@ -11,98 +11,34 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "builtins.h"
|
#include "builtins.h"
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
static uint8_t get_uint8_from_num(const char *arg, uint8_t *status);
|
u_int8_t builtin_exit(
|
||||||
static uint8_t has_overflow(
|
|
||||||
uint64_t n,
|
|
||||||
char digit,
|
|
||||||
uint64_t limit
|
|
||||||
);
|
|
||||||
static uint8_t resolve_exit_status(
|
|
||||||
t_command cmd,
|
|
||||||
t_minishell *msh,
|
|
||||||
uint8_t *exit_status
|
|
||||||
);
|
|
||||||
|
|
||||||
uint8_t builtin_exit(
|
|
||||||
t_command cmd,
|
t_command cmd,
|
||||||
t_minishell *msh
|
t_minishell *msh
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
uint8_t exit_status;
|
|
||||||
|
|
||||||
if (isatty(STDIN_FILENO))
|
if (isatty(STDIN_FILENO))
|
||||||
ft_eputendl("exit");
|
ft_eputendl("exit");
|
||||||
if (!resolve_exit_status(cmd, msh, &exit_status))
|
|
||||||
return (msh->exit_status);
|
|
||||||
msh->exit = true;
|
|
||||||
msh->exit_status = exit_status;
|
|
||||||
return (exit_status);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t resolve_exit_status(
|
|
||||||
t_command cmd,
|
|
||||||
t_minishell *msh,
|
|
||||||
uint8_t *exit_status
|
|
||||||
){
|
|
||||||
if (cmd.argc == 1)
|
if (cmd.argc == 1)
|
||||||
*exit_status = msh->exit_status;
|
|
||||||
else if (!get_uint8_from_num(cmd.argv[1], exit_status))
|
|
||||||
{
|
{
|
||||||
ft_eprintf("minishell: exit: %s: numeric argument required\n",
|
msh->exit = 1;
|
||||||
cmd.argv[1]);
|
return (msh->exit_status);
|
||||||
msh->exit = true;
|
}
|
||||||
msh->exit_status = 2;
|
else if (!ft_strisnum(cmd.argv[1]))
|
||||||
return (0);
|
{
|
||||||
|
ft_eputstr("exit: ");
|
||||||
|
ft_eputendl(cmd.argv[1]);
|
||||||
|
ft_eputendl(": numeric argument required");
|
||||||
|
return (2);
|
||||||
}
|
}
|
||||||
else if (cmd.argc > 2)
|
else if (cmd.argc > 2)
|
||||||
{
|
{
|
||||||
ft_eputendl("minishell: exit: too many arguments");
|
ft_eputendl("exit: too many arguments");
|
||||||
msh->exit_status = EXIT_FAILURE;
|
return (2);
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
return (1);
|
else
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t get_uint8_from_num(
|
|
||||||
const char *arg,
|
|
||||||
uint8_t *status
|
|
||||||
){
|
|
||||||
uint64_t n;
|
|
||||||
uint64_t limit;
|
|
||||||
int sign;
|
|
||||||
|
|
||||||
if (arg == NULL || *arg == '\0')
|
|
||||||
return (0);
|
|
||||||
n = 0;
|
|
||||||
sign = 1;
|
|
||||||
if (*arg == '+' || *arg == '-')
|
|
||||||
if (*arg++ == '-')
|
|
||||||
sign = -1;
|
|
||||||
if (*arg == '\0')
|
|
||||||
return (0);
|
|
||||||
limit = LONG_MAX;
|
|
||||||
if (sign == -1)
|
|
||||||
limit = (uint64_t)LONG_MAX + 1;
|
|
||||||
while (*arg != '\0')
|
|
||||||
{
|
{
|
||||||
if (!ft_isdigit(*arg) || has_overflow(n, *arg, limit))
|
msh->exit = 1;
|
||||||
return (0);
|
return ((u_int8_t)ft_atol(cmd.argv[1]));
|
||||||
n = (n * 10) + (*arg++ - '0');
|
|
||||||
}
|
}
|
||||||
*status = (uint8_t)(n * sign);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t has_overflow(
|
|
||||||
uint64_t n,
|
|
||||||
char digit,
|
|
||||||
uint64_t limit
|
|
||||||
){
|
|
||||||
if (n > (limit / 10))
|
|
||||||
return (1);
|
|
||||||
if (n == (limit / 10) && (uint64_t)(digit - '0') > (limit % 10))
|
|
||||||
return (1);
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user