chiark / gitweb /
[PATCH] sync klibc with release 0.95
[elogind.git] / klibc / klibc / strncasecmp.c
index 542493de0f83e541c5a623ec70a5e51413b31239..3309d1a7fe6b4074f3285b6d4a764007ab8b9085 100644 (file)
@@ -7,18 +7,17 @@
 
 int strncasecmp(const char *s1, const char *s2, size_t n)
 {
-       char *n1, *n2;
-       int i, retval;
+  const unsigned char *c1 = s1, *c2 = s2;
+  unsigned char ch;
+  int d = 0;
 
-       n1 = strndup(s1, n);
-       n2 = strndup(s2, n);
+  while ( n-- ) {
+    /* toupper() expects an unsigned char (implicitly cast to int)
+       as input, and returns an int, which is exactly what we want. */
+    d = toupper(ch = *c1++) - toupper(*c2++);
+    if ( d || !ch )
+      break;
+  }
 
-       for (i = 0; i < strlen(n1); i++)
-               n1[i] = toupper(n1[i]);
-       for (i = 0; i < strlen(n2); i++)
-               n2[i] = toupper(n2[i]);
-       retval = strcmp(n1, n2);
-       free(n1);
-       free(n2);
-       return retval;
+  return d;
 }