[klibc] [PATCH] Fixes bug which strips every other digit in klibc-utils dmesg output

Anthony Fok anthony.fok at thizgroup.com
Mon Jun 2 09:11:32 PDT 2008


Hello Peter,

Yes, your patch works very well here too.  Many thanks!  :-)

Anthony

2008/5/28 H. Peter Anvin <hpa at zytor.com>:

> [maks: removed you from the Cc: list because stro.at doesn't resolve at
> the moment... I know you're on the klibc list.]
>
> maximilian attems wrote:
>
>>
>> nack on the coding style,
>> please don't remove the readable switch to an if spagetti code.
>>
>> you are obfuscating the problem.
>>
>>
> Here is a more state-machine-like approach to the problem.  You may want to
> try it out.
>
>        -hpa
>
>
> diff --git a/usr/utils/dmesg.c b/usr/utils/dmesg.c
> index 8d2a719..e47bd63 100644
> --- a/usr/utils/dmesg.c
> +++ b/usr/utils/dmesg.c
> @@ -13,11 +13,14 @@ static void usage(char *name)
>  int main(int argc, char *argv[])
>  {
>        char *buf = NULL;
> +       const char *p;
> +       int c;
>        int bufsz = 0;
>        int cmd = 3;    /* Read all messages remaining in the ring buffer */
>        int len = 0;
>        int opt;
>        int i = 0;
> +       int newline;
>
>        while ((opt = getopt(argc, argv, "c")) != -1) {
>                switch (opt) {
> @@ -50,22 +53,28 @@ int main(int argc, char *argv[])
>                exit(1);
>        }
>
> -       while (buf[i] && i < len)
> -               switch (buf[i]) {
> +       newline = 1;
> +       p = buf;
> +       while ((c = *p)) {
> +               switch (c) {
> +               case '\n':
> +                       newline = 1;
> +                       putchar(c);
> +                       p++;
> +                       break;
>                case '<':
> -                       if (i == 0 || buf[i-1] == '\n')
> -                               i++;
> -               case '0' ... '9':
> -                       if (i > 0 && buf[i-1] == '<')
> -                               i++;
> -               case '>':
> -                       if (i > 0 && isdigit(buf[i-1]))
> -                               i++;
> +                       if (newline && isdigit(p[1]) && p[2] == '>') {
> +                               p += 3;
> +                               break;
> +                       }
> +                       /* else fall through */
>                default:
> -                       putchar(buf[i++]);
> +                       newline = 0;
> +                       putchar(c);
> +                       p++;
>                }
> -
> -       if (buf[i-1] != '\n')
> +       }
> +       if (!newline)
>                putchar('\n');
>
>        return 0;
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.zytor.com/pipermail/klibc/attachments/20080603/694306f7/attachment.html 


More information about the klibc mailing list