comparison uprogs/printf.c @ 0:ed10291ff195

first commit
author mir3636
date Sun, 06 Jan 2019 19:27:03 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ed10291ff195
1 #include "types.h"
2 #include "stat.h"
3 #include "user.h"
4
5 static void
6 putc(int fd, char c)
7 {
8 write(fd, &c, 1);
9 }
10
11 u32 div(u32 n, u32 d) // long division
12 {
13 u32 q=0, r=0;
14 int i;
15
16 for(i=31;i>=0;i--){
17 r = r << 1;
18 r = r | ((n >> i) & 1);
19 if(r >= d) {
20 r = r - d;
21 q = q | (1 << i);
22 }
23 }
24 return q;
25 }
26
27 static void
28 printint(int fd, int xx, int base, int sgn)
29 {
30 static char digits[] = "0123456789ABCDEF";
31 char buf[16];
32 int i, neg;
33 uint x, y, b;
34
35 neg = 0;
36 if(sgn && xx < 0){
37 neg = 1;
38 x = -xx;
39 } else {
40 x = xx;
41 }
42
43 b = base;
44 i = 0;
45 do{
46 y = div(x, b);
47 buf[i++] = digits[x - y * b];
48 }while((x = y) != 0);
49 if(neg)
50 buf[i++] = '-';
51
52 while(--i >= 0)
53 putc(fd, buf[i]);
54 }
55
56 // Print to the given fd. Only understands %d, %x, %p, %s.
57 void
58 printf(int fd, char *fmt, ...)
59 {
60 char *s;
61 int c, i, state;
62 uint *ap;
63
64 state = 0;
65 ap = (uint*)(void*)&fmt + 1;
66 for(i = 0; fmt[i]; i++){
67 c = fmt[i] & 0xff;
68 if(state == 0){
69 if(c == '%'){
70 state = '%';
71 } else {
72 putc(fd, c);
73 }
74 } else if(state == '%'){
75 if(c == 'd'){
76 printint(fd, *ap, 10, 1);
77 ap++;
78 } else if(c == 'x' || c == 'p'){
79 printint(fd, *ap, 16, 0);
80 ap++;
81 } else if(c == 's'){
82 s = (char*)*ap;
83 ap++;
84 if(s == 0)
85 s = "(null)";
86 while(*s != 0){
87 putc(fd, *s);
88 s++;
89 }
90 } else if(c == 'c'){
91 putc(fd, *ap);
92 ap++;
93 } else if(c == '%'){
94 putc(fd, c);
95 } else {
96 // Unknown % sequence. Print it to draw attention.
97 putc(fd, '%');
98 putc(fd, c);
99 }
100 state = 0;
101 }
102 }
103 }