2006-08-10 03:28:57 +02:00
|
|
|
#include "types.h"
|
2006-08-14 23:22:13 +02:00
|
|
|
#include "stat.h"
|
|
|
|
#include "user.h"
|
2006-08-10 03:28:57 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
putc(int fd, char c)
|
|
|
|
{
|
2006-09-06 19:27:19 +02:00
|
|
|
write(fd, &c, 1);
|
2006-08-10 03:28:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
printint(int fd, int xx, int base, int sgn)
|
|
|
|
{
|
2007-08-10 19:17:42 +02:00
|
|
|
static char digits[] = "0123456789ABCDEF";
|
2006-08-10 03:28:57 +02:00
|
|
|
char buf[16];
|
2007-08-10 19:17:42 +02:00
|
|
|
int i, neg;
|
2006-08-10 03:28:57 +02:00
|
|
|
uint x;
|
2006-09-06 19:27:19 +02:00
|
|
|
|
2007-08-10 19:17:42 +02:00
|
|
|
neg = 0;
|
2006-08-10 03:28:57 +02:00
|
|
|
if(sgn && xx < 0){
|
|
|
|
neg = 1;
|
2007-08-10 19:17:42 +02:00
|
|
|
x = -xx;
|
2006-08-10 03:28:57 +02:00
|
|
|
} else {
|
|
|
|
x = xx;
|
|
|
|
}
|
|
|
|
|
2007-08-10 19:17:42 +02:00
|
|
|
i = 0;
|
2006-08-10 03:28:57 +02:00
|
|
|
do {
|
|
|
|
buf[i++] = digits[x % base];
|
|
|
|
} while((x /= base) != 0);
|
|
|
|
if(neg)
|
|
|
|
buf[i++] = '-';
|
|
|
|
|
|
|
|
while(--i >= 0)
|
|
|
|
putc(fd, buf[i]);
|
|
|
|
}
|
|
|
|
|
2006-09-06 19:50:20 +02:00
|
|
|
// Print to the given fd. Only understands %d, %x, %p, %s.
|
2006-08-10 03:28:57 +02:00
|
|
|
void
|
|
|
|
printf(int fd, char *fmt, ...)
|
|
|
|
{
|
2007-08-10 19:17:42 +02:00
|
|
|
char *s;
|
|
|
|
int c, i, state;
|
|
|
|
uint *ap;
|
2006-08-10 03:28:57 +02:00
|
|
|
|
2007-08-10 19:17:42 +02:00
|
|
|
state = 0;
|
|
|
|
ap = (uint*)(void*)&fmt + 1;
|
2006-08-10 03:28:57 +02:00
|
|
|
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, *ap, 10, 1);
|
|
|
|
ap++;
|
|
|
|
} else if(c == 'x' || c == 'p'){
|
|
|
|
printint(fd, *ap, 16, 0);
|
|
|
|
ap++;
|
|
|
|
} else if(c == 's'){
|
2007-08-10 19:17:42 +02:00
|
|
|
s = (char*)*ap;
|
2006-08-10 03:28:57 +02:00
|
|
|
ap++;
|
|
|
|
while(*s != 0){
|
|
|
|
putc(fd, *s);
|
|
|
|
s++;
|
|
|
|
}
|
2006-08-14 23:22:13 +02:00
|
|
|
} else if(c == 'c'){
|
2006-09-06 19:04:06 +02:00
|
|
|
putc(fd, *ap);
|
|
|
|
ap++;
|
2006-08-10 03:28:57 +02:00
|
|
|
} else if(c == '%'){
|
|
|
|
putc(fd, c);
|
|
|
|
} else {
|
|
|
|
// Unknown % sequence. Print it to draw attention.
|
|
|
|
putc(fd, '%');
|
|
|
|
putc(fd, c);
|
|
|
|
}
|
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|