ft_strace/libft/ft_long_utils.c

46 lines
1.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_long_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/30 10:11:17 by alier #+# #+# */
/* Updated: 2025/02/16 19:03:51 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include <errno.h>
#include "libft.h"
/*
* Reimplementation of libc atol() but sets `errno` to `ERANGE` if an
* domain error is encountered (overflow, underflow) or `EINVAL` if
* no digits characters or invalid characters at end).
*/
long ft_atole(const char *nptr)
{
long n;
long mult;
n = 0;
mult = -1;
while (ft_isspace(*nptr))
nptr++;
if (*nptr == '+' || *nptr == '-')
{
if (*nptr == '-')
mult = 1;
nptr++;
}
if (!ft_isdigit(*nptr))
errno = EINVAL;
while (ft_isdigit(*nptr))
{
n = safe_long_sub(safe_long_mul(n, 10), *nptr - '0');
nptr++;
}
if (*nptr != '\0')
errno = EINVAL;
return (safe_long_mul(n, mult));
}