123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- #include <linux/uaccess.h>
- #include <linux/seq_file.h>
- #include <linux/seq_buf.h>
- static bool seq_buf_can_fit(struct seq_buf *s, size_t len)
- {
- return s->len + len <= s->size;
- }
- int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s)
- {
- unsigned int len = seq_buf_used(s);
- return seq_write(m, s->buffer, len);
- }
- int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args)
- {
- int len;
- WARN_ON(s->size == 0);
- if (s->len < s->size) {
- len = vsnprintf(s->buffer + s->len, s->size - s->len, fmt, args);
- if (s->len + len < s->size) {
- s->len += len;
- return 0;
- }
- }
- seq_buf_set_overflow(s);
- return -1;
- }
- int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
- {
- va_list ap;
- int ret;
- va_start(ap, fmt);
- ret = seq_buf_vprintf(s, fmt, ap);
- va_end(ap);
- return ret;
- }
- #ifdef CONFIG_BINARY_PRINTF
- int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary)
- {
- unsigned int len = seq_buf_buffer_left(s);
- int ret;
- WARN_ON(s->size == 0);
- if (s->len < s->size) {
- ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
- if (s->len + ret < s->size) {
- s->len += ret;
- return 0;
- }
- }
- seq_buf_set_overflow(s);
- return -1;
- }
- #endif
- int seq_buf_puts(struct seq_buf *s, const char *str)
- {
- unsigned int len = strlen(str);
- WARN_ON(s->size == 0);
-
- len += 1;
- if (seq_buf_can_fit(s, len)) {
- memcpy(s->buffer + s->len, str, len);
-
- s->len += len - 1;
- return 0;
- }
- seq_buf_set_overflow(s);
- return -1;
- }
- int seq_buf_putc(struct seq_buf *s, unsigned char c)
- {
- WARN_ON(s->size == 0);
- if (seq_buf_can_fit(s, 1)) {
- s->buffer[s->len++] = c;
- return 0;
- }
- seq_buf_set_overflow(s);
- return -1;
- }
- int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
- {
- WARN_ON(s->size == 0);
- if (seq_buf_can_fit(s, len)) {
- memcpy(s->buffer + s->len, mem, len);
- s->len += len;
- return 0;
- }
- seq_buf_set_overflow(s);
- return -1;
- }
- #define MAX_MEMHEX_BYTES 8U
- #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
- int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
- unsigned int len)
- {
- unsigned char hex[HEX_CHARS];
- const unsigned char *data = mem;
- unsigned int start_len;
- int i, j;
- WARN_ON(s->size == 0);
- while (len) {
- start_len = min(len, HEX_CHARS - 1);
- #ifdef __BIG_ENDIAN
- for (i = 0, j = 0; i < start_len; i++) {
- #else
- for (i = start_len-1, j = 0; i >= 0; i--) {
- #endif
- hex[j++] = hex_asc_hi(data[i]);
- hex[j++] = hex_asc_lo(data[i]);
- }
- if (WARN_ON_ONCE(j == 0 || j/2 > len))
- break;
-
- len -= j / 2;
- hex[j++] = ' ';
- seq_buf_putmem(s, hex, j);
- if (seq_buf_has_overflowed(s))
- return -1;
- }
- return 0;
- }
- int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc)
- {
- char *buf;
- size_t size = seq_buf_get_buf(s, &buf);
- int res = -1;
- WARN_ON(s->size == 0);
- if (size) {
- char *p = d_path(path, buf, size);
- if (!IS_ERR(p)) {
- char *end = mangle_path(buf, p, esc);
- if (end)
- res = end - buf;
- }
- }
- seq_buf_commit(s, res);
- return res;
- }
- int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt)
- {
- int len;
- int ret;
- if (!cnt)
- return 0;
- len = seq_buf_used(s);
- if (len <= s->readpos)
- return -EBUSY;
- len -= s->readpos;
- if (cnt > len)
- cnt = len;
- ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
- if (ret == cnt)
- return -EFAULT;
- cnt -= ret;
- s->readpos += cnt;
- return cnt;
- }
|