Files

74 lines
1.5 KiB
C
Raw Permalink Normal View History

2020-07-16 14:02:51 +01:00
#include <ctype.h>
#include <stdint.h>
#include <limits.h>
long strtol(const char *nptr, char **endptr, int base)
{
const char *s = nptr;
unsigned long acc;
int c;
unsigned long cutoff;
int neg = 0, any, cutlim;
do {
c = *s++;
} while (isspace(c));
if (c == '-') {
neg = 1;
c = *s++;
} else if (c == '+') {
2020-07-16 14:02:51 +01:00
c = *s++;
} if ((base == 0 || base == 16) &&
2020-07-16 14:02:51 +01:00
c == '0' && (*s == 'x' || *s == 'X')) {
c = s[1];
s += 2;
base = 16;
} else if ((base == 0 || base == 2) &&
c == '0' && (*s == 'b' || *s == 'B')) {
c = s[1];
s += 2;
base = 2;
}
if (base == 0) {
2020-07-16 14:02:51 +01:00
base = c == '0' ? 8 : 10;
}
2020-07-16 14:02:51 +01:00
cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
cutlim = cutoff % (unsigned long)base;
cutoff /= (unsigned long)base;
2020-07-16 14:02:51 +01:00
for (acc = 0, any = 0;; c = *s++) {
if (isdigit(c)) {
2020-07-16 14:02:51 +01:00
c -= '0';
} else if (isalpha(c)) {
2020-07-16 14:02:51 +01:00
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
} else {
2020-07-16 14:02:51 +01:00
break;
}
if (c >= base) {
2020-07-16 14:02:51 +01:00
break;
} if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) {
2020-07-16 14:02:51 +01:00
any = -1;
} else {
2020-07-16 14:02:51 +01:00
any = 1;
acc *= base;
acc += c;
}
}
2020-07-16 14:02:51 +01:00
if (any < 0) {
acc = neg ? LONG_MIN : LONG_MAX;
} else if (neg) {
2020-07-16 14:02:51 +01:00
acc = -acc;
}
if (endptr != 0) {
2020-07-16 14:02:51 +01:00
*endptr = (char *)(any ? s - 1 : nptr);
}
return acc;
2020-07-16 14:02:51 +01:00
}