From 45286d4ad7d54adc8d9d06dd3382912b5bee6845 Mon Sep 17 00:00:00 2001
From: Jeff Layton <jlayton@redhat.com>
Date: Sun, 11 Jan 2009 11:55:35 +0100
Subject: [PATCH] Add strspn, strcspn, and strtok_r
This adds strspn, strcspn, and strtok_r to char_io.c. This is to support the
parsing of multiple options on the initrd commandline.
These were "borrowed" from dietlibc with some small changes.
Bug Report: https://bugzilla.redhat.com/show_bug.cgi?id=179127
---
stage2/char_io.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
stage2/shared.h | 7 ++++++-
2 files changed, 57 insertions(+), 1 deletions(-)
diff --git a/stage2/char_io.c b/stage2/char_io.c
index 7a67289..9b67c0d 100644
--- a/stage2/char_io.c
+++ b/stage2/char_io.c
@@ -1363,6 +1363,57 @@ grub_strlen (const char *str)
return len;
}
+
+/* this function "borrowed" from dietlibc */
+int
+grub_strspn(const char *s, const char *accept)
+{
+ int l=0;
+ int a=1,i,al=grub_strlen(accept);
+
+ while((a)&&(*s))
+ {
+ for(a=i=0;(!a)&&(i<al);i++)
+ if (*s==accept[i]) a=1;
+ if (a) l++;
+ s++;
+ }
+ return l;
+}
+
+/* this function "borrowed" from dietlibc */
+int
+grub_strcspn(const char *s, const char *reject)
+{
+ int l=0;
+ int a=1,i,al=grub_strlen(reject);
+
+ while((a)&&(*s))
+ {
+ for(i=0;(a)&&(i<al);i++)
+ if (*s==reject[i]) a=0;
+ if (a) l++;
+ s++;
+ }
+ return l;
+}
+
+/* this function "borrowed" from dietlibc */
+char *
+grub_strtok_r(char *s, const char *delim, char **ptrptr) {
+ char *tmp=0;
+
+ if (s==0) s=*ptrptr;
+ s+=grub_strspn(s,delim); /* overread leading delimiter */
+ if (*s) {
+ tmp=s;
+ s+=grub_strcspn(s,delim);
+ if (*s) *s++=0; /* not the end ? => terminate it */
+ }
+ *ptrptr=s;
+ return tmp;
+}
+
#endif /* ! STAGE1_5 */
#ifdef GRUB_UTIL
diff --git a/stage2/shared.h b/stage2/shared.h
index 1be7ab1..93f586f 100644
--- a/stage2/shared.h
+++ b/stage2/shared.h
@@ -375,9 +375,11 @@ extern void *grub_scratch_mem;
#define tolower grub_tolower
#define strlen grub_strlen
#define strcpy grub_strcpy
+#define strspn grub_strspn
+#define strcspn grub_strcspn
+#define strtok_r grub_strtok_r
#endif /* WITHOUT_LIBC_STUBS */
-
#ifndef ASM_FILE
/*
* Below this should be ONLY defines and other constructs for C code.
@@ -899,6 +901,9 @@ int grub_memcmp (const char *s1, const char *s2, int n);
int grub_strcmp (const char *s1, const char *s2);
int grub_strlen (const char *str);
char *grub_strcpy (char *dest, const char *src);
+int grub_strspn(const char *s, const char *accept);
+int grub_strcspn(const char *s, const char *reject);
+char *grub_strtok_r(char *s, const char *delim, char **ptrptr);
#ifndef GRUB_UTIL
typedef unsigned long grub_jmp_buf[8];
--
1.5.5.6