diff options
Diffstat (limited to 'ft_strtol.c')
| -rw-r--r-- | ft_strtol.c | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/ft_strtol.c b/ft_strtol.c index 447f1af..94fc114 100644 --- a/ft_strtol.c +++ b/ft_strtol.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/15 10:26:45 by cacharle #+# #+# */ -/* Updated: 2020/01/15 11:39:34 by cacharle ### ########.fr */ +/* Updated: 2020/02/04 04:40:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,8 @@ static int strtol_handle_base(int base, const char **str) { + if (base > 36) + return (-1); if (base != 16 && base != 0) return (base); if (base == 16 && **str == '0' && (*str)[1] == 'x') @@ -55,20 +57,20 @@ long ft_strtol(const char *str, char **endptr, int base) long long nb; char base_str[37]; - if (base > 36) - return (errno_return(EINVAL)); while (ft_isspace(*str)) str++; is_negative = *str == '-' ? TRUE : FALSE; if (*str == '-' || *str == '+') str++; - base = strtol_handle_base(base, &str); + if ((base = strtol_handle_base(base, &str)) == -1) + return (errno_return(EINVAL)); ft_strncpy(base_str, STRTOL_STD_BASE, base); + base_str[base] = '\0'; nb = 0; - while (ft_strchr(base_str, *str) != NULL) + while (*str != '\0' && ft_strchr(base_str, *str) != NULL) { nb *= base; - nb += ft_strchr(base_str, ft_tolower(*str)) - base_str; + nb += ft_strchr(base_str, ft_tolower(*str++)) - base_str; if (((long)nb ^ (long)(nb / base)) < 0) return (errno_return(ERANGE)); } |
