]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
seq_file: introduce seq_setwidth() and seq_pad()
authorTetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Thu, 14 Nov 2013 22:31:56 +0000 (14:31 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 15 Nov 2013 00:32:20 +0000 (09:32 +0900)
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>
fs/seq_file.c
include/linux/seq_file.h

index a290157265ef5cfb52dbdb6a9e950e9733462d16..1cd2388ca5bd7c11cd504b3a65ba67969cd936b5 100644 (file)
@@ -766,6 +766,21 @@ int seq_write(struct seq_file *seq, const void *data, size_t len)
 }
 EXPORT_SYMBOL(seq_write);
 
+/**
+ * seq_pad - write padding spaces to buffer
+ * @m: seq_file identifying the buffer to which data should be written
+ * @c: the byte to append after padding if non-zero
+ */
+void seq_pad(struct seq_file *m, char c)
+{
+       int size = m->pad_until - m->count;
+       if (size > 0)
+               seq_printf(m, "%*s", size, "");
+       if (c)
+               seq_putc(m, c);
+}
+EXPORT_SYMBOL(seq_pad);
+
 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
 {
        struct list_head *lh;
index 4e32edc8f506b89f13b0260200498b5cf87b490a..52e0097f61f00108f0faec9b37c57c2f03e1fcb2 100644 (file)
@@ -20,6 +20,7 @@ struct seq_file {
        size_t size;
        size_t from;
        size_t count;
+       size_t pad_until;
        loff_t index;
        loff_t read_pos;
        u64 version;
@@ -79,6 +80,20 @@ static inline void seq_commit(struct seq_file *m, int num)
        }
 }
 
+/**
+ * seq_setwidth - set padding width
+ * @m: the seq_file handle
+ * @size: the max number of bytes to pad.
+ *
+ * Call seq_setwidth() for setting max width, then call seq_printf() etc. and
+ * finally call seq_pad() to pad the remaining bytes.
+ */
+static inline void seq_setwidth(struct seq_file *m, size_t size)
+{
+       m->pad_until = m->count + size;
+}
+void seq_pad(struct seq_file *m, char c);
+
 char *mangle_path(char *s, const char *p, const char *esc);
 int seq_open(struct file *, const struct seq_operations *);
 ssize_t seq_read(struct file *, char __user *, size_t, loff_t *);