1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d0761e37SArnaldo Carvalho de Melo #include <sys/types.h>
3d0761e37SArnaldo Carvalho de Melo #include <linux/kernel.h>
4d0761e37SArnaldo Carvalho de Melo #include <stdio.h>
5d0761e37SArnaldo Carvalho de Melo
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)6d0761e37SArnaldo Carvalho de Melo int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
7d0761e37SArnaldo Carvalho de Melo {
8d0761e37SArnaldo Carvalho de Melo int i = vsnprintf(buf, size, fmt, args);
9d0761e37SArnaldo Carvalho de Melo ssize_t ssize = size;
10d0761e37SArnaldo Carvalho de Melo
11d0761e37SArnaldo Carvalho de Melo return (i >= ssize) ? (ssize - 1) : i;
12d0761e37SArnaldo Carvalho de Melo }
13d0761e37SArnaldo Carvalho de Melo
scnprintf(char * buf,size_t size,const char * fmt,...)14d0761e37SArnaldo Carvalho de Melo int scnprintf(char * buf, size_t size, const char * fmt, ...)
15d0761e37SArnaldo Carvalho de Melo {
16d0761e37SArnaldo Carvalho de Melo ssize_t ssize = size;
17d0761e37SArnaldo Carvalho de Melo va_list args;
18d0761e37SArnaldo Carvalho de Melo int i;
19d0761e37SArnaldo Carvalho de Melo
20d0761e37SArnaldo Carvalho de Melo va_start(args, fmt);
21d0761e37SArnaldo Carvalho de Melo i = vsnprintf(buf, size, fmt, args);
22d0761e37SArnaldo Carvalho de Melo va_end(args);
23d0761e37SArnaldo Carvalho de Melo
24d0761e37SArnaldo Carvalho de Melo return (i >= ssize) ? (ssize - 1) : i;
25d0761e37SArnaldo Carvalho de Melo }
26*1c492422SJiri Olsa
scnprintf_pad(char * buf,size_t size,const char * fmt,...)27*1c492422SJiri Olsa int scnprintf_pad(char * buf, size_t size, const char * fmt, ...)
28*1c492422SJiri Olsa {
29*1c492422SJiri Olsa ssize_t ssize = size;
30*1c492422SJiri Olsa va_list args;
31*1c492422SJiri Olsa int i;
32*1c492422SJiri Olsa
33*1c492422SJiri Olsa va_start(args, fmt);
34*1c492422SJiri Olsa i = vscnprintf(buf, size, fmt, args);
35*1c492422SJiri Olsa va_end(args);
36*1c492422SJiri Olsa
37*1c492422SJiri Olsa if (i < (int) size) {
38*1c492422SJiri Olsa for (; i < (int) size; i++)
39*1c492422SJiri Olsa buf[i] = ' ';
40*1c492422SJiri Olsa buf[i] = 0x0;
41*1c492422SJiri Olsa }
42*1c492422SJiri Olsa
43*1c492422SJiri Olsa return (i >= ssize) ? (ssize - 1) : i;
44*1c492422SJiri Olsa }
45