42_minishell/utils/ft_atoi_check.c
Etienne Rey-bethbeder 0bf045d4ab KAN RP SEX CHATGPT SUR
2023-04-04 13:46:32 +02:00

51 lines
1.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/21 08:21:05 by erey-bet #+# #+# */
/* Updated: 2023/04/04 13:40:43 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.h"
static int ft_isspace(char c)
{
if (c == ' ' || c == '\f'
||c == '\n' || c == '\r' || c == '\t' || c == '\v')
return (1);
return (0);
}
int ft_atoi_check(const char *nptr)
{
int64_t result;
int sign;
while (ft_isspace(*nptr))
nptr++;
sign = 1;
if (*nptr == '+' || *nptr == '-')
{
if (*nptr == '-')
sign = -1;
nptr++;
}
result = 0;
while (*nptr >= '0' && *nptr <= '9')
{
result = result * 10 + (*nptr++ - '0') * sign;
if ((result < 0 && sign == 1)
|| (result > 0 && sign == -1))
return (2);
}
if (*nptr--)
return (1);
if (*nptr == '-' || *nptr == '+')
return (1);
return (0);
}