Browse Source

libc: fix bad strncmp

K. Lange 4 years ago
parent
commit
e492f967c0
1 changed files with 5 additions and 6 deletions
  1. 5 6
      libc/string/strncmp.c

+ 5 - 6
libc/string/strncmp.c

@@ -1,13 +1,12 @@
 #include <string.h>
 
 int strncmp(const char *s1, const char *s2, size_t n) {
-	size_t i = 0;
-	while (i < n && *s1 && *s2) {
-		if (*s1 < *s2) return -1;
-		if (*s1 > *s2) return 1;
+	if (n == 0) return 0;
+
+	while (n-- && *s1 == *s2) {
+		if (!n || !*s1) break;
 		s1++;
 		s2++;
-		i++;
 	}
-	return 0;
+	return (*(unsigned char *)s1) - (*(unsigned char *)s2);
 }