Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

seq_file: introduce seq_setwidth() and seq_pad()

There are several users who want to know bytes written by seq_*() for
alignment purpose. Currently they are using %n format for knowing it
because seq_*() returns 0 on success.

This patch introduces seq_setwidth() and seq_pad() for allowing them to
align without using %n format.

Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: Joe Perches <joe@perches.com>
Cc: David Miller <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Tetsuo Handa and committed by
Linus Torvalds
839cc2a9 57f4257e

+30
+15
fs/seq_file.c
··· 766 766 } 767 767 EXPORT_SYMBOL(seq_write); 768 768 769 + /** 770 + * seq_pad - write padding spaces to buffer 771 + * @m: seq_file identifying the buffer to which data should be written 772 + * @c: the byte to append after padding if non-zero 773 + */ 774 + void seq_pad(struct seq_file *m, char c) 775 + { 776 + int size = m->pad_until - m->count; 777 + if (size > 0) 778 + seq_printf(m, "%*s", size, ""); 779 + if (c) 780 + seq_putc(m, c); 781 + } 782 + EXPORT_SYMBOL(seq_pad); 783 + 769 784 struct list_head *seq_list_start(struct list_head *head, loff_t pos) 770 785 { 771 786 struct list_head *lh;
+15
include/linux/seq_file.h
··· 20 20 size_t size; 21 21 size_t from; 22 22 size_t count; 23 + size_t pad_until; 23 24 loff_t index; 24 25 loff_t read_pos; 25 26 u64 version; ··· 79 78 m->count += num; 80 79 } 81 80 } 81 + 82 + /** 83 + * seq_setwidth - set padding width 84 + * @m: the seq_file handle 85 + * @size: the max number of bytes to pad. 86 + * 87 + * Call seq_setwidth() for setting max width, then call seq_printf() etc. and 88 + * finally call seq_pad() to pad the remaining bytes. 89 + */ 90 + static inline void seq_setwidth(struct seq_file *m, size_t size) 91 + { 92 + m->pad_until = m->count + size; 93 + } 94 + void seq_pad(struct seq_file *m, char c); 82 95 83 96 char *mangle_path(char *s, const char *p, const char *esc); 84 97 int seq_open(struct file *, const struct seq_operations *);