[OpenBIOS] [PATCH] implement the strstr() function

Programmingkid programmingkidx at gmail.com
Tue Apr 26 07:09:46 CEST 2016


Implement the strstr() function.

Signed-off-by: John Arbuckle <programmingkidx at gmail.com>

Index: include/libc/string.h
===================================================================
--- include/libc/string.h	(revision 1395)
+++ include/libc/string.h	(working copy)
@@ -47,7 +47,7 @@
 extern char	*strdup( const char *str );
 extern int	strcasecmp( const char *cs, const char *ct );
 extern int	strncasecmp( const char *cs, const char *ct, size_t count );
-
+extern char *strstr(char *buffer, const char *search_string);
 extern  char 	*strncpy_nopad( char *dest, const char *src, size_t n );
 
 #define _U      0x01    /* upper */
Index: libc/string.c
===================================================================
--- libc/string.c	(revision 1395)
+++ libc/string.c	(working copy)
@@ -385,3 +385,30 @@
 	return __res;
 }
 
+// Search for a string within another string
+char *strstr(char *buffer, const char *search_string)
+{
+    if (!*search_string || (strlen(search_string) == 0)) {
+        return buffer;
+    }
+ 
+    int match_found, index, index2;
+    for (index = 0; index < strlen(buffer); index++) {
+        if (buffer[index] == search_string[0]) {
+            match_found = 1;
+            // see if we have a match
+            for (index2 = 0; index2 < strlen(search_string); index2++) {
+                if (buffer[index + index2] != search_string[index2]) {
+                    match_found = 0; // match not found
+                    break;
+                }
+            }
+            if(match_found == 1) {
+                return &buffer[index];
+            }
+        }
+    }
+    
+    // Could not find search_string in buffer
+    return NULL;
+}




More information about the OpenBIOS mailing list