-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_atoimax.c
More file actions
45 lines (35 loc) · 817 Bytes
/
Copy path_atoimax.c
File metadata and controls
45 lines (35 loc) · 817 Bytes
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
39
40
41
42
43
44
45
#include <ctype.h> /* isdigit */
#include <limits.h> /* INTMAX_MAX */
#include <stddef.h> /* size_t */
#include "util_functions.h"
/**
* _atoimax - converts the initial digits in a string into an integer.
* @s: the string with a number.
*
* Return: the integer in the string `s`.
*/
intmax_t _atoimax(char const *const s)
{
uintmax_t output = 0, max_val = INTMAX_MAX;
size_t i = 0;
unsigned char is_negative = 0;
while (*(s + i) && (*(s + i) == '-' || *(s + i) == '+'))
{
if (*(s + i) == '-')
is_negative = !is_negative;
i++;
}
if (is_negative)
++max_val;
while (*(s + i) && isdigit(*(s + i)) && output < max_val)
{
uintmax_t tmp = (output * 10) + (*(s + i) - '0');
if (tmp > max_val)
break;
output = tmp;
i++;
}
if (is_negative)
return (-output);
return (output);
}