1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2014 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
7 * The seq_buf is a handy tool that allows you to pass a descriptor around
8 * to a buffer that other functions can write to. It is similar to the
9 * seq_file functionality but has some differences.
11 * To use it, the seq_buf must be initialized with seq_buf_init().
12 * This will set up the counters within the descriptor. You can call
13 * seq_buf_init() more than once to reset the seq_buf to start
16 #include <linux/seq_buf.h>
20 * seq_buf_can_fit - can the new data fit in the current buffer?
21 * @s: the seq_buf descriptor
22 * @len: The length to see if it can fit in the current buffer
24 * Returns true if there's enough unused space in the seq_buf buffer
25 * to fit the amount of new data according to @len.
27 static bool seq_buf_can_fit(struct seq_buf *s, size_t len)
29 return s->len + len <= s->size;
33 * seq_buf_vprintf - sequence printing of information.
34 * @s: seq_buf descriptor
35 * @fmt: printf format string
36 * @args: va_list of arguments from a printf() type function
38 * Writes a vnprintf() format into the sequencce buffer.
40 * Returns zero on success, -1 on overflow.
42 int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args)
46 WARN_ON(s->size == 0);
48 if (s->len < s->size) {
49 len = vsnprintf(s->buffer + s->len, s->size - s->len, fmt, args);
50 if (s->len + len < s->size) {
55 seq_buf_set_overflow(s);
60 * seq_buf_printf - sequence printing of information
61 * @s: seq_buf descriptor
62 * @fmt: printf format string
64 * Writes a printf() format into the sequence buffer.
66 * Returns zero on success, -1 on overflow.
68 int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
74 ret = seq_buf_vprintf(s, fmt, ap);
81 * seq_buf_puts - sequence printing of simple string
82 * @s: seq_buf descriptor
83 * @str: simple string to record
85 * Copy a simple string into the sequence buffer.
87 * Returns zero on success, -1 on overflow
89 int seq_buf_puts(struct seq_buf *s, const char *str)
91 size_t len = strlen(str);
93 WARN_ON(s->size == 0);
95 /* Add 1 to len for the trailing null byte which must be there */
98 if (seq_buf_can_fit(s, len)) {
99 memcpy(s->buffer + s->len, str, len);
100 /* Don't count the trailing null byte against the capacity */
104 seq_buf_set_overflow(s);
109 * seq_buf_putc - sequence printing of simple character
110 * @s: seq_buf descriptor
111 * @c: simple character to record
113 * Copy a single character into the sequence buffer.
115 * Returns zero on success, -1 on overflow
117 int seq_buf_putc(struct seq_buf *s, unsigned char c)
119 WARN_ON(s->size == 0);
121 if (seq_buf_can_fit(s, 1)) {
122 s->buffer[s->len++] = c;
125 seq_buf_set_overflow(s);
130 * seq_buf_putmem - write raw data into the sequenc buffer
131 * @s: seq_buf descriptor
132 * @mem: The raw memory to copy into the buffer
133 * @len: The length of the raw memory to copy (in bytes)
135 * There may be cases where raw memory needs to be written into the
136 * buffer and a strcpy() would not work. Using this function allows
139 * Returns zero on success, -1 on overflow
141 int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
143 WARN_ON(s->size == 0);
145 if (seq_buf_can_fit(s, len)) {
146 memcpy(s->buffer + s->len, mem, len);
150 seq_buf_set_overflow(s);