]> jfr.im git - solanum.git/blobdiff - librb/src/tools.c
Name the fallback strncasecmp properly [ci skip]
[solanum.git] / librb / src / tools.c
index 03dd632e53b85cfccf2de6df500f2de43a2cee9e..d6a1e93399c28e612723eda0db300e5a9f37872b 100644 (file)
@@ -20,7 +20,6 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
  *  USA
  *
- *  $Id: tools.c 26170 2008-10-26 20:59:07Z androsyn $
  *
  *  Here is the original header:
  *
@@ -32,6 +31,7 @@
  *  defined, tools.h will build inlined versions of the functions
  *  on supported compilers
  */
+
 #define _GNU_SOURCE 1
 #include <librb_config.h>
 #include <rb_lib.h>
@@ -138,6 +138,103 @@ rb_string_to_array(char *string, char **parv, int maxpara)
        return x;
 }
 
+#ifndef HAVE_STRCASECMP
+#ifndef _WIN32
+/* Fallback taken from FreeBSD. --Elizafox */
+int
+rb_strcasecmp(const char *s1, const char *s2)
+{
+       const unsigned char *us1 = (const unsigned char *)s1,
+       const unsigned char *us2 = (const unsigned char *)s2;
+
+       while (tolower(*us1) == tolower(*us2++))
+       {
+               if (*us1++ == '\0')
+                       return 0;
+       }
+
+       return (tolower(*us1) - tolower(*--us2));
+}
+#else /* _WIN32 */
+int
+rb_strcasecmp(const char *s1, const char *s2)
+{
+       return stricmp(s1, s2);
+}
+#endif /* _WIN32 */
+#else /* HAVE_STRCASECMP */
+int
+rb_strcasecmp(const char *s1, const char *s2)
+{
+       return strcasecmp(s1, s2);
+}
+#endif
+
+#ifndef HAVE_STRNCASECMP
+#ifndef _WIN32
+/* Fallback taken from FreeBSD. --Elizafox */
+int
+rb_strncasecmp(const char *s1, const char *s2, size_t n)
+{
+       if (n != 0)
+       {
+               const unsigned char *us1 = (const unsigned char *)s1;
+               const unsigned char *us2 = (const unsigned char *)s2;
+
+               do
+               {
+                       if (tolower(*us1) != tolower(*us2++))
+                               return (tolower(*us1) - tolower(*--us2));
+                       if (*us1++ == '\0')
+                               break;
+               } while (--n != 0);
+       }
+       return 0;
+}
+#else /* _WIN32 */
+int
+rb_strncasecmp(const char *s1, const char *s2, size_t n)
+{
+       return strnicmp(s1, s2, n);
+}
+#endif /* _WIN32 */
+#else /* HAVE_STRNCASECMP */
+int
+rb_strncasecmp(const char *s1, const char *s2, size_t n)
+{
+       return strncasecmp(s1, s2, n);
+}
+#endif
+
+#ifndef HAVE_STRCASESTR
+/* Fallback taken from FreeBSD. --Elizafox */
+char *
+rb_strcasestr(const char *s, const char *find)
+{
+       char c, sc;
+       size_t len;
+
+       if ((c = *find++) != 0) {
+               c = tolower((unsigned char)c);
+               len = strlen(find);
+               do {
+                       do {
+                               if ((sc = *s++) == 0)
+                                       return (NULL);
+                       } while ((char)tolower((unsigned char)sc) != c);
+               } while (rb_strncasecmp(s, find, len) != 0);
+               s--;
+       }
+       return ((char *)s);
+}
+#else
+char *
+rb_strcasestr(const char *s, const char *find)
+{
+       return strcasestr(s, find);
+}
+#endif
+
 #ifndef HAVE_STRLCAT
 size_t
 rb_strlcat(char *dest, const char *src, size_t count)