qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH] log: use strtok_r


From: Paolo Bonzini
Subject: [Qemu-devel] [PATCH] log: use strtok_r
Date: Tue, 1 Mar 2016 12:48:55 +0100

Signed-off-by: Paolo Bonzini <address@hidden>
---
 include/sysemu/os-win32.h |  1 +
 util/log.c                | 30 ++++++++----------------------
 util/oslib-win32.c        | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+), 22 deletions(-)

diff --git a/include/sysemu/os-win32.h b/include/sysemu/os-win32.h
index fbed346..042633f 100644
--- a/include/sysemu/os-win32.h
+++ b/include/sysemu/os-win32.h
@@ -80,6 +80,7 @@ struct tm *gmtime_r(const time_t *timep, struct tm *result);
 struct tm *localtime_r(const time_t *timep, struct tm *result);
 #endif /* CONFIG_LOCALTIME_R */
 
+char *strtok_r(char *str, const char *delim, char **saveptr);
 
 static inline void os_setup_signal_handling(void) {}
 static inline void os_daemonize(void) {}
diff --git a/util/log.c b/util/log.c
index 8b921de..e4f2679 100644
--- a/util/log.c
+++ b/util/log.c
@@ -140,40 +140,29 @@ const QEMULogItem qemu_log_items[] = {
     { 0, NULL, NULL },
 };
 
-static int cmp1(const char *s1, int n, const char *s2)
-{
-    if (strlen(s2) != n) {
-        return 0;
-    }
-    return memcmp(s1, s2, n) == 0;
-}
-
 /* takes a comma separated list of log masks. Return 0 if error. */
 int qemu_str_to_log_mask(const char *str)
 {
     const QEMULogItem *item;
     int mask;
-    const char *p, *p1;
+    char *copy, *p, *p1;
 
-    p = str;
+    copy = strdup(str);
     mask = 0;
-    for (;;) {
-        p1 = strchr(p, ',');
-        if (!p1) {
-            p1 = p + strlen(p);
-        }
-        if (cmp1(p,p1-p,"all")) {
+    for (p = strtok_r(copy, ",", &p1); p;
+         p = strtok_r(NULL, ",", &p1)) {
+        if (!strcmp(p,"all")) {
             for (item = qemu_log_items; item->mask != 0; item++) {
                 mask |= item->mask;
             }
 #ifdef CONFIG_TRACE_LOG
-        } else if (strncmp(p, "trace:", 6) == 0 && p + 6 != p1) {
+        } else if (strncmp(p, "trace:", 6) == 0 && p[6] != 0) {
             trace_enable_events(p + 6);
             mask |= LOG_TRACE;
 #endif
         } else {
             for (item = qemu_log_items; item->mask != 0; item++) {
-                if (cmp1(p, p1 - p, item->name)) {
+                if (!strcmp(p, item->name)) {
                     goto found;
                 }
             }
@@ -181,11 +170,8 @@ int qemu_str_to_log_mask(const char *str)
         found:
             mask |= item->mask;
         }
-        if (*p1 != ',') {
-            break;
-        }
-        p = p1 + 1;
     }
+    g_free(copy);
     return mask;
 }
 
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index 438cfa4..9f94871 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -504,3 +504,38 @@ pid_t qemu_fork(Error **errp)
                      "cannot fork child process");
     return -1;
 }
+
+/*
+ * public domain strtok_r() by Charlie Gordon
+ *
+ *   from comp.lang.c  9/14/2007
+ *
+ *      http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684
+ *
+ *     (Declaration that it's public domain):
+ *      http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c
+ */
+char *strtok_r(char *str, const char *delim, char **saveptr)
+{
+    char *ret;
+
+    if (!str) {
+        str = *saveptr;
+    }
+
+    /* Ignore delimiters at beginning of string.  */
+    str += strspn(str, delim);
+    if (!*str) {
+        /* Ignore delimiters at end of string too.  */
+        return NULL;
+    }
+
+    ret = str;
+    str += strcspn(str, delim);
+    if (*str) {
+        *str++ = '\0';
+    }
+
+    *saveptr = str;
+    return ret;
+}
-- 
2.5.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]