-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atol.c
47 lines (42 loc) · 1.45 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atol.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgata-va <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/16 18:23:02 by fgata-va #+# #+# */
/* Updated: 2021/03/09 11:11:50 by fgata-va ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static long ft_save_long(int i, const char *str, int negative)
{
long long nbr;
nbr = 0;
while (ft_isdigit(str[i]))
{
nbr = (nbr * 10) + (str[i] - '0');
if ((nbr > 9223372036854775807 || nbr < -9223372036854775807)
&& negative > 0)
return (9223372036854775807);
i++;
}
return (nbr * negative);
}
long ft_atol(const char *str)
{
int i;
int negative;
i = 0;
negative = 1;
while (ft_strchr("\t\n\v\f\r ", str[i]))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i])
negative *= -1;
i++;
}
return (ft_save_long(i, str, negative));
}