qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: jtomko@redhat.com, kraxel@redhat.com
Subject: Re: [Qemu-devel] [PATCH] log: use strtok_r
Date: Tue, 1 Mar 2016 12:51:20 +0100	[thread overview]
Message-ID: <56D58238.8060707@redhat.com> (raw)
In-Reply-To: <1456832935-8748-1-git-send-email-pbonzini@redhat.com>

Sent by mistake, sorry.

Paolo

On 01/03/2016 12:48, Paolo Bonzini wrote:
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  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;
> +}
> 

      reply	other threads:[~2016-03-01 11:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-01 11:48 [Qemu-devel] [PATCH] log: use strtok_r Paolo Bonzini
2016-03-01 11:51 ` Paolo Bonzini [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56D58238.8060707@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=jtomko@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).