]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/device/subrs.c
xnu-792.24.17.tar.gz
[apple/xnu.git] / osfmk / device / subrs.c
index ad8b9c34f666c9cb0fbb2ae235d32a8a22bbd8ac..3fa97189bfe243bc4875720258d9a3aab6654a8f 100644 (file)
@@ -166,6 +166,49 @@ strncmp(
         return 0;
 }
 
+
+//
+// Lame implementation just for use by strcasecmp/strncasecmp
+//
+static int
+tolower(unsigned char ch)
+{
+    if (ch >= 'A' && ch <= 'Z')
+       ch = 'a' + (ch - 'A');
+
+    return ch;
+}
+
+int
+strcasecmp(const char *s1, const char *s2)
+{
+    const unsigned char *us1 = (const u_char *)s1,
+                 *us2 = (const u_char *)s2;
+
+    while (tolower(*us1) == tolower(*us2++))
+       if (*us1++ == '\0')
+           return (0);
+    return (tolower(*us1) - tolower(*--us2));
+}
+
+int
+strncasecmp(const char *s1, const char *s2, size_t n)
+{
+    if (n != 0) {
+       const unsigned char *us1 = (const u_char *)s1,
+                     *us2 = (const u_char *)s2;
+
+       do {
+           if (tolower(*us1) != tolower(*us2++))
+               return (tolower(*us1) - tolower(*--us2));
+           if (*us1++ == '\0')
+               break;
+       } while (--n != 0);
+    }
+    return (0);
+}
+
+
 /*
  * Abstract:
  *      strcpy copies the contents of the string "from" including
@@ -214,27 +257,6 @@ strncpy(
         return (os1);
 }
 
-
-#if !defined(__alpha)
-
-/*
- * Abstract:
- *      strlen returns the number of characters in "string" preceeding
- *      the terminating null character.
- */
-
-size_t
-strlen(
-        register const char *string)
-{
-        register const char *ret = string;
-
-        while (*string++ != '\0')
-                continue;
-        return string - 1 - ret;
-}
-#endif /* !defined(__alpha) */
-
 /*
  * atoi:
  *
@@ -300,6 +322,28 @@ atoi_term(
         return(f? -n: n);
 }
 
+/*
+ * Does the same thing as strlen, except only looks up
+ * to max chars inside the buffer. 
+ * Taken from archive/kern-stuff/sbf_machine.c in 
+ * seatbelt. 
+ * inputs:
+ *     s       string whose length is to be measured
+ *     max     maximum length of string to search for null
+ * outputs:
+ *     length of s or max; whichever is smaller
+ */
+size_t 
+strnlen(const char *s, size_t max) {
+       const char *es = s + max, *p = s;
+       while(*p && p != es) 
+               p++;
+
+       return p - s;
+}
+
+
+
 /*
  * convert an integer to an ASCII string.
  * inputs: