From 4bd71a3c895438d9e85930dc29d6000d8ecb59b4 Mon Sep 17 00:00:00 2001
From: "Andrey A. Chernov" <ache@FreeBSD.org>
Date: Sun, 2 Dec 2001 09:15:54 +0000
Subject: [PATCH] Make it works for non ASCII compatible encodings too. The
 only assumption left is that 'A'..'Z' 'a'..'z' both are contiguous

---
 lib/libc/stdlib/strtoimax.c | 14 +++++++-------
 lib/libc/stdlib/strtol.c    | 14 +++++++-------
 lib/libc/stdlib/strtoll.c   | 14 +++++++-------
 lib/libc/stdlib/strtoul.c   | 14 +++++++-------
 lib/libc/stdlib/strtoull.c  | 14 +++++++-------
 lib/libc/stdlib/strtoumax.c | 14 +++++++-------
 6 files changed, 42 insertions(+), 42 deletions(-)

diff --git a/lib/libc/stdlib/strtoimax.c b/lib/libc/stdlib/strtoimax.c
index f8ff09bf3cb0..958e72ae2b43 100644
--- a/lib/libc/stdlib/strtoimax.c
+++ b/lib/libc/stdlib/strtoimax.c
@@ -58,7 +58,7 @@ strtoimax(nptr, endptr, base)
 	uintmax_t acc;
 	unsigned char c;
 	uintmax_t cutoff;
