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