summaryrefslogtreecommitdiff
path: root/missing/strtol.c
blob: e94aa54ca0e0dece73c974d9ea7d2e4b8cbe8d5a (plain)
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
/* public domain rewrite of strtol(3) */

#include <ctype.h>

long
strtol(nptr, endptr, base)
    char *nptr;
    char **endptr;
    int base;
{
    long result;
    char *p = nptr;

    while (isspace(*p)) {
	p++;
    }
    if (*p == '-') {
	p++;
	result = -strtoul(p, endptr, base);
    }
    else {
	if (*p == '+') p++;
	result = strtoul(p, endptr, base);
    }
    if (endptr != 0 && *endptr == p) {
	*endptr = nptr;
    }
    return result;
}