--- Gnu-Mach/kern/printf.c 2020/09/02 04:42:46 1.1.1.2 +++ Gnu-Mach/kern/printf.c 2020/09/02 04:45:18 1.1.1.3 @@ -39,7 +39,10 @@ * FILE *fd; * char *format; * { + * va_list listp; + * va_start(listp, fmt); * _doprnt(format, &args, fd); + * va_end(listp); * } * * would suffice. (This example does not handle the fprintf's "return @@ -50,6 +53,7 @@ * * %d decimal conversion * %u unsigned conversion + * %p pointer address * %x hexadecimal conversion * %X hexadecimal conversion with capital letters * %o octal conversion @@ -112,9 +116,11 @@ * (compatibility) */ +#include +#include +#include #include #include -#include #include #define isdigit(d) ((d) >= '0' && (d) <= '9') @@ -151,7 +157,7 @@ boolean_t _doprnt_truncates = FALSE; boolean_t _doprnt_lock_initialized = FALSE; decl_simple_lock_data(,_doprnt_lock) -void printf_init() +void printf_init(void) { if (!_doprnt_lock_initialized) { @@ -162,7 +168,7 @@ void printf_init() void _doprnt( register const char *fmt, - va_list *argp, + va_list argp, /* character output routine */ void (*putc)( char, vm_offset_t), int radix, /* default radix - for '%r' */ @@ -245,7 +251,7 @@ void _doprnt( } } else if (c == '*') { - length = va_arg(*argp, int); + length = va_arg(argp, int); c = *++fmt; if (length < 0) { ladjust = !ladjust; @@ -263,7 +269,7 @@ void _doprnt( } } else if (c == '*') { - prec = va_arg(*argp, int); + prec = va_arg(argp, int); c = *++fmt; } } @@ -281,8 +287,8 @@ void _doprnt( boolean_t any; register int i; - u = va_arg(*argp, unsigned long); - p = va_arg(*argp, char *); + u = va_arg(argp, unsigned long); + p = va_arg(argp, char *); base = *p++; printnum(u, base, putc, putc_arg); @@ -290,7 +296,7 @@ void _doprnt( break; any = FALSE; - while (i = *p++) { + while ((i = *p++)) { /* NOTE: The '32' here is because ascii space */ if (*p <= 32) { /* @@ -330,7 +336,7 @@ void _doprnt( } case 'c': - c = va_arg(*argp, int); + c = va_arg(argp, int); (*putc)(c, putc_arg); break; @@ -342,7 +348,7 @@ void _doprnt( if (prec == -1) prec = 0x7fffffff; /* MAXINT */ - p = va_arg(*argp, char *); + p = va_arg(argp, char *); if (p == (char *)0) p = ""; @@ -399,6 +405,7 @@ void _doprnt( base = 10; goto print_unsigned; + case 'p': case 'x': truncate = _doprnt_truncates; case 'X': @@ -424,7 +431,7 @@ void _doprnt( goto print_unsigned; print_signed: - n = va_arg(*argp, long); + n = va_arg(argp, long); if (n >= 0) { u = n; sign_char = plus_sign; @@ -436,7 +443,7 @@ void _doprnt( goto print_num; print_unsigned: - u = va_arg(*argp, unsigned long); + u = va_arg(argp, unsigned long); goto print_num; print_num: @@ -507,20 +514,21 @@ void _doprnt( /* * Printing (to console) */ -extern void cnputc( char, /*not really*/vm_offset_t); -void vprintf(const char *fmt, va_list listp) +int vprintf(const char *fmt, va_list listp) { - _doprnt(fmt, &listp, cnputc, 16, 0); + _doprnt(fmt, listp, (void (*)( char, vm_offset_t)) cnputc, 16, 0); + return 0; } /*VARARGS1*/ -void printf(const char *fmt, ...) +int printf(const char *fmt, ...) { va_list listp; va_start(listp, fmt); vprintf(fmt, listp); va_end(listp); + return 0; } int indent = 0; @@ -545,7 +553,7 @@ void iprintf(const char *fmt, ...) } } va_start(listp, fmt); - _doprnt(fmt, &listp, cnputc, 16, 0); + _doprnt(fmt, listp, (void (*)( char, vm_offset_t)) cnputc, 16, 0); va_end(listp); } @@ -572,13 +580,41 @@ sprintf(char *buf, const char *fmt, ...) char *start = buf; va_start(listp, fmt); - _doprnt(fmt, &listp, sputc, 16, (vm_offset_t)&buf); + _doprnt(fmt, listp, sputc, 16, (vm_offset_t)&buf); va_end(listp); *buf = 0; return (buf - start); } +struct vsnprintf_cookie +{ + char *buf; + int index; + int max_len; +}; + +static void +snputc(char c, vm_offset_t arg) +{ + struct vsnprintf_cookie *cookie = (void *) arg; + + if (cookie->index < cookie->max_len) + cookie->buf[cookie->index ++] = c; +} + +int +vsnprintf(char *buf, size_t size, const char *fmt, va_list args) +{ + struct vsnprintf_cookie cookie + = { .buf = buf, .index = 0, .max_len = size }; + + _doprnt (fmt, args, snputc, 16, (vm_offset_t)&cookie); + cookie.buf[cookie.index] = '\0'; + + return cookie.index; +} + void safe_gets(str, maxlen) char *str;