1 #include "tunala.h"
2 
3 int int_strtoul(const char *str, unsigned long *val)
4 {
5 #ifdef HAVE_STRTOUL
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;
16 #else
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 		/* We have to multiply 'ret' by 10 before absorbing the next
25 		 * digit. If this will overflow, catch it now. */
26 		if(ret && (((ULONG_MAX + 10) / ret) < 10))
27 			return 0;
28 		ret *= 10;
29 		if(!isdigit(*str))
30 			return 0;
31 		buf[0] = *str;
32 		ret += atoi(buf);
33 		str++;
34 	}
35 	*val = ret;
36 	return 1;
37 #endif
38 }
39 
40 #ifndef HAVE_STRSTR
41 char *int_strstr(const char *haystack, const char *needle)
42 {
43 	const char *sub_haystack = haystack, *sub_needle = needle;
44 	unsigned int offset = 0;
45 	if(!needle)
46 		return haystack;
47 	if(!haystack)
48 		return NULL;
49 	while((*sub_haystack != '\0') && (*sub_needle != '\0')) {
50 		if(sub_haystack[offset] == sub_needle) {
51 			/* sub_haystack is still a candidate */
52 			offset++;
53 			sub_needle++;
54 		} else {
55 			/* sub_haystack is no longer a possibility */
56 			sub_haystack++;
57 			offset = 0;
58 			sub_needle = needle;
59 		}
60 	}
61 	if(*sub_haystack == '\0')
62 		/* Found nothing */
63 		return NULL;
64 	return sub_haystack;
65 }
66 #endif
67