110 lines
2.8 KiB
Diff
110 lines
2.8 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Alec Brown <alec.r.brown@oracle.com>
|
|
Date: Tue, 12 Aug 2025 03:45:32 +0000
|
|
Subject: [PATCH] kern/misc: Implement grub_strtok()
|
|
|
|
Add the functions grub_strtok() and grub_strtok_r() to help parse strings into
|
|
tokens separated by characters in the "delim" parameter. These functions are
|
|
present in gnulib but calling them directly from the gnulib code is quite
|
|
challenging since the call "#include <string.h>" would include the header file
|
|
grub-core/lib/posix_wrap/string.h instead of grub-core/lib/gnulib/string.h,
|
|
where strtok() and strtok_r() are declared. Since this overlap is quite
|
|
problematic, the simpler solution was to implement the code in the GRUB based
|
|
on gnulib's implementation. For more information on these functions, visit the
|
|
Linux Programmer's Manual, man strtok.
|
|
|
|
Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
|
|
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
|
|
---
|
|
grub-core/kern/misc.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
include/grub/misc.h | 3 +++
|
|
2 files changed, 65 insertions(+)
|
|
|
|
diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
|
|
index 69bd655f3d..c69fe7fb19 100644
|
|
--- a/grub-core/kern/misc.c
|
|
+++ b/grub-core/kern/misc.c
|
|
@@ -453,6 +453,68 @@ grub_strword (const char *haystack, const char *needle)
|
|
return 0;
|
|
}
|
|
|
|
+char *
|
|
+grub_strtok_r (char *s, const char *delim, char **save_ptr)
|
|
+{
|
|
+ char *token;
|
|
+ const char *c;
|
|
+ bool is_delim;
|
|
+
|
|
+ if (s == NULL)
|
|
+ s = *save_ptr;
|
|
+
|
|
+ /* Scan leading delimiters. */
|
|
+ while (*s != '\0')
|
|
+ {
|
|
+ is_delim = false;
|
|
+ for (c = delim; *c != '\0'; c++)
|
|
+ {
|
|
+ if (*s == *c)
|
|
+ {
|
|
+ is_delim = true;
|
|
+ break;
|
|
+ }
|
|
+ }
|
|
+ if (is_delim == true)
|
|
+ s++;
|
|
+ else
|
|
+ break;
|
|
+ }
|
|
+
|
|
+ if (*s == '\0')
|
|
+ {
|
|
+ *save_ptr = s;
|
|
+ return NULL;
|
|
+ }
|
|
+
|
|
+ /* Find the end of the token. */
|
|
+ token = s;
|
|
+ while (*s != '\0')
|
|
+ {
|
|
+ for (c = delim; *c != '\0'; c++)
|
|
+ {
|
|
+ if (*s == *c)
|
|
+ {
|
|
+ *s = '\0';
|
|
+ *save_ptr = s + 1;
|
|
+ return token;
|
|
+ }
|
|
+ }
|
|
+ s++;
|
|
+ }
|
|
+
|
|
+ *save_ptr = s;
|
|
+ return token;
|
|
+}
|
|
+
|
|
+char *
|
|
+grub_strtok (char *s, const char *delim)
|
|
+{
|
|
+ static char *last;
|
|
+
|
|
+ return grub_strtok_r (s, delim, &last);
|
|
+}
|
|
+
|
|
int
|
|
grub_isspace (int c)
|
|
{
|
|
diff --git a/include/grub/misc.h b/include/grub/misc.h
|
|
index 0429339ef3..626f0c3535 100644
|
|
--- a/include/grub/misc.h
|
|
+++ b/include/grub/misc.h
|
|
@@ -134,6 +134,9 @@ char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
|
|
char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
|
|
int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
|
|
|
|
+char *EXPORT_FUNC(grub_strtok_r) (char *s, const char *delim, char **save_ptr);
|
|
+char *EXPORT_FUNC(grub_strtok) (char *s, const char *delim);
|
|
+
|
|
/* Copied from gnulib.
|
|
Written by Bruno Haible <bruno@clisp.org>, 2005. */
|
|
static inline char *
|