]>
Commit | Line | Data |
---|---|---|
3866752e GT |
1 | #include "tunala.h" |
2 | ||
3 | int int_strtoul(const char *str, unsigned long *val) | |
4 | { | |
5 | #ifdef HAVE_STRTOUL | |
40720ce3 MC |
6 | char *tmp; |
7 | unsigned long ret = strtoul(str, &tmp, 10); | |
8 | if ((str == tmp) || (*tmp != '\0')) | |
9 | /* The value didn't parse cleanly */ | |
10 | return 0; | |
11 | if (ret == ULONG_MAX) | |
12 | /* We hit a limit */ | |
13 | return 0; | |
14 | *val = ret; | |
15 | return 1; | |
3866752e | 16 | #else |
40720ce3 MC |
17 | char buf[2]; |
18 | unsigned long ret = 0; | |
19 | buf[1] = '\0'; | |
20 | if (str == '\0') | |
21 | /* An empty string ... */ | |
22 | return 0; | |
23 | while (*str != '\0') { | |
24 | /* | |
25 | * We have to multiply 'ret' by 10 before absorbing the next digit. | |
26 | * If this will overflow, catch it now. | |
27 | */ | |
28 | if (ret && (((ULONG_MAX + 10) / ret) < 10)) | |
29 | return 0; | |
30 | ret *= 10; | |
31 | if (!isdigit(*str)) | |
32 | return 0; | |
33 | buf[0] = *str; | |
34 | ret += atoi(buf); | |
35 | str++; | |
36 | } | |
37 | *val = ret; | |
38 | return 1; | |
3866752e GT |
39 | #endif |
40 | } | |
41 | ||
42 | #ifndef HAVE_STRSTR | |
43 | char *int_strstr(const char *haystack, const char *needle) | |
44 | { | |
40720ce3 MC |
45 | const char *sub_haystack = haystack, *sub_needle = needle; |
46 | unsigned int offset = 0; | |
47 | if (!needle) | |
48 | return haystack; | |
49 | if (!haystack) | |
50 | return NULL; | |
51 | while ((*sub_haystack != '\0') && (*sub_needle != '\0')) { | |
52 | if (sub_haystack[offset] == sub_needle) { | |
53 | /* sub_haystack is still a candidate */ | |
54 | offset++; | |
55 | sub_needle++; | |
56 | } else { | |
57 | /* sub_haystack is no longer a possibility */ | |
58 | sub_haystack++; | |
59 | offset = 0; | |
60 | sub_needle = needle; | |
61 | } | |
62 | } | |
63 | if (*sub_haystack == '\0') | |
64 | /* Found nothing */ | |
65 | return NULL; | |
66 | return sub_haystack; | |
3866752e GT |
67 | } |
68 | #endif |