xv6

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 40884996414a0b5bacdf306a98531f55cebdfbfe
parent 3994dade649dba764fdab1b8660ef609ab373fc9
Author: Brian Swetland <swetland@frotz.net>
Date:   Sun, 29 Dec 2013 09:44:41 -0800

update printf() and cprintf() to use stdarg.h

Rolling your own stdarg stuff is cute, but AMD64 passes the first six parameters
via registers, using al as a count, etc.  Better to trust the toolchain to get
it right.

Diffstat:
Mkernel/console.c | 13++++++++-----
Mulib/printf.c | 18++++++++----------
2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/kernel/console.c b/kernel/console.c @@ -2,6 +2,8 @@ // Input is from the keyboard or serial port. // Output is written to the screen and serial port. +#include <stdarg.h> + #include "types.h" #include "defs.h" #include "param.h" @@ -53,10 +55,12 @@ printint(int xx, int base, int sign) void cprintf(char *fmt, ...) { + va_list ap; int i, c, locking; - uint *argp; char *s; + va_start(ap, fmt); + locking = cons.locking; if(locking) acquire(&cons.lock); @@ -64,7 +68,6 @@ cprintf(char *fmt, ...) if (fmt == 0) panic("null fmt"); - argp = (uint*)(void*)(&fmt + 1); for(i = 0; (c = fmt[i] & 0xff) != 0; i++){ if(c != '%'){ consputc(c); @@ -75,14 +78,14 @@ cprintf(char *fmt, ...) break; switch(c){ case 'd': - printint(*argp++, 10, 1); + printint(va_arg(ap, int), 10, 1); break; case 'x': case 'p': - printint(*argp++, 16, 0); + printint(va_arg(ap, int), 16, 0); break; case 's': - if((s = (char*)*argp++) == 0) + if((s = va_arg(ap, char*)) == 0) s = "(null)"; for(; *s; s++) consputc(*s); diff --git a/ulib/printf.c b/ulib/printf.c @@ -1,3 +1,5 @@ +#include <stdarg.h> + #include "types.h" #include "stat.h" #include "user.h" @@ -39,12 +41,12 @@ printint(int fd, int xx, int base, int sgn) void printf(int fd, char *fmt, ...) { + va_list ap; char *s; int c, i, state; - uint *ap; + va_start(ap, fmt); state = 0; - ap = (uint*)(void*)&fmt + 1; for(i = 0; fmt[i]; i++){ c = fmt[i] & 0xff; if(state == 0){ @@ -55,14 +57,11 @@ printf(int fd, char *fmt, ...) } } else if(state == '%'){ if(c == 'd'){ - printint(fd, *ap, 10, 1); - ap++; + printint(fd, va_arg(ap, int), 10, 1); } else if(c == 'x' || c == 'p'){ - printint(fd, *ap, 16, 0); - ap++; + printint(fd, va_arg(ap, int), 16, 0); } else if(c == 's'){ - s = (char*)*ap; - ap++; + s = va_arg(ap, char*); if(s == 0) s = "(null)"; while(*s != 0){ @@ -70,8 +69,7 @@ printf(int fd, char *fmt, ...) s++; } } else if(c == 'c'){ - putc(fd, *ap); - ap++; + putc(fd, va_arg(ap, uint)); } else if(c == '%'){ putc(fd, c); } else {