minishell/libft/ft_printf_atoi.c

35 lines
1.3 KiB
C
Raw Normal View History

2025-02-12 14:51:05 +01:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/01 13:45:04 by jguelen #+# #+# */
/* Updated: 2024/12/01 17:16:53 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*Does not deal with the isspace(3) characters.
* @RETURN a long castable into an int or LONG_MAX if overflow has
* occurred in the positive.
*/
long ft_printf_atoi(char *str, char **current)
{
long calc;
calc = 0;
while (*str && ft_isdigit(*str))
{
if ((calc > (INT_MAX / 10))
|| ((calc == (INT_MAX / 10)) && ((INT_MAX % 10) < (*str - '0'))))
return (LONG_MAX);
calc = calc * 10 + (*str - '0');
str++;
}
*current = str;
return (calc);
}