Wednesday, October 17, 2012

%.*s - Weird printk or Format of the format string

See this patch: https://lkml.org/lkml/2012/8/21/46

The author suggests:

- p9_debug(P9_DEBUG_VFS, "%s -> %s (%s)\n",
- dentry->d_name.name, st->extension, buffer);
+ p9_debug(P9_DEBUG_VFS, "%s -> %s (%.*s)\n",
+ dentry->d_name.name, st->extension, buflen, buffer);

But what the heck does %.*s? And why buflen variable was added as parameter to p9_debug? What would be the output of the C program above:

#include <stdio.h>

void main () {
        int i;
        char *ab ="abcdefghi";

        for (i = 0; i < 10; i++)
                printf ("%.*s\n", i, ab);
}

[peter@ace tmp]$ gcc test.c;./a.out

a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi

Cool huh?

No comments:

Post a Comment