chiark / gitweb /
[PATCH] klibc: strlcpy/strlcat - don't alter destination if size == 0
[elogind.git] / klibc / klibc / strcasecmp.c
index e583491b35f8af712d8026870ece455537ba43d0..12aef40d932995136ec6ac280d5e64be62c09f57 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * strcasecmp.c
- *
  */
 
 #include <string.h>
@@ -8,18 +7,17 @@
 
 int strcasecmp(const char *s1, const char *s2)
 {
-       char *n1, *n2;
-       int i, retval;
+  const unsigned char *c1 = s1, *c2 = s2;
+  unsigned char ch;
+  int d = 0;
 
-       n1 = strdup(s1);
-       n2 = strdup(s2);
+  while ( 1 ) {
+    /* 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;
 }