-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atol.c
61 lines (55 loc) · 1.52 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
53
54
55
56
57
58
59
60
61
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atol.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lrocigno <lrocigno@student.42sp.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/10 15:53:47 by lrocigno #+# #+# */
/* Updated: 2021/12/06 12:11:24 by lrocigno ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ignorable(char c)
{
char *space_list;
space_list = " \t\r\n\v\f";
if (ft_strchr(space_list, c))
return (1);
return (0);
}
static long long int calc_am(long long int am, int sig, char c)
{
am = (am * 10) + (c - 48);
if (am > LONG_MAX)
{
if (sig < 0)
return (0);
return (-1);
}
return (am);
}
long int ft_atol(const char *str)
{
long long int am;
int sig;
am = 0;
sig = 1;
while (ignorable(*str))
++str;
if (*str == '-')
{
sig = -1;
++str;
}
else if (*str == '+')
++str;
while (ft_isdigit(*str))
{
am = calc_am(am, sig, *str);
++str;
}
if (am != 0 && am != -1)
am *= sig;
return (am);
}