annotate cos/kernel/snprintf.c @ 9:92ace1ca50a8

64 bits kernel without interrupts but with printf in C
author windel
date Sun, 13 Nov 2011 12:47:47 +0100
parents
children fcdae30b2782
rev   line source
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
1 /*
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
2 * snprintf.c
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
3 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
4 #include "kernel.h"
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
5
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
6 #include "stdarg.h"
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
7 #include "ctype.h"
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
8
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
9 unsigned char _ctype[] = {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
10 _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
11 _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
12 _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
13 _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
14 _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
15 _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
16 _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
17 _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
18 _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
19 _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
20 _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
21 _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
22 _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
23 _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
24 _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
25 _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
26 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
27 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
28 _S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 160-175 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
29 _P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 176-191 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
30 _U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U, /* 192-207 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
31 _U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L, /* 208-223 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
32 _L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L, /* 224-239 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
33 _L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L}; /* 240-255 */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
34
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
35
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
36 #define MORE_THAN_YOU_WANT 1<<30
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
37 #define MAX_STDOUT_CHARS 255
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
38
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
39 static char hexmap[] = {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
40 '0', '1', '2', '3', '4', '5', '6', '7',
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
41 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
42 };
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
43
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
44 void va_snprintf(char *b, int l, const char *fmt, va_list pvar)
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
45 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
46 int n, i;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
47 uint_t u;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
48 ulonglong_t ull;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
49 char *t;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
50 char d[10];
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
51 char mod_l;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
52 char mod_ll;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
53
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
54 if (!fmt || !b || (l < 1))
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
55 return;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
56
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
57 mod_l = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
58 mod_ll = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
59
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
60 while (l && *fmt) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
61 if (*fmt == '%') {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
62 if (!(--l))
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
63 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
64 again:
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
65 fmt++;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
66
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
67 switch (*fmt) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
68 case '.': /* precision modifier */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
69 while(isdigit(fmt[1]))
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
70 fmt++;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
71 goto again;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
72
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
73 case 'l': /* long modifier */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
74 if (!mod_l) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
75 mod_l = 1;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
76 } else if (!mod_ll) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
77 mod_l = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
78 mod_ll = 1;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
79 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
80 goto again;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
81
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
82 case 's': /* string */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
83 t = va_arg(pvar, char *);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
84 while (l && *t)
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
85 *b++ = *t++, l--;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
86 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
87
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
88 case 'c': /* single character */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
89 *b++ = va_arg(pvar, char);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
90 l--;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
91 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
92
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
93 case 'S': /* uint32 as a short ... */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
94 if (l < 4) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
95 l = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
96 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
97 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
98 u = va_arg(pvar, unsigned int);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
99 for (i = 3; i >= 0; i--) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
100 b[i] = hexmap[u & 0x0F];
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
101 u >>= 4;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
102 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
103 b += 4;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
104 l -= 4;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
105 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
106
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
107 case 'x':
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
108 case 'p':
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
109 if (!mod_ll) { /* 8 digit, unsigned 32-bit hex integer */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
110 if (l < 8) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
111 l = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
112 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
113 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
114 u = va_arg(pvar, unsigned int);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
115 for (i = 7; i >= 0; i--) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
116 b[i] = hexmap[u & 0x0F];
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
117 u >>= 4;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
118 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
119 b += 8;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
120 l -= 8;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
121 } else if (mod_ll) { /* 16 digit, unsigned 64-bit hex integer */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
122 if (l < 16) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
123 l = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
124 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
125 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
126 ull = va_arg(pvar, unsigned long long);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
127 for (i = 15; i >= 0; i--) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
128 b[i] = hexmap[ull & 0x0f];
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
129 ull >>= 4;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
130 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
131 b += 16;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
132 l -= 16;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
133 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
134 mod_l = mod_ll = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
135 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
136
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
137 case 'd': /* signed integer */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
138 n = va_arg(pvar, int);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
139 if (n < 0) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
140 u = -n;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
141 *b++ = '-';
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
142 if (!(--l))
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
143 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
144 } else {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
145 u = n;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
146 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
147 goto u2;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
148
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
149 case 'u': /* unsigned integer */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
150 u = va_arg(pvar, unsigned int);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
151 u2:
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
152 i = 9;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
153 do {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
154 d[i] = (u % 10) + '0';
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
155 u /= 10;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
156 i--;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
157 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
158 while (u && i >= 0);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
159 while (++i < 10) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
160 *b++ = d[i];
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
161 if (!(--l))
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
162 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
163 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
164 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
165
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
166 case 'U':
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
167 u = va_arg(pvar, unsigned int);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
168 i = 9;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
169 d[8] = d[7] = d[6] = ' ';
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
170 do {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
171 d[i] = (u % 10) + '0';
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
172 u /= 10;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
173 i--;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
174 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
175 while (u && i >= 0);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
176 i = 5;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
177 while (++i < 10) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
178 *b++ = d[i];
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
179 if (!(--l))
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
180 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
181 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
182 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
183
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
184 case 'X': /* 2 digit, unsigned 8bit hex int */
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
185 if (l < 2) {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
186 l = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
187 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
188 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
189 n = va_arg(pvar, int);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
190 *b++ = hexmap[(n & 0xF0) >> 4];
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
191 *b++ = hexmap[n & 0x0F];
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
192 l -= 2;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
193 break;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
194 default:
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
195 *b++ = *fmt;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
196 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
197 } else {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
198 *b++ = *fmt;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
199 l--;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
200 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
201 fmt++;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
202 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
203 *b = 0;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
204 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
205
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
206 void snprintf(char *str, int len, char *fmt, ...)
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
207 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
208 va_list pvar;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
209 va_start(pvar, fmt);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
210 va_snprintf(str, len, fmt, pvar);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
211 va_end(pvar);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
212 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
213
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
214 void
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
215 sprintf(char *dst, const char *fmt, ...)
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
216 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
217 va_list args;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
218 va_start(args, fmt);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
219 va_snprintf(dst, MORE_THAN_YOU_WANT, fmt, args);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
220 va_end(args);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
221 }
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
222
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
223 void
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
224 printf(const char* fmt, ...)
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
225 {
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
226 static char buf[MAX_STDOUT_CHARS];
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
227
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
228 va_list args;
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
229 va_start(args, fmt);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
230 va_snprintf(buf, MAX_STDOUT_CHARS, fmt, args);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
231 va_end(args);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
232
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
233 print_string(buf);
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents:
diff changeset
234 }