copy on git
This commit is contained in:
commit
42653de246
205 changed files with 7459 additions and 0 deletions
46
libft/ft_long_utils.c
Normal file
46
libft/ft_long_utils.c
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue