aboutsummaryrefslogtreecommitdiff
path: root/src/str/ft_atoi_strict.c
blob: f573593dd9c1b6f4d6fe6582d6ac403f66774e83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_strict_atoi.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/01/15 10:06:29 by cacharle          #+#    #+#             */
/*   Updated: 2020/02/10 02:20:40 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);
}