Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAL3m8eDWTt5fEHecnKCLO6O4L-8N3z==HGswgqRZrg6EuctBhw@mail.gmail.com>
Date: Thu, 21 Feb 2013 10:44:25 -0500
From: Strake <strake888@...il.com>
To: musl <musl@...ts.openwall.com>
Subject: Re: [PATCH] write strcasestr

Sorry, included stdio for fault-finding and forgot to exclude it again.


>From 60b96f3e18f75d989c09ab4d6dc3958558d766fb Mon Sep 17 00:00:00 2001
From: Strake <strake888@...il.com>
Date: Thu, 21 Feb 2013 10:41:09 -0500
Subject: [PATCH] write strcasestr

---
 src/string/strcasestr.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)

diff --git a/src/string/strcasestr.c b/src/string/strcasestr.c
index f1cb0e8..b21a7b5 100644
--- a/src/string/strcasestr.c
+++ b/src/string/strcasestr.c
@@ -1,7 +1,34 @@
+#include <stdlib.h>
 #include <string.h>
+#include <wchar.h>

-char *strcasestr(const char *h, const char *n)
-{
-	//FIXME!
-	return strstr(h, n);
+static void mbstolower (char *s) {
+	mbstate_t mbst;
+	wchar_t wx;
+	
+	while (*s) {
+		memset (&mbst, 0, sizeof (mbstate_t));
+		if (mbrtowc (&wx, s, MB_CUR_MAX, &mbst) < 0) break;
+		wx = towlower (wx);
+		memset (&mbst, 0, sizeof (mbstate_t));
+		s += wcrtomb (s, wx, &mbst);
+	}
+}
+
+char *strcasestr (const char *s, const char *t) {
+	char *x, *y, *z;
+	
+	x = strdup (s); if (!x) return 0;
+	y = strdup (t); if (!y) return 0;
+
+	mbstolower (x);
+	mbstolower (y);
+	
+	z = strstr (x, y);
+	if (z) s += z - x; else s = 0;
+	
+	free (x);
+	free (y);
+	
+	return s;
 }
-- 
1.7.11.3

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.