aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/str/ft_strtol.c29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/str/ft_strtol.c b/src/str/ft_strtol.c
index 7e4cb30..499e54c 100644
--- a/src/str/ft_strtol.c
+++ b/src/str/ft_strtol.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/15 10:26:45 by cacharle #+# #+# */
-/* Updated: 2020/10/11 14:03:49 by cacharle ### ########.fr */
+/* Updated: 2020/10/11 14:32:46 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -39,19 +39,27 @@ static int st_strtol_handle_base(int base, const char **str)
return (10);
}
-static long st_errno_return(int err, long ret)
+static void st_setup_base(char base_str[37], int base)
{
+ ft_strncpy(base_str, STRTOL_STD_BASE, base);
+ base_str[base] = '\0';
+}
+
+static long st_return(int err, char **end, long ret)
+{
+ if (end != NULL)
+ *end = NULL;
errno = err;
return (ret);
}
/*
-** If there is no digits doesn't put str in endptr like the original,
+** If there is no digits doesn't put str in end like the original,
** instead it puts the address of the char after spaces and sign.
** Too much lines and annoyance, I can't be bothered.
*/
-long ft_strtol(const char *str, char **endptr, int base)
+long ft_strtol(const char *str, char **end, int base)
{
t_ftbool is_negative;
unsigned long long nb;
@@ -63,18 +71,19 @@ long ft_strtol(const char *str, char **endptr, int base)
if (*str == '-' || *str == '+')
str++;
if ((base = st_strtol_handle_base(base, &str)) == -1)
- return (st_errno_return(EINVAL, 0));
- ft_strncpy(base_str, STRTOL_STD_BASE, base);
- base_str[base] = '\0';
+ return (st_return(EINVAL, end, 0));
+ st_setup_base(base_str, base);
nb = 0;
while (*str != '\0' && ft_strchr(base_str, *str) != NULL)
{
nb *= base;
if (is_negative ? (nb > -(unsigned long)LONG_MIN) : (nb > LONG_MAX))
- return (st_errno_return(ERANGE, is_negative ? LONG_MIN : LONG_MAX));
+ return (st_return(ERANGE, end, is_negative ? LONG_MIN : LONG_MAX));
nb += ft_strchr(base_str, ft_tolower(*str++)) - base_str;
+ if (is_negative ? (nb > -(unsigned long)LONG_MIN) : (nb > LONG_MAX))
+ return (st_return(ERANGE, end, is_negative ? LONG_MIN : LONG_MAX));
}
- if (endptr != NULL)
- *endptr = (char*)str;
+ if (end != NULL)
+ *end = (char*)str;
return (is_negative ? -nb : nb);
}