view src/usr/printf.c @ 21:45035047d186

use stdarg for printf
author mir3636
date Sat, 07 Apr 2018 18:44:18 +0900
parents 83c23a36980d
children
line wrap: on
line source

#include "types.h"
#include "stat.h"
#include "user.h"
#include <stdarg.h>

static void
putc(int fd, char c)
{
    write(fd, &c, 1);
}

static void
printint(int fd, int xx, int base, int sgn)
{
    static char digits[] = "0123456789ABCDEF";
    char buf[16];
    int i, neg;
    uint x;
    
    neg = 0;
    if(sgn && xx < 0){
        neg = 1;
        x = -xx;
    } else {
        x = xx;
    }
    
    i = 0;
    do{
        buf[i++] = digits[x % base];
    }while((x /= base) != 0);
    if(neg)
        buf[i++] = '-';
    
    while(--i >= 0)
        putc(fd, buf[i]);
}

// Print to the given fd. Only understands %d, %x, %p, %s.
void
printf(int fd, char *fmt, ...)
{
    char *s;
    int c, i, state;
    va_list ap;
    
    state = 0;
    va_start(ap, fmt); 
    for(i = 0; fmt[i]; i++){
        c = fmt[i] & 0xff;
        if(state == 0){
            if(c == '%'){
                state = '%';
            } else {
                putc(fd, c);
            }
        } else if(state == '%'){
            if(c == 'd'){
                printint(fd, va_arg(ap,int), 10, 1);
            } else if(c == 'x' || c == 'p'){
                printint(fd, va_arg(ap,int), 16, 0);
            } else if(c == 's'){
                s = va_arg(ap,char*);
                if(s == 0)
                    s = "(null)";
                while(*s != 0){
                    putc(fd, *s);
                    s++;
                }
            } else if(c == 'c'){
                putc(fd, va_arg(ap,int));
            } else if(c == '%'){
                putc(fd, c);
            } else {
                // Unknown % sequence.  Print it to draw attention.
                putc(fd, '%');
                putc(fd, c);
            }
            state = 0;
        }
    }
    va_end(ap);
}