shithub: scc

Download patch

ref: 4975fa36bb99219effb418cceec5382258d9b368
parent: 0922923b6268490b89a24c1b4cc7316a34daea16
author: Quentin Rameau <[email protected]>
date: Sun Dec 23 12:49:05 EST 2018

[libc] strtoul[l]: return input string on parsing error

Setting errno to EINVAL is not specified in c99 (it is in POSIX though).

--- a/src/libc/stdlib/strtoul.c
+++ b/src/libc/stdlib/strtoul.c
@@ -14,6 +14,9 @@
 	static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 	const char *t, *p;
 
+	if (end)
+		*end = s;
+
 	while (isspace(*s))
 		++s;
 
@@ -25,14 +28,11 @@
 	}
 
 	if (base == 0) {
-		if (*s == '0' && toupper(s[1]) == 'X')
-			base = 16;
-		else if (*s == '0')
-			base = 8;
+		if (*s == '0')
+			base = toupper(s[1]) == 'X' ? 16 : 8;
 		else
 			base = 10;
 	}
-
 	if (base == 16 && *s == '0' && toupper(s[1]) == 'X')
 		s += 2;
 
@@ -48,11 +48,9 @@
 		n += d;
 	}
 
-
-	if (end)
+	if (end && t != s)
 		*end = t;
-	if (n == 0 && s == t)
-		errno = EINVAL;
+
 	return n*sign;
 
 overflow:
--- a/src/libc/stdlib/strtoull.c
+++ b/src/libc/stdlib/strtoull.c
@@ -14,6 +14,9 @@
 	static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 	const char *t, *p;
 
+	if (end)
+		*end = s;
+
 	while (isspace(*s))
 		++s;
 
@@ -25,14 +28,11 @@
 	}
 
 	if (base == 0) {
-		if (*s == '0' && toupper(s[1]) == 'X')
-			base = 16;
-		else if (*s == '0')
-			base = 8;
+		if (*s == '0')
+			base = toupper(s[1]) == 'X' ? 16 : 8;
 		else
 			base = 10;
 	}
-
 	if (base == 16 && *s == '0' && toupper(s[1]) == 'X')
 		s += 2;
 
@@ -48,11 +48,9 @@
 		n += d;
 	}
 
-
-	if (end)
+	if (end && t != s)
 		*end = t;
-	if (n == 0 && s == t)
-		errno = EINVAL;
+
 	return n*sign;
 
 overflow: