[ovs-dev] [patch_v7 1/6] string: Implement strcasestr if it is missing.
Darrell Ball
dlu998 at gmail.com
Thu Aug 3 20:49:34 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 | 41 ++++++++++++++++++++++++++++++++++++++++-
lib/string.h.in | 1 +
2 files changed, 41 insertions(+), 1 deletion(-)
diff --git a/lib/string.c b/lib/string.c
index 082359d..445acd7 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -15,9 +15,14 @@
*/
#include <config.h>
-
+#include <ctype.h>
+#include <stdio.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 +31,37 @@ strnlen(const char *s, size_t maxlen)
return end ? end - s : maxlen;
}
#endif
+
+char *strcasestr_s(const char *str, const char *substr)
+{
+ if (!str || !substr) {
+ return NULL;
+ }
+ if (strlen(substr) > strlen(str)) {
+ return NULL;
+ }
+
+ int si = 0; /* string index. */
+ int wsi = 0; /* working string index. */
+ int ssi = 0; /* substring index. */
+ const char *save_str;
+ while (str[si]) {
+ save_str = &str[si];
+ while (substr[ssi]) {
+ if (toupper(str[wsi]) == toupper(substr[ssi])) {
+ ssi++;
+ wsi++;
+ } else {
+ ssi = 0;
+ si++;
+ wsi = si;
+ }
+ break;
+ }
+ /* We matched the substring. */
+ if (!substr[ssi]) {
+ return CONST_CAST(char *, save_str);
+ }
+ }
+ return NULL;
+}
diff --git a/lib/string.h.in b/lib/string.h.in
index bbdaeb4..5a86ea3 100644
--- a/lib/string.h.in
+++ b/lib/string.h.in
@@ -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