-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
executable file
·49 lines (44 loc) · 1.49 KB
/
ft_atoi.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tseguier <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/09/13 03:27:42 by tseguier #+# #+# */
/* Updated: 2014/03/27 18:37:46 by jcoignet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <limits.h>
static int ft_isspace(int c)
{
return ((c >= 9 && c <= 13) || c == 32);
}
long ft_atol(const char *str)
{
long number;
long sign;
number = 0;
if (ft_strncmp(str, "-9223372036854775808", 20) == 0
&& (str[20] < '0' || str[20] > '9'))
return (LONG_MIN);
while (ft_isspace(*str))
++str;
sign = (*str == '-') ? -1 : 1;
if (*str == '-' || *str == '+')
++str;
while (*str >= '0' && *str <= '9')
{
number *= 10;
number += (long)*str - 48;
++str;
if (number < 0)
return (-1);
}
return (number * sign);
}
int ft_atoi(const char *str)
{
return ((int)ft_atol(str));
}