summaryrefslogtreecommitdiff
path: root/libft/ft_atol.c
diff options
context:
space:
mode:
Diffstat (limited to 'libft/ft_atol.c')
-rw-r--r--libft/ft_atol.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/libft/ft_atol.c b/libft/ft_atol.c
new file mode 100644
index 0000000..10d96a9
--- /dev/null
+++ b/libft/ft_atol.c
@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_atol.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: kdx <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2022/07/25 16:53:01 by kdx #+# #+# */
+/* Updated: 2022/10/07 13:25:39 by kdx ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include <limits.h>
+
+t_i64 ft_atol(const char *nptr)
+{
+ t_i64 result;
+ int sign;
+
+ while (*nptr == ' ' || *nptr == '\n' || *nptr == '\t'
+ || *nptr == '\r' || *nptr == '\f' || *nptr == '\v')
+ nptr++;
+ sign = 1;
+ if (*nptr == '+' || *nptr == '-')
+ {
+ if (*nptr == '-')
+ sign = -sign;
+ nptr++;
+ }
+ result = 0;
+ while (*nptr >= '0' && *nptr <= '9')
+ {
+ result = result * 10 + (*nptr - '0') * sign;
+ if (result >= LONG_MAX / 10 || result <= LONG_MIN / 10)
+ return (result);
+ nptr++;
+ }
+ return (result);
+}