changeset 21:45035047d186

use stdarg for printf
author mir3636
date Sat, 07 Apr 2018 18:44:18 +0900
parents 33c9000056b5
children 397e74cbf14e
files src/usr/printf.c
diffstat 1 files changed, 8 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/usr/printf.c	Sat Apr 07 15:50:32 2018 +0900
+++ b/src/usr/printf.c	Sat Apr 07 18:44:18 2018 +0900
@@ -1,6 +1,7 @@
 #include "types.h"
 #include "stat.h"
 #include "user.h"
+#include <stdarg.h>
 
 static void
 putc(int fd, char c)
@@ -41,10 +42,10 @@
 {
     char *s;
     int c, i, state;
-    uint *ap;
+    va_list ap;
     
     state = 0;
-    ap = (uint*)(void*)&fmt + 1;
+    va_start(ap, fmt); 
     for(i = 0; fmt[i]; i++){
         c = fmt[i] & 0xff;
         if(state == 0){
@@ -55,14 +56,11 @@
             }
         } 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 +68,7 @@
                     s++;
                 }
             } else if(c == 'c'){
-                putc(fd, *ap);
-                ap++;
+                putc(fd, va_arg(ap,int));
             } else if(c == '%'){
                 putc(fd, c);
             } else {
@@ -82,4 +79,5 @@
             state = 0;
         }
     }
+    va_end(ap);
 }