1109998Smarkm#include "tunala.h"
2109998Smarkm
3109998Smarkmint int_strtoul(const char *str, unsigned long *val)
4109998Smarkm{
5109998Smarkm#ifdef HAVE_STRTOUL
6296465Sdelphij    char *tmp;
7296465Sdelphij    unsigned long ret = strtoul(str, &tmp, 10);
8296465Sdelphij    if ((str == tmp) || (*tmp != '\0'))
9296465Sdelphij        /* The value didn't parse cleanly */
10296465Sdelphij        return 0;
11296465Sdelphij    if (ret == ULONG_MAX)
12296465Sdelphij        /* We hit a limit */
13296465Sdelphij        return 0;
14296465Sdelphij    *val = ret;
15296465Sdelphij    return 1;
16109998Smarkm#else
17296465Sdelphij    char buf[2];
18296465Sdelphij    unsigned long ret = 0;
19296465Sdelphij    buf[1] = '\0';
20296465Sdelphij    if (str == '\0')
21296465Sdelphij        /* An empty string ... */
22296465Sdelphij        return 0;
23296465Sdelphij    while (*str != '\0') {
24296465Sdelphij        /*
25296465Sdelphij         * We have to multiply 'ret' by 10 before absorbing the next digit.
26296465Sdelphij         * If this will overflow, catch it now.
27296465Sdelphij         */
28296465Sdelphij        if (ret && (((ULONG_MAX + 10) / ret) < 10))
29296465Sdelphij            return 0;
30296465Sdelphij        ret *= 10;
31296465Sdelphij        if (!isdigit(*str))
32296465Sdelphij            return 0;
33296465Sdelphij        buf[0] = *str;
34296465Sdelphij        ret += atoi(buf);
35296465Sdelphij        str++;
36296465Sdelphij    }
37296465Sdelphij    *val = ret;
38296465Sdelphij    return 1;
39109998Smarkm#endif
40109998Smarkm}
41109998Smarkm
42109998Smarkm#ifndef HAVE_STRSTR
43109998Smarkmchar *int_strstr(const char *haystack, const char *needle)
44109998Smarkm{
45296465Sdelphij    const char *sub_haystack = haystack, *sub_needle = needle;
46296465Sdelphij    unsigned int offset = 0;
47296465Sdelphij    if (!needle)
48296465Sdelphij        return haystack;
49296465Sdelphij    if (!haystack)
50296465Sdelphij        return NULL;
51296465Sdelphij    while ((*sub_haystack != '\0') && (*sub_needle != '\0')) {
52296465Sdelphij        if (sub_haystack[offset] == sub_needle) {
53296465Sdelphij            /* sub_haystack is still a candidate */
54296465Sdelphij            offset++;
55296465Sdelphij            sub_needle++;
56296465Sdelphij        } else {
57296465Sdelphij            /* sub_haystack is no longer a possibility */
58296465Sdelphij            sub_haystack++;
59296465Sdelphij            offset = 0;
60296465Sdelphij            sub_needle = needle;
61296465Sdelphij        }
62296465Sdelphij    }
63296465Sdelphij    if (*sub_haystack == '\0')
64296465Sdelphij        /* Found nothing */
65296465Sdelphij        return NULL;
66296465Sdelphij    return sub_haystack;
67109998Smarkm}
68109998Smarkm#endif
69