[ovs-dev] [patch_v9 1/6] string: Implement strcasestr substitute.

Darrell Ball dlu998 at gmail.com
Fri Aug 4 04:34:41 UTC 2017


strcasestr is not defined for Windows, so implement a version
that could be used on Windows. This is needed for an upcoming
patch.

Signed-off-by: Darrell Ball <dlu998 at gmail.com>
---
 lib/string.c    | 34 ++++++++++++++++++++++++++++++++--
 lib/string.h.in |  3 ++-
 2 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/lib/string.c b/lib/string.c
index 082359d..a9ae996 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2011 Nicira, Inc.
+ * Copyright (c) 2009-2017 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -15,9 +15,13 @@
  */
 
 #include <config.h>
-
+#include <ctype.h>
 #include <string.h>
 
+#include "util.h"
+
+char *strcasestr_s(const char *str, const char *substr);
+
 #ifndef HAVE_STRNLEN
 size_t
 strnlen(const char *s, size_t maxlen)
@@ -26,3 +30,29 @@ strnlen(const char *s, size_t maxlen)
     return end ? end - s : maxlen;
 }
 #endif
+
+char *strcasestr_s(const char *str, const char *substr)
+{
+
+    int si = 0;  /* string index. */
+    int wsi = 0; /* working string index. */
+    int ssi = 0; /* substring index. */
+    while (str[si]) {
+         while (substr[ssi]) {
+            if (tolower(str[wsi]) == tolower(substr[ssi])) {
+                ssi++;
+                wsi++;
+            } else {
+                ssi = 0;
+                si++;
+                wsi = si;
+            }
+            break;
+        }
+        /* We matched the substring. */
+        if (!substr[ssi]) {
+            return CONST_CAST(char *, &str[si]);
+        }
+    }
+    return NULL;
+}
diff --git a/lib/string.h.in b/lib/string.h.in
index bbdaeb4..ac8f3b6 100644
--- a/lib/string.h.in
+++ b/lib/string.h.in
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2011, 2013 Nicira, Inc.
+ * Copyright (c) 2009-2017 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
 #define strcasecmp _stricmp
 #define strncasecmp _strnicmp
 #define strerror_r(errnum, buf, buflen) strerror_s(buf, buflen, errnum)
+#define strcasestr(str, substr) strcasestr_s(str, substr)
 #endif
 
 #ifndef HAVE_STRNLEN
-- 
1.9.1



More information about the dev mailing list