forked from regehr/str2long_contest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdavec_2.c
More file actions
40 lines (32 loc) · 696 Bytes
/
davec_2.c
File metadata and controls
40 lines (32 loc) · 696 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
#include "str2long.h"
long str2long_davec_2(const char *str) {
int i = 0;
long n = 0, last_n = 0, m = 1;
char *p= (char *)str, c;
if (!p[0]) { goto err; }
if (p[0] == '-') {
p++;
m = -1;
if (!p[0]) {
goto err;
}
}
while((c = *p++)) {
if (c >= '0' && c <= '9') {
last_n = n;
if (i > 0) { n *= 10; }
n += m * (c - '0');
} else {
goto err;
}
/* detect overflow */
if (n && ((m == -1 && n > last_n) || (m == 1 && n < last_n))) {
goto err;
}
i++;
}
return n;
err:
error = 1;
return 0;
}