mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [patch/rfc] implement memmem() locally in kallsyms.c
@ 2007-06-07  5:16 Mike Frysinger
  2007-06-07  9:36 ` Jesper Juhl
  2007-06-08 12:27 ` Paulo Marques
  0 siblings, 2 replies; 4+ messages in thread
From: Mike Frysinger @ 2007-06-07  5:16 UTC (permalink / raw)
  To: linux-kernel

This patch basically copies the gnulib version of memmem() into
scripts/kallsyms.c.  While a useful function, it isn't in POSIX so some
systems (like Darwin) choose to omit it.  How do others feel ?

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
---
--- a/scripts/kallsyms.c
+++ b/scripts/kallsyms.c
@@ -26,8 +26,6 @@
  *
  */
 
-#define _GNU_SOURCE
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -56,6 +54,37 @@ int token_profit[0x10000];
 unsigned char best_table[256][2];
 unsigned char best_table_len[256];
 
+/* memmem(), while useful, is not in POSIX, so create a local version
+ * so we can compile on non-GNU systems (Darwin, *BSD, etc...)
+ */
+void *memmem(const void *haystack, size_t haystack_len,
+             const void *needle, size_t needle_len)
+{
+	const char *begin;
+	const char *const last_possible =
+		(const char *)haystack + haystack_len - needle_len;
+
+	/* The first occurrence of the empty string is deemed to occur at
+	 * the beginning of the string.
+	 */
+	if (needle_len == 0)
+		return (void *)haystack;
+
+	/* Sanity check, otherwise the loop might search through the whole
+	 * memory.
+	 */
+	if (haystack_len < needle_len)
+		return NULL;
+
+	for (begin = (const char *)haystack; begin <= last_possible; ++begin)
+		if (begin[0] == ((const char *)needle)[0] &&
+		    !memcmp((const void *)&begin[1],
+		            (const void *)((const char *)needle + 1),
+		            needle_len - 1))
+			return (void *)begin;
+
+	return NULL;
+}
 
 static void usage(void)
 {

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-06-08 12:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-07  5:16 [patch/rfc] implement memmem() locally in kallsyms.c Mike Frysinger
2007-06-07  9:36 ` Jesper Juhl
2007-06-07 15:44   ` Mike Frysinger
2007-06-08 12:27 ` Paulo Marques

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®