From 676b37f963789f3d85b18ed5b3708374971b65e2 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 18 Jan 2020 11:54:47 +0100 Subject: Added ft_getchar and fixing strtol --- src/io/ft_getchar.c | 22 ++++++++++++++++++++++ src/lst/ft_lstreverse_ret.c | 3 ++- src/str/ft_atoi_strict.c | 3 +-- src/str/ft_strtol.c | 12 +++++++----- 4 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 src/io/ft_getchar.c (limited to 'src') diff --git a/src/io/ft_getchar.c b/src/io/ft_getchar.c new file mode 100644 index 0000000..54b44d4 --- /dev/null +++ b/src/io/ft_getchar.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/18 10:29:54 by cacharle #+# #+# */ +/* Updated: 2020/01/18 10:46:56 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char ft_getchar(void) +{ + char c; + + if (read(STDIN_FILENO, &c, 1) < 0) + return (-1); + return (c); +} diff --git a/src/lst/ft_lstreverse_ret.c b/src/lst/ft_lstreverse_ret.c index 03ae98e..caf6ec1 100644 --- a/src/lst/ft_lstreverse_ret.c +++ b/src/lst/ft_lstreverse_ret.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/15 12:51:15 by cacharle #+# #+# */ -/* Updated: 2020/01/15 13:36:46 by cacharle ### ########.fr */ +/* Updated: 2020/01/18 11:52:50 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ t_list *ft_lstreverse_ret(t_list *lst) { t_list *tmp; + if (lst == NULL) return (NULL); if (lst->next == NULL) diff --git a/src/str/ft_atoi_strict.c b/src/str/ft_atoi_strict.c index 6156b03..f12e8db 100644 --- a/src/str/ft_atoi_strict.c +++ b/src/str/ft_atoi_strict.c @@ -6,13 +6,12 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/15 10:06:29 by cacharle #+# #+# */ -/* Updated: 2020/01/15 14:09:03 by cacharle ### ########.fr */ +/* Updated: 2020/01/18 11:53:07 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -#include int ft_strict_atoi(const char *s) { char *end; diff --git a/src/str/ft_strtol.c b/src/str/ft_strtol.c index 0fb5261..7fab7ed 100644 --- a/src/str/ft_strtol.c +++ b/src/str/ft_strtol.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/15 10:26:45 by cacharle #+# #+# */ -/* Updated: 2020/01/15 14:15:40 by cacharle ### ########.fr */ +/* Updated: 2020/01/18 11:52:22 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,17 +57,17 @@ 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) != base_str + base) + while (*str != '\0' && ft_strchr(base_str, *str) != NULL) { nb *= base; nb += ft_strchr(base_str, ft_tolower(*str++)) - base_str; -- cgit