-	int neg, any, cutlim;
+	int neg, any, cutlim, n;
 
 	/*
 	 * Skip white space and pick up leading +/- sign if any.
@@ -113,19 +113,19 @@ strtoimax(nptr, endptr, base)
 	cutoff /= base;
 	for ( ; ; c = *s++) {
 		if (isxdigit(c))
-			c = digittoint(c);
-		else if (isascii(c) && isalpha(c))
-			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
+			n = digittoint(c);
+		else if (isalpha(c))
+			n = (char)c - (isupper(c) ? 'A' - 10 : 'a' - 10);
 		else
 			break;
-		if (c >= base)
+		if (n < 0 || n >= base)
 			break;
-		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
+		if (any < 0 || acc > cutoff || (acc == cutoff && n > cutlim))
 			any = -1;
 		else {
 			any = 1;
 			acc *= base;
-			acc += c;
+			acc += n;
 		}
 	}
 	if (any < 0) {
diff --git a/lib/libc/stdlib/strtol.c b/lib/libc/stdlib/strtol.c
index 3d196930376a..449f3c595019 100644
--- a/lib/libc/stdlib/strtol.c
+++ b/lib/libc/stdlib/strtol.c
@@ -59,7 +59,7 @@ strtol(nptr, endptr, base)
 	unsigned long acc;
 	unsigned char c;
 	unsigned long cutoff;
-	int neg, any, cutlim;
+	int neg, any, cutlim, n;
 
 	/*
 	 * Skip white space and pick up leading +/- sign if any.
@@ -113,19 +113,19 @@ strtol(nptr, endptr, base)
 	cutoff /= base;
 	for ( ; ; c = *s++) {
 		if (isxdigit(c))
-			c = digittoint(c);
-		else if (isascii(c) && isalpha(c))
-			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
+			n = digittoint(c);
+		else if (isalpha(c))
+			n = (char)c - (isupper(c) ? 'A' - 10 : 'a' - 10);
 		else
 			break;
-		if (c >= base)
+		if (n < 0 || n >= base)
 			break;
-		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
+		if (any < 0 || acc > cutoff || (acc == cutoff && n > cutlim))
 			any = -1;
 		else {
 			any = 1;
 			acc *= base;
-			acc += c;
+			acc += n;
 		}
 	}
 	if (any < 0) {
diff --git a/lib/libc/stdlib/strtoll.c b/lib/libc/stdlib/strtoll.c
index 1fd7d9cccb3e..75131faa5ea6 100644
--- a/lib/libc/stdlib/strtoll.c
+++ b/lib/libc/stdlib/strtoll.c
@@ -58,7 +58,7 @@ strtoll(nptr, endptr, base)
 	unsigned long long acc;
 	unsigned char c;
 	unsigned long long cutoff;
-	int neg, any, cutlim;
+	int neg, any, cutlim, n;
 
 	/*
 	 * Skip white space and pick up leading +/- sign if any.
@@ -113,19 +113,19 @@ strtoll(nptr, endptr, base)
 	cutoff /= base;
 	for ( ; ; c = *s++) {
 		if (isxdigit(c))
-			c = digittoint(c);
-		else if (isascii(c) && isalpha(c))
-			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
+			n = digittoint(c);
+		else if (isalpha(c))
+			n = (char)c - (isupper(c) ? 'A' - 10 : 'a' - 10);
 		else
 			break;
-		if (c >= base)
+		if (n < 0 || n >= base)
 			break;
-		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
+		if (any < 0 || acc > cutoff || (acc == cutoff && n > cutlim))
 			any = -1;
 		else {
 			any = 1;
 			acc *= base;
-			acc += c;
+			acc += n;
 		}
 	}
 	if (any < 0) {
diff --git a/lib/libc/stdlib/strtoul.c b/lib/libc/stdlib/strtoul.c
index e431dc87670a..1fd800a04a63 100644
--- a/lib/libc/stdlib/strtoul.c
+++ b/lib/libc/stdlib/strtoul.c
@@ -58,7 +58,7 @@ strtoul(nptr, endptr, base)
 	unsigned long acc;
 	unsigned char c;
 	unsigned long cutoff;
-	int neg, any, cutlim;
+	int neg, any, cutlim, n;
 
 	/*
 	 * See strtol for comments as to the logic used.
@@ -91,19 +91,19 @@ strtoul(nptr, endptr, base)
 	cutlim = ULONG_MAX % base;
 	for ( ; ; c = *s++) {
 		if (isxdigit(c))
-			c = digittoint(c);
-		else if (isascii(c) && isalpha(c))
-			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
+			n = digittoint(c);
+		else if (isalpha(c))
+			n = (char)c - (isupper(c) ? 'A' - 10 : 'a' - 10);
 		else
 			break;
-		if (c >= base)
+		if (n < 0 || n >= base)
 			break;
-		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
+		if (any < 0 || acc > cutoff || (acc == cutoff && n > cutlim))
 			any = -1;
 		else {
 			any = 1;
 			acc *= base;
-			acc += c;
+			acc += n;
 		}
 	}
 	if (any < 0) {
diff --git a/lib/libc/stdlib/strtoull.c b/lib/libc/stdlib/strtoull.c
index adb655681da9..d9e3e4fd8cfb 100644
--- a/lib/libc/stdlib/strtoull.c
+++ b/lib/libc/stdlib/strtoull.c
@@ -58,7 +58,7 @@ strtoull(nptr, endptr, base)
 	unsigned long long acc;
 	unsigned char c;
 	unsigned long long cutoff;
-	int neg, any, cutlim;
+	int neg, any, cutlim, n;
 
 	/*
 	 * See strtoq for comments as to the logic used.
@@ -91,19 +91,19 @@ strtoull(nptr, endptr, base)
 	cutlim = ULLONG_MAX % base;
 	for ( ; ; c = *s++) {
 		if (isxdigit(c))
-			c = digittoint(c);
-		else if (isascii(c) && isalpha(c))
-			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
+			n = digittoint(c);
+		else if (isalpha(c))
+			n = (char)c - (isupper(c) ? 'A' - 10 : 'a' - 10);
 		else
 			break;
-		if (c >= base)
+		if (n < 0 || n >= base)
 			break;
-		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
+		if (any < 0 || acc > cutoff || (acc == cutoff && n > cutlim))
 			any = -1;
 		else {
 			any = 1;
 			acc *= base;
-			acc += c;
+			acc += n;
 		}
 	}
 	if (any < 0) {
diff --git a/lib/libc/stdlib/strtoumax.c b/lib/libc/stdlib/strtoumax.c
index ea592459b4b4..a2da67878f3b 100644
--- a/lib/libc/stdlib/strtoumax.c
+++ b/lib/libc/stdlib/strtoumax.c
@@ -58,7 +58,7 @@ strtoumax(nptr, endptr, base)
 	uintmax_t acc;
 	unsigned char c;
 	uintmax_t cutoff;
-	int neg, any, cutlim;
+	int neg, any, cutlim, n;
 
 	/*
 	 * See strtoimax for comments as to the logic used.
@@ -91,19 +91,19 @@ strtoumax(nptr, endptr, base)
 	cutlim = UINTMAX_MAX % base;
 	for ( ; ; c = *s++) {
 		if (isxdigit(c))
-			c = digittoint(c);
-		else if (isascii(c) && isalpha(c))
-			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
+			n = digittoint(c);
+		else if (isalpha(c))
+			n = (char)c - (isupper(c) ? 'A' - 10 : 'a' - 10);
 		else
 			break;
-		if (c >= base)
+		if (n < 0 || n >= base)
 			break;
-		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
+		if (any < 0 || acc > cutoff || (acc == cutoff && n > cutlim))
 			any = -1;
 		else {
 			any = 1;
 			acc *= base;
-			acc += c;
+			acc += n;
 		}
 	}
 	if (any < 0) {