This commit is contained in:
Loic Deridder
2025-01-21 13:42:32 +01:00
parent 563349c2f8
commit 9dbc3f75e8
8 changed files with 149 additions and 79 deletions

View File

@@ -19,6 +19,7 @@
# include <unistd.h>
# include <limits.h>
# include <stdarg.h>
# include <errno.h>
# include "colors.h"
# include "general.h"
@@ -53,6 +54,7 @@ int ft_toupper(int c);
int ft_tolower(int c);
int ft_atoi(const char *str);
long ft_atol(const char *str);
char *ft_itoa(int n);
void *ft_calloc(size_t count, size_t size);

View File

@@ -0,0 +1,48 @@
#include "../../libft.h"
static int is_signed(char c, int *sign)
{
if (c == '+' || c == '-')
{
if (c == '-')
*sign *= -1;
return (1);
}
return (0);
}
long ft_atol(const char *str)
{
long res;
int sign;
res = 0;
sign = 1;
errno = 0;
while (*str && ((*str >= 9 && *str <= 13) || *str == 32))
str++;
if (is_signed(*str, &sign))
str++;
while (*str >= '0' && *str <= '9')
{
if (((sign > 0) && res >= (LONG_MAX / 10) &&
(*str - '0' > LONG_MAX % 10))
|| (sign == -1 && res >= LONG_MAX / 10 && *str - '0' > LONG_MAX % 10 + 1))
{
errno = ERANGE;
break;
}
res = (res * 10) + (*str - '0');
str++;
}
return (res * sign);
}
//
// #include <stdio.h>
//
// int main(int ac, char **av)
// {
// printf("res: %ld\n", ft_atol(av[1]));
// printf("errno: %d\n", errno);
//
// }