summaryrefslogtreecommitdiffstats
path: root/tools/lib/vsprintf.c
blob: 8780b4cdab210519570097b10e0b048db8b880a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// SPDX-License-Identifier: GPL-2.0
#include <sys/types.h>
#include <linux/kernel.h>
#include <stdio.h>

int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
{
       int i = vsnprintf(buf, size, fmt, args);
       ssize_t ssize = size;

       return (i >= ssize) ? (ssize - 1) : i;
}

int scnprintf(char * buf, size_t size, const char * fmt, ...)
{
       ssize_t ssize = size;
       va_list args;
       int i;

       va_start(args, fmt);
       i = vsnprintf(buf, size, fmt, args);
       va_end(args);

       return (i >= ssize) ? (ssize - 1) : i;
}

int scnprintf_pad(char * buf, size_t size, const char * fmt, ...)
{
	ssize_t ssize = size;
	va_list args;
	int i;

	va_start(args, fmt);
	i = vscnprintf(buf, size, fmt, args);
	va_end(args);

	if (i < (int) size) {
		for (; i < (int) size; i++)
			buf[i] = ' ';
		buf[i] = 0x0;
	}

	return (i >= ssize) ? (ssize - 1) : i;
}