]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/seq_buf.c
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[bcachefs-tools-debian] / linux / seq_buf.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * seq_buf.c
4  *
5  * Copyright (C) 2014 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6  *
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.
10  *
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
14  * from scratch.
15  */
16 #include <linux/seq_buf.h>
17 #include <stdio.h>
18
19 /**
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
23  *
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.
26  */
27 static bool seq_buf_can_fit(struct seq_buf *s, size_t len)
28 {
29         return s->len + len <= s->size;
30 }
31
32 /**
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
37  *
38  * Writes a vnprintf() format into the sequencce buffer.
39  *
40  * Returns zero on success, -1 on overflow.
41  */
42 int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args)
43 {
44         int len;
45
46         WARN_ON(s->size == 0);
47
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) {
51                         s->len += len;
52                         return 0;
53                 }
54         }
55         seq_buf_set_overflow(s);
56         return -1;
57 }
58
59 /**
60  * seq_buf_printf - sequence printing of information
61  * @s: seq_buf descriptor
62  * @fmt: printf format string
63  *
64  * Writes a printf() format into the sequence buffer.
65  *
66  * Returns zero on success, -1 on overflow.
67  */
68 int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
69 {
70         va_list ap;
71         int ret;
72
73         va_start(ap, fmt);
74         ret = seq_buf_vprintf(s, fmt, ap);
75         va_end(ap);
76
77         return ret;
78 }
79
80 /**
81  * seq_buf_puts - sequence printing of simple string
82  * @s: seq_buf descriptor
83  * @str: simple string to record
84  *
85  * Copy a simple string into the sequence buffer.
86  *
87  * Returns zero on success, -1 on overflow
88  */
89 int seq_buf_puts(struct seq_buf *s, const char *str)
90 {
91         size_t len = strlen(str);
92
93         WARN_ON(s->size == 0);
94
95         /* Add 1 to len for the trailing null byte which must be there */
96         len += 1;
97
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 */
101                 s->len += len - 1;
102                 return 0;
103         }
104         seq_buf_set_overflow(s);
105         return -1;
106 }
107
108 /**
109  * seq_buf_putc - sequence printing of simple character
110  * @s: seq_buf descriptor
111  * @c: simple character to record
112  *
113  * Copy a single character into the sequence buffer.
114  *
115  * Returns zero on success, -1 on overflow
116  */
117 int seq_buf_putc(struct seq_buf *s, unsigned char c)
118 {
119         WARN_ON(s->size == 0);
120
121         if (seq_buf_can_fit(s, 1)) {
122                 s->buffer[s->len++] = c;
123                 return 0;
124         }
125         seq_buf_set_overflow(s);
126         return -1;
127 }
128
129 /**
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)
134  *
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
137  * for such cases.
138  *
139  * Returns zero on success, -1 on overflow
140  */
141 int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
142 {
143         WARN_ON(s->size == 0);
144
145         if (seq_buf_can_fit(s, len)) {
146                 memcpy(s->buffer + s->len, mem, len);
147                 s->len += len;
148                 return 0;
149         }
150         seq_buf_set_overflow(s);
151         return -1;
152 }