-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atol.c
52 lines (47 loc) · 1.62 KB
/
ft_atol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atol.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yshimoda <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/19 19:14:41 by yshimoda #+# #+# */
/* Updated: 2022/10/19 19:40:31 by yshimoda ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_check_over(int sign, long ans, char c)
{
long tmp;
tmp = LONG_MAX / 10;
if (sign == 1)
c++;
if (tmp < ans || (tmp == ans && LONG_MAX % 10 + 1 < c - '0'))
return (1);
return (0);
}
long ft_atol(const char *str)
{
int sign;
long total;
sign = 1;
total = 0;
while (*str == '\t' || *str == '\n' || *str == '\v'
|| *str == '\f' || *str == '\r' || *str == ' ')
str++;
if (*str == '-')
sign *= -1;
if (*str == '-' || *str == '+')
str++;
while ('0' <= *str && *str <= '9')
{
if (sign == 1 && ft_check_over(sign, total, *str))
return (LONG_MAX);
if (sign == -1 && ft_check_over(sign, total, *str))
return (LONG_MIN);
total = total * 10 + *str - '0';
str++;
}
total *= sign;
return (total);
}