[Spice-devel] [usbredir v3 PATCH] usbredirfilter: Win32: use strtok_r implementation of glibc

Uri Lublin uril at redhat.com
Thu May 31 03:12:22 PDT 2012


I could not find an imlpementation of strtok_r in mingw.
It seems strtok_s requires an additional package to be installed on the
usb-host machine.

This patch adds the glibc implementation of strtok_r to be used on windows.
---
 usbredirparser/Makefile.am      |    4 ++
 usbredirparser/strtok_r.c       |   66 +++++++++++++++++++++++++++++++++++++++
 usbredirparser/strtok_r.h       |   26 +++++++++++++++
 usbredirparser/usbredirfilter.c |    5 +++
 4 files changed, 101 insertions(+), 0 deletions(-)
 create mode 100644 usbredirparser/strtok_r.c
 create mode 100644 usbredirparser/strtok_r.h

diff --git a/usbredirparser/Makefile.am b/usbredirparser/Makefile.am
index d73ba77..53c1012 100644
--- a/usbredirparser/Makefile.am
+++ b/usbredirparser/Makefile.am
@@ -6,5 +6,9 @@ libusbredirparser_la_HEADERS = usbredirparser.h usbredirfilter.h usbredirproto.h
 libusbredirparser_la_LDFLAGS = -version-info $(LIBUSBREDIRPARSER_SO_VERSION) \
                                -no-undefined

+if OS_WIN32
+libusbredirparser_la_SOURCES += strtok_r.c strtok_r.h
+endif
+
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = libusbredirparser.pc
diff --git a/usbredirparser/strtok_r.c b/usbredirparser/strtok_r.c
new file mode 100644
index 0000000..6f27374
--- /dev/null
+++ b/usbredirparser/strtok_r.c
@@ -0,0 +1,66 @@
+/* Reentrant string tokenizer.  Generic version.
+   Copyright (C) 1991,1996-1999,2001,2004 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <string.h>
+
+/* Parse S into tokens separated by characters in DELIM.
+   If S is NULL, the saved pointer in SAVE_PTR is used as
+   the next starting point.  For example:
+	char s[] = "-abc-=-def";
+	char *sp;
+	x = strtok_r(s, "-", &sp);	// x = "abc", sp = "=-def"
+	x = strtok_r(NULL, "-=", &sp);	// x = "def", sp = NULL
+	x = strtok_r(NULL, "=", &sp);	// x = NULL
+		// s = "abc\0-def\0"
+*/
+char *
+glibc_strtok_r (char *s, const char *delim, char **save_ptr)
+{
+  char *token;
+
+  if (s == NULL)
+    s = *save_ptr;
+
+  /* Scan leading delimiters.  */
+  s += strspn (s, delim);
+  if (*s == '\0')
+    {
+      *save_ptr = s;
+      return NULL;
+    }
+
+  /* Find the end of the token.  */
+  token = s;
+  s = strpbrk (token, delim);
+  if (s == NULL)
+    /* This token finishes the string.  */
+    *save_ptr = strchr (token, '\0');
+  else
+    {
+      /* Terminate the token and make *SAVE_PTR point past it.  */
+      *s = '\0';
+      *save_ptr = s + 1;
+    }
+  return token;
+}
+
diff --git a/usbredirparser/strtok_r.h b/usbredirparser/strtok_r.h
new file mode 100644
index 0000000..50e6e0e
--- /dev/null
+++ b/usbredirparser/strtok_r.h
@@ -0,0 +1,26 @@
+/* Reentrant string tokenizer.  Generic version.
+   Copyright (C) 1991,1996-1999,2001,2004 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#ifndef _USBREDIRPARSER_STRTOK_H_
+#define _USBREDIRPARSER_STRTOK_H_
+
+char *
+glibc_strtok_r(char *s, const char *delim, char **save_ptr);
+
+#endif
diff --git a/usbredirparser/usbredirfilter.c b/usbredirparser/usbredirfilter.c
index b74c921..b90d16f 100644
--- a/usbredirparser/usbredirfilter.c
+++ b/usbredirparser/usbredirfilter.c
@@ -23,6 +23,11 @@
 #include <string.h>
 #include <errno.h>

+#ifdef WIN32
+#include "strtok_r.h"
+#define strtok_r  glibc_strtok_r
+#endif
+
 #include "usbredirfilter.h"

 int usbredirfilter_string_to_rules(
-- 
1.7.7.6



More information about the Spice-devel mailing list