-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atol.c
77 lines (69 loc) · 2.17 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atol.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anpayot <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/17 19:30:22 by anpayot #+# #+# */
/* Updated: 2024/10/27 17:24:32 by anpayot ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
long ft_loverflow(int sign)
{
if (sign == 1)
return (LONG_MAX);
return (LONG_MIN);
}
long ft_atol(const char *str)
{
int sign;
long result;
sign = 1;
result = 0;
while (ft_isspace(*str))
str++;
if (*str == '-' || *str == '+')
{
if (*str == '-')
sign = -1;
str++;
}
while (ft_isdigit(*str))
{
if (result > (LONG_MAX - (*str - '0')) / 10)
return (ft_loverflow(sign));
result = result * 10 + (*str - '0');
str++;
}
return (result * sign);
}
/*
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char *str1 = "12345";
const char *str2 = "-98765";
const char *str3 = "0";
const char *str4 = "2147483647"; // LONG_MAX
const char *str5 = "-2147483648"; // LONG_MIN
long num1 = ft_atol(str1);
long num2 = ft_atol(str2);
long num3 = ft_atol(str3);
long num4 = ft_atol(str4);
long num5 = ft_atol(str5);
long orig_num1 = atol(str1);
long orig_num2 = atol(str2);
long orig_num3 = atol(str3);
long orig_num4 = atol(str4);
long orig_num5 = atol(str5);
printf("1:\t%ld\t\tatol_libc:\t%ld\n", num1, orig_num1);
printf("2:\t%ld\t\tatol_libc:\t%ld\n", num2, orig_num2);
printf("3:\t%ld\t\tatol_libc:\t%ld\n", num3, orig_num3);
printf("4:\t%ld\tatol_libc:\t%ld\n", num4, orig_num4);
printf("5:\t%ld\tatol_libc:\t%ld\n", num5, orig_num5);
return 0;
}
*/