aboutsummaryrefslogtreecommitdiff
path: root/src/str/ft_atoi_strict.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-01-18 11:54:47 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-04 05:33:59 +0100
commitf078f191515d0fc65340a8722fad14f3de679d7b (patch)
treed6881ef24f6cc062332f3aa17709f693cf783b20 /src/str/ft_atoi_strict.c
parent014d876b691b5c0e84faa98bd308a855269e4ae4 (diff)
downloadlibft-f078f191515d0fc65340a8722fad14f3de679d7b.tar.gz
libft-f078f191515d0fc65340a8722fad14f3de679d7b.tar.bz2
libft-f078f191515d0fc65340a8722fad14f3de679d7b.zip
Added ft_getchar and fixing strtol
Diffstat (limited to 'src/str/ft_atoi_strict.c')
-rw-r--r--src/str/ft_atoi_strict.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/str/ft_atoi_strict.c b/src/str/ft_atoi_strict.c
new file mode 100644
index 0000000..f12e8db
--- /dev/null
+++ b/src/str/ft_atoi_strict.c
@@ -0,0 +1,38 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strict_atoi.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/15 10:06:29 by cacharle #+# #+# */
+/* Updated: 2020/01/18 11:53:07 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int ft_strict_atoi(const char *s)
+{
+ char *end;
+ long ret;
+
+ if (*s != '-' && !ft_isdigit(*s))
+ {
+ errno = EINVAL;
+ return (0);
+ }
+ errno = 0;
+ ret = ft_strtol(s, &end, 10);
+ if (errno == ERANGE || ret > INT_MAX || ret < INT_MIN)
+ {
+ errno = ERANGE;
+ return (0);
+ }
+ if (*end != '\0')
+ {
+ errno = EINVAL;
+ return (0);
+ }
+ return (ret);
+}