tools/testing/selftests/kvm/lib/guest_sprintf.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/kvm/lib/guest_sprintf.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/kvm/lib/guest_sprintf.c
Extension
.c
Size
6348 bytes
Lines
315
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (num < 0) {
			sign = '-';
			num = -num;
			size--;
		} else if (type & PLUS) {
			sign = '+';
			size--;
		} else if (type & SPACE) {
			sign = ' ';
			size--;
		}
	}
	if (type & SPECIAL) {
		if (base == 16)
			size -= 2;
		else if (base == 8)
			size--;
	}
	i = 0;
	if (num == 0)
		tmp[i++] = '0';
	else
		while (num != 0)
			tmp[i++] = (digits[__do_div(num, base)] | locase);
	if (i > precision)
		precision = i;
	size -= precision;
	if (!(type & (ZEROPAD + LEFT)))
		while (size-- > 0)
			APPEND_BUFFER_SAFE(str, end, ' ');
	if (sign)
		APPEND_BUFFER_SAFE(str, end, sign);
	if (type & SPECIAL) {
		if (base == 8)
			APPEND_BUFFER_SAFE(str, end, '0');
		else if (base == 16) {
			APPEND_BUFFER_SAFE(str, end, '0');
			APPEND_BUFFER_SAFE(str, end, 'x');
		}
	}
	if (!(type & LEFT))
		while (size-- > 0)
			APPEND_BUFFER_SAFE(str, end, c);
	while (i < precision--)
		APPEND_BUFFER_SAFE(str, end, '0');
	while (i-- > 0)
		APPEND_BUFFER_SAFE(str, end, tmp[i]);
	while (size-- > 0)
		APPEND_BUFFER_SAFE(str, end, ' ');

	return str;
}

int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args)
{
	char *str, *end;
	const char *s;
	u64 num;
	int i, base;
	int len;

	int flags;		/* flags to number() */

	int field_width;	/* width of output field */
	int precision;		/*
				 * min. # of digits for integers; max
				 * number of chars for from string
				 */
	int qualifier;		/* 'h', 'l', or 'L' for integer fields */

	end = buf + n;
	GUEST_ASSERT(buf < end);
	GUEST_ASSERT(n > 0);

	for (str = buf; *fmt; ++fmt) {
		if (*fmt != '%') {
			APPEND_BUFFER_SAFE(str, end, *fmt);
			continue;
		}

		/* process flags */
		flags = 0;
repeat:
		++fmt;		/* this also skips first '%' */
		switch (*fmt) {
		case '-':
			flags |= LEFT;
			goto repeat;
		case '+':
			flags |= PLUS;

Annotation

Implementation Notes