--- Gnu-Mach/kern/printf.c 2020/09/02 04:36:57 1.1 +++ 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,10 +116,12 @@ * (compatibility) */ +#include +#include +#include #include #include -#include -#include +#include #define isdigit(d) ((d) >= '0' && (d) <= '9') #define Ctod(c) ((c) - '0') @@ -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) { @@ -161,8 +167,8 @@ void printf_init() } void _doprnt( - register char *fmt, - va_list *argp, + register const char *fmt, + 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,24 +514,21 @@ void _doprnt( /* * Printing (to console) */ -extern void cnputc( char, /*not really*/vm_offset_t); -void vprintf(fmt, listp) - 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(fmt, va_alist) - char * fmt; - va_dcl +int printf(const char *fmt, ...) { va_list listp; - va_start(listp); + va_start(listp, fmt); vprintf(fmt, listp); va_end(listp); + return 0; } int indent = 0; @@ -533,9 +537,7 @@ int indent = 0; * Printing (to console) with indentation. */ /*VARARGS1*/ -void iprintf(fmt, va_alist) - char * fmt; - va_dcl +void iprintf(const char *fmt, ...) { va_list listp; register int i; @@ -550,8 +552,8 @@ void iprintf(fmt, va_alist) i--; } } - va_start(listp); - _doprnt(fmt, &listp, cnputc, 16, 0); + va_start(listp, fmt); + _doprnt(fmt, listp, (void (*)( char, vm_offset_t)) cnputc, 16, 0); va_end(listp); } @@ -572,22 +574,47 @@ sputc( } int -sprintf( buf, fmt, va_alist) - char *buf; - char *fmt; - va_dcl +sprintf(char *buf, const char *fmt, ...) { va_list listp; char *start = buf; - va_start(listp); - _doprnt(fmt, &listp, sputc, 16, (vm_offset_t)&buf); + va_start(listp, fmt); + _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;