Added src/libconfig so distribution
[collectd.git] / src / libconfig / strtoll.c
1 #include <sys/types.h>
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <ctype.h>
5 #include <stdio.h>
6
7 /* We only handle base 10. */
8 long long int strtoll(const char *nptr, char **endptr, int base) {
9         long long int retval = 0;
10         const char **endptrd = (const char **) endptr;
11         const char *idx = NULL;
12         int allowspace = 1;
13
14         idx = nptr;
15         while (1) {
16                 if (*idx == '\0') {
17                         break;
18                 }
19
20                 if (!isdigit(*idx)) {
21                         if (*idx == '-') {
22                                 retval *= -1;
23                                 continue;
24                         }
25                         if ((*idx == ' ' || *idx == '\t') && allowspace) {
26                                 continue;
27                         }
28                         break;
29                 }
30
31                 retval *= 10;
32                 retval += (*idx - '0');
33
34                 allowspace = 0;
35                 idx++;
36         }
37
38         if (endptrd != NULL) {
39                 *endptrd = idx;
40         }
41
42         return(retval);
43 }