forked from regehr/str2long_contest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgedare_2.c
More file actions
40 lines (37 loc) · 765 Bytes
/
gedare_2.c
File metadata and controls
40 lines (37 loc) · 765 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
#include "str2long.h"
long str2long_gedare_2 (const char *input)
{
long rv;
char t;
if (input) {
if (*input == 0) {
error = 1;
} else {
rv = 0;
if (*input == '-') {
if (!*++input) {
error = 1;
} else {
do {
t = *input - '0';
if ( rv < LONG_MIN/10L || rv*10L < LONG_MIN + t ) {
error = 1;
break;
}
rv = rv * 10L - t;
} while (*++input);
}
} else {
do {
t = *input - '0';
if ( rv > LONG_MAX/10L || rv*10L > LONG_MAX - t ) {
error = 1;
break;
}
rv = rv * 10L + t;
} while (*++input);
}
}
}
return rv;
}