while (isdigit(**s))
i = i * 10 + *((*s)++) - '0'; return i;
}
#define ZEROPAD 1 /* pad with zero */ #define SIGN 2 /* unsigned/signed long */ #define PLUS 4 /* show plus */ #define SPACE 8 /* space if plus */ #define LEFT 16 /* left justified */ #define SMALL 32 /* Must be 32 == 0x20 */ #define SPECIAL 64 /* 0x */
staticchar *number(char *str, constchar *end, long num, int base, int size, int precision, int type)
{ /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ staticconstchar digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
char tmp[66]; char c, sign, locase; int i;
/* * locase = 0 or 0x20. ORing digits or letters with 'locase' * produces same digits or (maybe lowercased) letters
*/
locase = (type & SMALL); if (type & LEFT)
type &= ~ZEROPAD; if (base < 2 || base > 16) return NULL;
c = (type & ZEROPAD) ? '0' : ' ';
sign = 0; if (type & SIGN) { if (num < 0) {
sign = '-';
num = -num;
size--;
} elseif (type & PLUS) {
sign = '+';
size--;
} elseif (type & SPACE) {
sign = ' ';
size--;
}
} if (type & SPECIAL) { if (base == 16)
size -= 2; elseif (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'); elseif (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, constchar *fmt, va_list args)
{ char *str, *end; constchar *s;
uint64_t 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 */
/* get field width */
field_width = -1; if (isdigit(*fmt))
field_width = skip_atoi(&fmt); elseif (*fmt == '*') {
++fmt; /* it's the next argument */
field_width = va_arg(args, int); if (field_width < 0) {
field_width = -field_width;
flags |= LEFT;
}
}
/* get the precision */
precision = -1; if (*fmt == '.') {
++fmt; if (isdigit(*fmt))
precision = skip_atoi(&fmt); elseif (*fmt == '*') {
++fmt; /* it's the next argument */
precision = va_arg(args, int);
} if (precision < 0)
precision = 0;
}
/* get the conversion qualifier */
qualifier = -1; if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
qualifier = *fmt;
++fmt;
}
/* * Play nice with %llu, %llx, etc. KVM selftests only support * 64-bit builds, so just treat %ll* the same as %l*.
*/ if (qualifier == 'l' && *fmt == 'l')
++fmt;
/* default base */
base = 10;
switch (*fmt) { case'c': if (!(flags & LEFT)) while (--field_width > 0)
APPEND_BUFFER_SAFE(str, end, ' ');
APPEND_BUFFER_SAFE(str, end,
(uint8_t)va_arg(args, int)); while (--field_width > 0)
APPEND_BUFFER_SAFE(str, end, ' '); continue;
case's':
s = va_arg(args, char *);
len = strnlen(s, precision);
if (!(flags & LEFT)) while (len < field_width--)
APPEND_BUFFER_SAFE(str, end, ' '); for (i = 0; i < len; ++i)
APPEND_BUFFER_SAFE(str, end, *s++); while (len < field_width--)
APPEND_BUFFER_SAFE(str, end, ' '); continue;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.